1 | package com.hammurapi.eventbus.local; |
2 | |
3 | import java.lang.ref.Reference; |
4 | import java.lang.ref.SoftReference; |
5 | import java.lang.ref.WeakReference; |
6 | import java.util.Collection; |
7 | import java.util.concurrent.ConcurrentLinkedQueue; |
8 | |
9 | import com.hammurapi.eventbus.AbstractEventBus; |
10 | import com.hammurapi.eventbus.AbstractEventBus.Handle; |
11 | import com.hammurapi.eventbus.Derivation; |
12 | import com.hammurapi.eventbus.EventHandler; |
13 | import com.hammurapi.eventbus.EventStore; |
14 | import com.hammurapi.eventbus.InferenceContext; |
15 | import com.hammurapi.eventbus.local.LocalEventBusBase.LocalHandle; |
16 | import com.hammurapi.store.Store; |
17 | import com.hammurapi.store.local.LocalHandle.HandleStrength; |
18 | |
19 | /** |
20 | * Facade handles are used by event joiners and derivations. When master handle is invalidated, it sends notifications to facades and they get invalidated. |
21 | * @author Pavel Vlasov |
22 | * |
23 | * @param <E> |
24 | * @param <P> |
25 | * @param <C> |
26 | * @param <S> |
27 | */ |
28 | class FacadeHandle<E, P extends Comparable<P>, C, S extends EventStore<E, P, C, AbstractEventBus.Handle<E, P, C, Long>, S>> implements LocalHandle<E,P,C,S> { |
29 | private MasterHandle<E,P,C,S> master; |
30 | private E event; // Never read - just to create strong reference. |
31 | private Reference<E> eventRef; |
32 | private Collection<Derivation<E,P,C>> derivatives = new ConcurrentLinkedQueue<Derivation<E,P,C>>(); |
33 | private long masterId; |
34 | |
35 | public FacadeHandle(MasterHandle<E,P,C,S> master, HandleStrength handleStrength) { |
36 | this.master = master; |
37 | this.masterId = master.getId(); |
38 | master.addFacade(this); |
39 | switch (handleStrength) { |
40 | case STRONG: |
41 | event = master.getEvent(); |
42 | break; |
43 | case SOFT: |
44 | eventRef = new SoftReference<E>(master.getEvent()); |
45 | break; |
46 | case WEAK: |
47 | eventRef = new WeakReference<E>(master.getEvent()); |
48 | break; |
49 | } |
50 | } |
51 | |
52 | void addDerivative(Derivation<E,P,C> derivative) { |
53 | derivatives.add(derivative); |
54 | } |
55 | |
56 | public Long getId() { |
57 | return masterId; |
58 | } |
59 | |
60 | public Collection<Derivation<E,P,C>> getDerivations() { |
61 | return master.getDerivations(); |
62 | } |
63 | |
64 | public E getEvent() { |
65 | return master.getEvent(); |
66 | } |
67 | |
68 | @Override |
69 | public void remove() { |
70 | if (master!=null) { |
71 | master.remove(); |
72 | } |
73 | } |
74 | |
75 | public void remove(boolean forUpdate, InferenceContext<E,P,C,Long,AbstractEventBus.Handle<E,P,C,Long>,S> inferenceContext) { |
76 | if (master!=null) { |
77 | master.remove(forUpdate, inferenceContext); |
78 | } |
79 | } |
80 | |
81 | public boolean isDerivedFrom(E event) { |
82 | return master.isDerivedFrom(event); |
83 | } |
84 | |
85 | public boolean isValid() { |
86 | return master!=null && master.isValid(); |
87 | } |
88 | |
89 | // public void setEvent(E event) { |
90 | // master.setEvent(event); |
91 | // } |
92 | |
93 | public void update() { |
94 | master.update(); |
95 | } |
96 | |
97 | public MasterHandle<E,P,C,S> getMaster() { |
98 | return master; |
99 | } |
100 | |
101 | /** |
102 | * Invoked by master. |
103 | */ |
104 | void invalidateFacade(InferenceContext<E,P,C,Long,AbstractEventBus.Handle<E,P,C,Long>,S> inferenceContext) { |
105 | event = null; |
106 | if (eventRef!=null) { |
107 | eventRef.clear(); |
108 | eventRef = null; |
109 | } |
110 | master = null; |
111 | |
112 | for (Derivation<E,P,C> derivative: derivatives) { |
113 | derivative.invalidate(inferenceContext); |
114 | } |
115 | derivatives.clear(); |
116 | } |
117 | |
118 | @Override |
119 | public void join() throws InterruptedException { |
120 | master.join(); |
121 | } |
122 | |
123 | @Override |
124 | public boolean join(long timeout) throws InterruptedException { |
125 | return master.join(timeout); |
126 | } |
127 | |
128 | @Override |
129 | public <H extends AbstractEventBus.Handle<E,P,C,Long>, S extends EventStore<E, P, C, H, S>> void setStoreHandle(Store.Handle<H, E, S> storeHandle) { |
130 | throw new UnsupportedOperationException("This operation shall not be invoked for FacadeHandle"); |
131 | } |
132 | |
133 | @Override |
134 | public String toString() { |
135 | return "FacadeHandle [getId()="+getId()+", master=" + master + "]"; |
136 | } |
137 | |
138 | @Override |
139 | public void handleException(Exception e) { |
140 | master.handleException(e); |
141 | } |
142 | |
143 | @Override |
144 | public boolean isDirectPost() { |
145 | return master.isDirectPost(); |
146 | } |
147 | |
148 | @Override |
149 | public void setDirectPost() { |
150 | master.setDirectPost(); |
151 | } |
152 | |
153 | @Override |
154 | public void addDerivation(Long handlerId, EventHandler<E, P, C, ?, ?> handler, Handle<E, P, C, Long>[] inputs) { |
155 | master.addDerivation(handlerId, handler, inputs); |
156 | |
157 | } |
158 | |
159 | @Override |
160 | public void addRemoveListener(RemoveListener<E, P, C, S> removeListener) { |
161 | master.addRemoveListener(removeListener); |
162 | } |
163 | |
164 | } |
165 | |