001package Torello.JSON;
002
003import Torello.Java.Additional.Counter;
004import Torello.Java.Function.IntIntTConsumer;
005
006
007import java.util.function.Consumer;
008import java.util.function.ObjIntConsumer;
009import javax.json.JsonValue;
010
011public class ACCEPTOR<T>
012{
013    private final Counter acceptCounter = new Counter(0);
014
015    private final T                 defaultValue;
016    private final ObjIntConsumer<T> MAIN_ACCEPTOR;
017
018    void accept(final T t, final int jsonArrayIndex)
019    { MAIN_ACCEPTOR.accept(t, jsonArrayIndex); }
020
021    void reset()
022    { this.acceptCounter.set(0); }
023
024    int retrieveCount()
025    { return this.acceptCounter.size(); }
026
027    ACCEPTOR(final SETTINGS_REC_BUILDER<T, ?> b)
028    {
029        super();
030
031        this.defaultValue = b.defaultValue;
032    
033        final Consumer<T>           oneArgAcceptor      = b.oneArgAcceptor();
034        final IntIntTConsumer<T>    threeArgAcceptor    = b.threeArgAcceptor();
035
036        if (    (oneArgAcceptor != null)
037            &&  (threeArgAcceptor != null)
038        )
039            throw new RJArrSettingsError(
040                "The ACCEPTOR constructor seems to have been invoked with two non-null user " +
041                "consumers.  It is expected that the ACCEPTOR constructor be invoked with " +
042                "precisely 1 user based consumer."
043            );
044
045        if (oneArgAcceptor != null) this.MAIN_ACCEPTOR =
046            (final T t, int jsonArrayIndex) ->
047                oneArgAcceptor.accept(t);
048
049        else if (threeArgAcceptor != null) this.MAIN_ACCEPTOR =
050            (final T t, int jsonArrayIndex) ->
051                threeArgAcceptor.accept(jsonArrayIndex, this.acceptCounter.addOne(), t);
052
053        else throw new RJArrSettingsError(
054            "While building the ACCEPTOR, there doesn't appear to be a non-null consumer.  It " +
055            "is expected that the ACCEPTOR constructor be invoked with precisely 1 user based " +
056            "consumer."
057        );
058    }
059
060    void AN(int i)      { MAIN_ACCEPTOR.accept(null, i); }
061    void AD(int i)      { MAIN_ACCEPTOR.accept(defaultValue, i); }
062    void NOOP(int i)    { }
063
064    void AN(JsonValue jv, int i)    { MAIN_ACCEPTOR.accept(null, i); }
065    void AD(JsonValue jv, int i)    { MAIN_ACCEPTOR.accept(defaultValue, i); }
066    void NOOP(JsonValue jv, int i)  { }
067}