    001    package com.hammurapi.eventbus;
    002
    003    import java.util.Arrays;
    004
    005    import com.hammurapi.eventbus.AbstractEventBus.Handle;
    006    import com.hammurapi.extract.Predicate;
    007
    008
    009    /**
    010     * @author Pavel Vlasov
    011     *
    012     * @param <E>
    013     * @param <P>
    014     * @param <C>
    015     * @param <H>
    016     */
    017    public class PostCommand<E, P extends Comparable<P>, C, K, H extends EventBus.Handle<E,P,C>, S extends EventStore<E,P,C,H,S>> extends InferenceCommand<E,P,C,K,H,S> {
    018
    019            private Predicate<E, S>[] validators;
    020            private E event;
    021            private boolean directPost;
    022            private Handle<E,P,C,K> handle;
    023            private boolean isHandleMode;
    024
    025            public PostCommand(
    026                            E event,
    027                            boolean directPost,
    028                            K handlerId,
    029                            EventHandler<E, P, C, H, S> handler,
    030                            Handle<E,P,C,K>[] inputs,
    031                            InferenceContext<E,P,C,K,H,S> inferenceContext,
    032                            Predicate<E, S>[] validators) {
    033                    super(handlerId, handler,inputs,inferenceContext);
    034                    this.event = event;
    035                    this.validators = validators;
    036                    this.directPost = directPost;
    037            }
    038
    039            /**
    040             * For update
    041             * @param handle
    042             * @param directPost
    043             * @param handlerId
    044             * @param handler
    045             * @param inputs
    046             * @param inferenceContext
    047             * @param validators
    048             */
    049            public PostCommand(
    050                            Handle<E,P,C,K> handle,
    051                            K handlerId,
    052                            EventHandler<E, P, C, H, S> handler,
    053                            Handle<E,P,C,K>[] inputs,
    054                            InferenceContext<E,P,C,K,H,S> inferenceContext) {
    055                    super(handlerId, handler,inputs,inferenceContext);
    056                    this.handle = handle;
    057                    isHandleMode = true;
    058            }
    059
    060            public E getEvent() {
    061                    if (isHandleMode) {
    062                            throw new IllegalStateException("Invoke getHandle() instead");
    063                    }
    064                    return event;
    065            }
    066
    067            public Handle<E, P, C, K> getHandle() {
    068                    if (!isHandleMode) {
    069                            throw new IllegalStateException("Invoke getEvent() instead");
    070                    }
    071                    return handle;
    072            }
    073
    074            public boolean isHandleMode() {
    075                    return isHandleMode;
    076            }
    077
    078            public Predicate<E, S>[] getValidators() {
    079                    return validators;
    080            }
    081
    082            @Override
    083            public String toString() {
    084                    return "PostCommand [event=" + event
    085                                    + ", handler=" + getHandler() + ", inputs="
    086                                    + Arrays.toString(getInputs()) + ", validators="
    087                                    + Arrays.toString(validators) + "]";
    088            }
    089
    090            public boolean isDirectPost() {
    091                    return directPost;
    092            }
    093
    094    }
    095