1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 | package Torello.JSON;
import java.util.function.Consumer;
import java.util.Objects;
import Torello.Java.Function.IntIntTConsumer;
public class CHANGEABLE_CONSUMER<T>
{
private Consumer<T> userConsumerV1 = null;
private IntIntTConsumer<T> userConsumerV2 = null;
final Consumer<T> wrappedUserConsumerV1;
final IntIntTConsumer<T> wrappedUserConsumerV2;
private final boolean V1_OR_V2;
CHANGEABLE_CONSUMER(final boolean V1_OR_V2)
{
this.V1_OR_V2 = V1_OR_V2;
if (V1_OR_V2)
{
this.wrappedUserConsumerV1 = (T theValue) -> this.userConsumerV1.accept(theValue);
this.wrappedUserConsumerV2 = null;
}
else
{
this.wrappedUserConsumerV1 = null;
this.wrappedUserConsumerV2 =
(int jsonArrayIndex, int javaOutCount, T theValue) ->
this.userConsumerV2.accept(jsonArrayIndex, javaOutCount, theValue);
}
}
void setConsumer(final Consumer<T> c)
{
if (! V1_OR_V2) throw new WrongModeException(
"This CHANGEABLE CONSUMER was initialized in V2 mode (IntIntTConsumer), " +
"but you're attempting to call a V1-specific method (Consumer<T>).\n" +
"To register a new Consumer with this SettingsRec, please provide one which also " +
"accepts two integer parameters, in addition to a datum.\n" +
"Expected Consumer Signature: (int jsonArrayIndex, int javaOutCount, T theValue)"
);
Objects.requireNonNull(c, "You have passed null to Consumer-Parameter 'c'");
this.userConsumerV1 = c;
}
void setConsumer(final IntIntTConsumer<T> c)
{
if (V1_OR_V2) throw new WrongModeException(
"This CHANGEABLE CONSUMER was initialized in V1 mode (Consumer<T>), " +
"but you're attempting to call a V2-specific method (IntIntTConsumer<T>).\n" +
"To register a new Consumer with this SettingsRec, please provide one which accepts " +
"only the datum.\n" +
"Expected Consumer Signature: (T theValue)"
);
Objects.requireNonNull(c, "You have passed null to IntIntTConsumer-Parameter 'c'");
this.userConsumerV2 = c;
}
private static final String exMsg =
"You have not intialized the SettingsRec with a newly prepared Consumer. The primary " +
"purpose of utilizing a SettingsRec instance is to reuse the configurations inside the " +
"record. When you are transferring data to a Java consumer, you must first assing a " +
"Consumer to this record.";
// This is to be used by the "SettingsRec.opener"
void openerCode()
{
if (V1_OR_V2) Objects.requireNonNull(this.userConsumerV1, exMsg);
else Objects.requireNonNull(this.userConsumerV2, exMsg);
}
}
|