View Javadoc

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  import java.util.concurrent.CopyOnWriteArrayList;
17  
18  import org.eclipse.jetty.util.LazyList;
19  import org.eclipse.jetty.util.log.Log;
20  
21  /* ------------------------------------------------------------ */
22  /** Container.
23   * This class allows a containment events to be generated from update methods.
24   * 
25   * The style of usage is: <pre>
26   *   public void setFoo(Foo foo)
27   *   {
28   *       getContainer().update(this,this.foo,foo,"foo");
29   *       this.foo=foo;
30   *   }
31   *   
32   *   public void setBars(Bar[] bars)
33   *   {
34   *       getContainer().update(this,this.bars,bars,"bar");
35   *       this.bars=bars;
36   *   }
37   * </pre>
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      /** Update single parent to child relationship.
55       * @param parent The parent of the child.
56       * @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.
57       * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated.
58       * @param relationship The name of the relationship
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      /** Update single parent to child relationship.
70       * @param parent The parent of the child.
71       * @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.
72       * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated.
73       * @param relationship The name of the relationship
74       * @param addRemove If true add/remove is called for the new/old children as well as the relationships
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      /** Update multiple parent to child relationship.
95       * @param parent The parent of the child.
96       * @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.
97       * This array is modified and children that remain in the new children array are nulled out of the old children array.
98       * @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.
99       * @param relationship The name of the relationship
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     /** Update multiple parent to child relationship.
108      * @param parent The parent of the child.
109      * @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.
110      * This array is modified and children that remain in the new children array are nulled out of the old children array.
111      * @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.
112      * @param relationship The name of the relationship
113      * @param addRemove If true add/remove is called for the new/old children as well as the relationships
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     /** Add a parent child relationship
191      * @param parent
192      * @param child
193      * @param relationship
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     /** remove a parent child relationship
209      * @param parent
210      * @param child
211      * @param relationship
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     /** A Container event.
227      * @see Listener
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     /** 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 }