1 | package com.hammurapi.eventbus; |
2 | |
3 | import java.util.Arrays; |
4 | |
5 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
6 | import com.hammurapi.extract.Predicate; |
7 | |
8 | |
9 | /** |
10 | * @author Pavel Vlasov |
11 | * |
12 | * @param <E> |
13 | * @param <P> |
14 | * @param <C> |
15 | * @param <H> |
16 | */ |
17 | 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> { |
18 | |
19 | private Predicate<E, S>[] validators; |
20 | private E event; |
21 | private boolean directPost; |
22 | private Handle<E,P,C,K> handle; |
23 | private boolean isHandleMode; |
24 | |
25 | public PostCommand( |
26 | E event, |
27 | boolean directPost, |
28 | K handlerId, |
29 | EventHandler<E, P, C, H, S> handler, |
30 | Handle<E,P,C,K>[] inputs, |
31 | InferenceContext<E,P,C,K,H,S> inferenceContext, |
32 | Predicate<E, S>[] validators) { |
33 | super(handlerId, handler,inputs,inferenceContext); |
34 | this.event = event; |
35 | this.validators = validators; |
36 | this.directPost = directPost; |
37 | } |
38 | |
39 | /** |
40 | * For update |
41 | * @param handle |
42 | * @param directPost |
43 | * @param handlerId |
44 | * @param handler |
45 | * @param inputs |
46 | * @param inferenceContext |
47 | * @param validators |
48 | */ |
49 | public PostCommand( |
50 | Handle<E,P,C,K> handle, |
51 | K handlerId, |
52 | EventHandler<E, P, C, H, S> handler, |
53 | Handle<E,P,C,K>[] inputs, |
54 | InferenceContext<E,P,C,K,H,S> inferenceContext) { |
55 | super(handlerId, handler,inputs,inferenceContext); |
56 | this.handle = handle; |
57 | isHandleMode = true; |
58 | } |
59 | |
60 | public E getEvent() { |
61 | if (isHandleMode) { |
62 | throw new IllegalStateException("Invoke getHandle() instead"); |
63 | } |
64 | return event; |
65 | } |
66 | |
67 | public Handle<E, P, C, K> getHandle() { |
68 | if (!isHandleMode) { |
69 | throw new IllegalStateException("Invoke getEvent() instead"); |
70 | } |
71 | return handle; |
72 | } |
73 | |
74 | public boolean isHandleMode() { |
75 | return isHandleMode; |
76 | } |
77 | |
78 | public Predicate<E, S>[] getValidators() { |
79 | return validators; |
80 | } |
81 | |
82 | @Override |
83 | public String toString() { |
84 | return "PostCommand [event=" + event |
85 | + ", handler=" + getHandler() + ", inputs=" |
86 | + Arrays.toString(getInputs()) + ", validators=" |
87 | + Arrays.toString(validators) + "]"; |
88 | } |
89 | |
90 | public boolean isDirectPost() { |
91 | return directPost; |
92 | } |
93 | |
94 | } |
95 | |