001package Torello.JSON;
002
003import java.util.function.Consumer;
004import java.util.Objects;
005
006import Torello.Java.Function.IntIntTConsumer;
007
008public class CHANGEABLE_CONSUMER<T>
009{
010    private Consumer<T>         userConsumerV1 = null;
011    private IntIntTConsumer<T>  userConsumerV2 = null;
012
013    final Consumer<T>           wrappedUserConsumerV1;
014    final IntIntTConsumer<T>    wrappedUserConsumerV2;
015
016    private final boolean V1_OR_V2;
017
018    CHANGEABLE_CONSUMER(final boolean V1_OR_V2)
019    {
020        this.V1_OR_V2 = V1_OR_V2;
021
022        if (V1_OR_V2)
023        {
024            this.wrappedUserConsumerV1 = (T theValue) -> this.userConsumerV1.accept(theValue);
025            this.wrappedUserConsumerV2 = null;
026        }
027
028        else
029        {
030            this.wrappedUserConsumerV1 = null;
031
032            this.wrappedUserConsumerV2 =
033                (int jsonArrayIndex, int javaOutCount, T theValue) -> 
034                    this.userConsumerV2.accept(jsonArrayIndex, javaOutCount, theValue);
035        }
036    }
037
038    void setConsumer(final Consumer<T> c)
039    {
040        if (! V1_OR_V2) throw new WrongModeException(
041            "This CHANGEABLE CONSUMER was initialized in V2 mode (IntIntTConsumer), " +
042            "but you're attempting to call a V1-specific method (Consumer<T>).\n" +
043            "To register a new Consumer with this SettingsRec, please provide one which also " +
044            "accepts two integer parameters, in addition to a datum.\n" +
045            "Expected Consumer Signature: (int jsonArrayIndex, int javaOutCount, T theValue)"
046        );
047
048        Objects.requireNonNull(c, "You have passed null to Consumer-Parameter 'c'");
049        this.userConsumerV1 = c;
050    }
051
052    void setConsumer(final IntIntTConsumer<T> c)
053    {
054        if (V1_OR_V2) throw new WrongModeException(
055            "This CHANGEABLE CONSUMER was initialized in V1 mode (Consumer<T>), " +
056            "but you're attempting to call a V2-specific method (IntIntTConsumer<T>).\n" +
057            "To register a new Consumer with this SettingsRec, please provide one which accepts " +
058            "only the datum.\n" +
059            "Expected Consumer Signature: (T theValue)"
060        );
061
062        Objects.requireNonNull(c, "You have passed null to IntIntTConsumer-Parameter 'c'");
063        this.userConsumerV2 = c;
064    }
065
066    private static final String exMsg = 
067        "You have not intialized the SettingsRec with a newly prepared Consumer.  The primary " +
068        "purpose of utilizing a SettingsRec instance is to reuse the configurations inside the " +
069        "record.  When you are transferring data to a Java consumer, you must first assing a " +
070        "Consumer to this record.";
071
072    // This is to be used by the "SettingsRec.opener"
073    void openerCode()
074    {
075        if (V1_OR_V2)   Objects.requireNonNull(this.userConsumerV1, exMsg);
076        else            Objects.requireNonNull(this.userConsumerV2, exMsg);
077    }
078}