1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.util.component;
15 import java.util.EventListener;
16 import java.util.concurrent.CopyOnWriteArrayList;
17
18 import org.eclipse.jetty.util.LazyList;
19 import org.eclipse.jetty.util.log.Log;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class Container
40 {
41 private final CopyOnWriteArrayList<Container.Listener> _listeners=new CopyOnWriteArrayList<Container.Listener>();
42
43 public void addEventListener(Container.Listener listener)
44 {
45 _listeners.add(listener);
46 }
47
48 public void removeEventListener(Container.Listener listener)
49 {
50 _listeners.remove(listener);
51 }
52
53
54
55
56
57
58
59
60 public void update(Object parent, Object oldChild, final Object child, String relationship)
61 {
62 if (oldChild!=null && !oldChild.equals(child))
63 remove(parent,oldChild,relationship);
64 if (child!=null && !child.equals(oldChild))
65 add(parent,child,relationship);
66 }
67
68
69
70
71
72
73
74
75
76 public void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove)
77 {
78 if (oldChild!=null && !oldChild.equals(child))
79 {
80 remove(parent,oldChild,relationship);
81 if (addRemove)
82 removeBean(oldChild);
83 }
84
85 if (child!=null && !child.equals(oldChild))
86 {
87 if (addRemove)
88 addBean(child);
89 add(parent,child,relationship);
90 }
91 }
92
93
94
95
96
97
98
99
100
101 public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship)
102 {
103 update(parent,oldChildren,children,relationship,false);
104 }
105
106
107
108
109
110
111
112
113
114
115 public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove)
116 {
117 Object[] newChildren = null;
118 if (children!=null)
119 {
120 newChildren = new Object[children.length];
121
122 for (int i=children.length;i-->0;)
123 {
124 boolean new_child=true;
125 if (oldChildren!=null)
126 {
127 for (int j=oldChildren.length;j-->0;)
128 {
129 if (children[i]!=null && children[i].equals(oldChildren[j]))
130 {
131 oldChildren[j]=null;
132 new_child=false;
133 }
134 }
135 }
136 if (new_child)
137 newChildren[i]=children[i];
138 }
139 }
140
141 if (oldChildren!=null)
142 {
143 for (int i=oldChildren.length;i-->0;)
144 {
145 if (oldChildren[i]!=null)
146 {
147 remove(parent,oldChildren[i],relationship);
148 if (addRemove)
149 removeBean(oldChildren[i]);
150 }
151 }
152 }
153
154 if (newChildren!=null)
155 {
156 for (int i=0;i<newChildren.length;i++)
157 if (newChildren[i]!=null)
158 {
159 if (addRemove)
160 addBean(newChildren[i]);
161 add(parent,newChildren[i],relationship);
162 }
163 }
164 }
165
166
167 public void addBean(Object obj)
168 {
169 if (_listeners!=null)
170 {
171 for (int i=0; i<LazyList.size(_listeners); i++)
172 {
173 Listener listener=(Listener)LazyList.get(_listeners, i);
174 listener.addBean(obj);
175 }
176 }
177 }
178
179
180 public void removeBean(Object obj)
181 {
182 if (_listeners!=null)
183 {
184 for (int i=0; i<LazyList.size(_listeners); i++)
185 ((Listener)LazyList.get(_listeners, i)).removeBean(obj);
186 }
187 }
188
189
190
191
192
193
194
195 private void add(Object parent, Object child, String relationship)
196 {
197 if (Log.isDebugEnabled())
198 Log.debug("Container "+parent+" + "+child+" as "+relationship);
199 if (_listeners!=null)
200 {
201 Relationship event=new Relationship(this,parent,child,relationship);
202 for (int i=0; i<LazyList.size(_listeners); i++)
203 ((Listener)LazyList.get(_listeners, i)).add(event);
204 }
205 }
206
207
208
209
210
211
212
213 private void remove(Object parent, Object child, String relationship)
214 {
215 if (Log.isDebugEnabled())
216 Log.debug("Container "+parent+" - "+child+" as "+relationship);
217 if (_listeners!=null)
218 {
219 Relationship event=new Relationship(this,parent,child,relationship);
220 for (int i=0; i<LazyList.size(_listeners); i++)
221 ((Listener)LazyList.get(_listeners, i)).remove(event);
222 }
223 }
224
225
226
227
228
229
230 public static class Relationship
231 {
232 private Object _parent;
233 private Object _child;
234 private String _relationship;
235 private Container _container;
236
237 private Relationship(Container container, Object parent,Object child, String relationship)
238 {
239 _container=container;
240 _parent=parent;
241 _child=child;
242 _relationship=relationship;
243 }
244
245 public Container getContainer()
246 {
247 return _container;
248 }
249
250 public Object getChild()
251 {
252 return _child;
253 }
254
255 public Object getParent()
256 {
257 return _parent;
258 }
259
260 public String getRelationship()
261 {
262 return _relationship;
263 }
264
265 @Override
266 public String toString()
267 {
268 return _parent+"---"+_relationship+"-->"+_child;
269 }
270
271 @Override
272 public int hashCode()
273 {
274 return _parent.hashCode()+_child.hashCode()+_relationship.hashCode();
275 }
276
277 @Override
278 public boolean equals(Object o)
279 {
280 if (o==null || !(o instanceof Relationship))
281 return false;
282 Relationship r = (Relationship)o;
283 return r._parent==_parent && r._child==_child && r._relationship.equals(_relationship);
284 }
285 }
286
287
288
289
290
291 public interface Listener extends EventListener
292 {
293 public void addBean(Object bean);
294 public void removeBean(Object bean);
295 public void add(Container.Relationship relationship);
296 public void remove(Container.Relationship relationship);
297 }
298 }