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