    001    package com.hammurapi.eventbus;
    002
    003    import com.hammurapi.extract.CommutativeAnd;
    004    import com.hammurapi.extract.Predicate;
    005    import com.hammurapi.extract.True;
    006
    007    /**
    008     * Base class for event handlers.
    009     * @author Pavel Vlasov
    010     *
    011     * @param <E>
    012     * @param <P>
    013     * @param <C>
    014     */
    015    public abstract class AbstractEventHandler<E, P extends Comparable<P>, C, H extends EventBus.Handle<E,P,C>, S extends EventStore<E,P,C,H,S>> implements EventHandler<E, P, C, H, S> {
    016
    017            private boolean oneOff;
    018            private Mode mode = Mode.POST;
    019            private Predicate<E, C> predicate;
    020
    021            public AbstractEventHandler() {
    022                    // Default constructor.
    023            }
    024
    025            public AbstractEventHandler(
    026                            int cardinality,
    027                            P priority,
    028                            C context,
    029                            boolean consumes,
    030                            boolean oneOff,
    031                            Mode mode,
    032                            Predicate<E, C> predicate) {
    033                    super();
    034                    this.cardinality = cardinality;
    035                    this.priority = priority;
    036                    this.context = context;
    037                    this.consumes = consumes;
    038                    this.oneOff = oneOff;
    039                    this.mode = mode;
    040                    if (predicate == null) {
    041                            this.predicate = True.getInstance();
    042                    } else {
    043                            this.predicate = predicate;
    044                    }
    045            }
    046
    047            public AbstractEventHandler(
    048                            int cardinality,
    049                            P priority,
    050                            C context,
    051                            boolean consumes,
    052                            boolean oneOff,
    053                            Mode mode,
    054                            Predicate<E, C>... predicates) {
    055                    this(cardinality, priority, context, consumes, oneOff, mode, wrap(predicates));
    056            }
    057
    058            private static <E,C> Predicate<E, C> wrap(Predicate<E, C>[] predicates) {
    059                    if (predicates.length==0) {
    060                            return True.getInstance();
    061                    }
    062
    063                    if (predicates.length==1) {
    064                            return predicates[0];
    065                    }
    066
    067                    return new CommutativeAnd<E,C>(0, null, predicates);
    068            }
    069
    070            private boolean consumes = true;
    071            private P priority;
    072            private int cardinality = 1;
    073            private C context;
    074
    075            public void setCardinality(int cardinality) {
    076                    this.cardinality = cardinality;
    077            }
    078
    079            public void setConsumes(boolean consumes) {
    080                    this.consumes = consumes;
    081            }
    082
    083            public void setContext(C context) {
    084                    this.context = context;
    085            }
    086
    087            public void setPriority(P priority) {
    088                    this.priority = priority;
    089            }
    090
    091            @Override
    092            public boolean consumes() {
    093                    return consumes;
    094            }
    095
    096            @Override
    097            public P getPriority() {
    098                    return priority;
    099            }
    100
    101            @Override
    102            public int getCardinality() {
    103                    return cardinality;
    104            }
    105
    106            @Override
    107            public void reset() {
    108                    // NOP, override if needed.
    109            }
    110
    111            @Override
    112            public C getContext() {
    113                    return context;
    114            }
    115
    116            @Override
    117            public String toString() {
    118                    return "AbstractEventHandler("+super.toString()+") [consumes=" + consumes + ", priority="
    119                                    + priority + ", cardinality=" + cardinality + ", context="
    120                                    + context + "]";
    121            }
    122
    123            @Override
    124            public boolean isOneOff() {
    125                    return oneOff;
    126            }
    127
    128            public void setOneOff(boolean oneOff) {
    129                    this.oneOff = oneOff;
    130            }
    131
    132            @Override
    133            public Mode getMode() {
    134                    return mode;
    135            }
    136
    137            public void setMode(Mode mode) {
    138                    this.mode = mode;
    139            }
    140
    141            @Override
    142            public Predicate<E, C> getPredicate() {
    143                    return predicate;
    144            }
    145
    146            public void setPredicate(Predicate<E, C> predicate) {
    147                    this.predicate = predicate;
    148            }
    149
    150    }