1 // ========================================================================
2 // Copyright (c) 2005-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.util.component;
15 import java.lang.ref.WeakReference;
16 import java.util.EventListener;
17 import java.util.concurrent.CopyOnWriteArrayList;
18
19 import org.eclipse.jetty.util.LazyList;
20 import org.eclipse.jetty.util.log.Log;
21
22 /* ------------------------------------------------------------ */
23 /** Container.
24 * This class allows a containment events to be generated from update methods.
25 *
26 * The style of usage is: <pre>
27 * public void setFoo(Foo foo)
28 * {
29 * getContainer().update(this,this.foo,foo,"foo");
30 * this.foo=foo;
31 * }
32 *
33 * public void setBars(Bar[] bars)
34 * {
35 * getContainer().update(this,this.bars,bars,"bar");
36 * this.bars=bars;
37 * }
38 * </pre>
39 */
40 public class Container
41 {
42 private final CopyOnWriteArrayList<Container.Listener> _listeners=new CopyOnWriteArrayList<Container.Listener>();
43
44 public void addEventListener(Container.Listener listener)
45 {
46 _listeners.add(listener);
47 }
48
49 public void removeEventListener(Container.Listener listener)
50 {
51 _listeners.remove(listener);
52 }
53
54 /* ------------------------------------------------------------ */
55 /** Update single parent to child relationship.
56 * @param parent The parent of the child.
57 * @param oldChild The previous value of the child. If this is non null and differs from <code>child</code>, then a remove event is generated.
58 * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated.
59 * @param relationship The name of the relationship
60 */
61 public void update(Object parent, Object oldChild, final Object child, String relationship)
62 {
63 if (oldChild!=null && !oldChild.equals(child))
64 remove(parent,oldChild,relationship);
65 if (child!=null && !child.equals(oldChild))
66 add(parent,child,relationship);
67 }
68
69 /* ------------------------------------------------------------ */
70 /** Update single parent to child relationship.
71 * @param parent The parent of the child.
72 * @param oldChild The previous value of the child. If this is non null and differs from <code>child</code>, then a remove event is generated.
73 * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated.
74 * @param relationship The name of the relationship
75 * @param addRemove If true add/remove is called for the new/old children as well as the relationships
76 */
77 public void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove)
78 {
79 if (oldChild!=null && !oldChild.equals(child))
80 {
81 remove(parent,oldChild,relationship);
82 if (addRemove)
83 removeBean(oldChild);
84 }
85
86 if (child!=null && !child.equals(oldChild))
87 {
88 if (addRemove)
89 addBean(child);
90 add(parent,child,relationship);
91 }
92 }
93
94 /* ------------------------------------------------------------ */
95 /** Update multiple parent to child relationship.
96 * @param parent The parent of the child.
97 * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the <code>children</code> array.
98 * This array is modified and children that remain in the new children array are nulled out of the old children array.
99 * @param children The current array of children. An add event is generated for any child in this array but not in the <code>oldChildren</code> array.
100 * @param relationship The name of the relationship
101 */
102 public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship)
103 {
104 update(parent,oldChildren,children,relationship,false);
105 }
106
107 /* ------------------------------------------------------------ */
108 /** Update multiple parent to child relationship.
109 * @param parent The parent of the child.
110 * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the <code>children</code> array.
111 * This array is modified and children that remain in the new children array are nulled out of the old children array.
112 * @param children The current array of children. An add event is generated for any child in this array but not in the <code>oldChildren</code> array.
113 * @param relationship The name of the relationship
114 * @param addRemove If true add/remove is called for the new/old children as well as the relationships
115 */
116 public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove)
117 {
118 Object[] newChildren = null;
119 if (children!=null)
120 {
121 newChildren = new Object[children.length];
122
123 for (int i=children.length;i-->0;)
124 {
125 boolean new_child=true;
126 if (oldChildren!=null)
127 {
128 for (int j=oldChildren.length;j-->0;)
129 {
130 if (children[i]!=null && children[i].equals(oldChildren[j]))
131 {
132 oldChildren[j]=null;
133 new_child=false;
134 }
135 }
136 }
137 if (new_child)
138 newChildren[i]=children[i];
139 }
140 }
141
142 if (oldChildren!=null)
143 {
144 for (int i=oldChildren.length;i-->0;)
145 {
146 if (oldChildren[i]!=null)
147 {
148 remove(parent,oldChildren[i],relationship);
149 if (addRemove)
150 removeBean(oldChildren[i]);
151 }
152 }
153 }
154
155 if (newChildren!=null)
156 {
157 for (int i=0;i<newChildren.length;i++)
158 if (newChildren[i]!=null)
159 {
160 if (addRemove)
161 addBean(newChildren[i]);
162 add(parent,newChildren[i],relationship);
163 }
164 }
165 }
166
167 /* ------------------------------------------------------------ */
168 public void addBean(Object obj)
169 {
170 if (_listeners!=null)
171 {
172 for (int i=0; i<LazyList.size(_listeners); i++)
173 {
174 Listener listener=(Listener)LazyList.get(_listeners, i);
175 listener.addBean(obj);
176 }
177 }
178 }
179
180 /* ------------------------------------------------------------ */
181 public void removeBean(Object obj)
182 {
183 if (_listeners!=null)
184 {
185 for (int i=0; i<LazyList.size(_listeners); i++)
186 ((Listener)LazyList.get(_listeners, i)).removeBean(obj);
187 }
188 }
189
190 /* ------------------------------------------------------------ */
191 /** Add a parent child relationship
192 * @param parent
193 * @param child
194 * @param relationship
195 */
196 private void add(Object parent, Object child, String relationship)
197 {
198 if (Log.isDebugEnabled())
199 Log.debug("Container "+parent+" + "+child+" as "+relationship);
200 if (_listeners!=null)
201 {
202 Relationship event=new Relationship(this,parent,child,relationship);
203 for (int i=0; i<LazyList.size(_listeners); i++)
204 ((Listener)LazyList.get(_listeners, i)).add(event);
205 }
206 }
207
208 /* ------------------------------------------------------------ */
209 /** remove a parent child relationship
210 * @param parent
211 * @param child
212 * @param relationship
213 */
214 private void remove(Object parent, Object child, String relationship)
215 {
216 if (Log.isDebugEnabled())
217 Log.debug("Container "+parent+" - "+child+" as "+relationship);
218 if (_listeners!=null)
219 {
220 Relationship event=new Relationship(this,parent,child,relationship);
221 for (int i=0; i<LazyList.size(_listeners); i++)
222 ((Listener)LazyList.get(_listeners, i)).remove(event);
223 }
224 }
225
226 /* ------------------------------------------------------------ */
227 /** A Container event.
228 * @see Listener
229 */
230 public static class Relationship
231 {
232 private final WeakReference<Object> _parent;
233 private final WeakReference<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=new WeakReference<Object>(parent);
241 _child=new WeakReference<Object>(child);
242 _relationship=relationship;
243 }
244
245 public Container getContainer()
246 {
247 return _container;
248 }
249
250 public Object getChild()
251 {
252 return _child.get();
253 }
254
255 public Object getParent()
256 {
257 return _parent.get();
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.get()==_parent.get() && r._child.get()==_child.get() && r._relationship.equals(_relationship);
284 }
285 }
286
287 /* ------------------------------------------------------------ */
288 /** Listener.
289 * A listener for Container events.
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 }