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