Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.component;
 
 
 /* ------------------------------------------------------------ */
Container. This class allows a containment events to be generated from update methods. The style of usage is:
   public void setFoo(Foo foo)
   {
       getContainer().update(this,this.foo,foo,"foo");
       this.foo=foo;
   }
   
   public void setBars(Bar[] bars)
   {
       getContainer().update(this,this.bars,bars,"bar");
       this.bars=bars;
   }
 
 
 public class Container
 {
     private static final Logger LOG = Log.getLogger(Container.class);
     
     public void addEventListener(Container.Listener listener)
     {
         .add(listener);
     }
     
     public void removeEventListener(Container.Listener listener)
     {
         .remove(listener);
     }
     
     /* ------------------------------------------------------------ */
    
Update single parent to child relationship.

Parameters:
parent The parent of the child.
oldChild The previous value of the child. If this is non null and differs from child, then a remove event is generated.
child The current child. If this is non null and differs from oldChild, then an add event is generated.
relationship The name of the relationship
 
     public void update(Object parentObject oldChildfinal Object childString relationship)
     {
         if (oldChild!=null && !oldChild.equals(child))
             remove(parent,oldChild,relationship);
         if (child!=null && !child.equals(oldChild))
             add(parent,child,relationship);
     }
     
     /* ------------------------------------------------------------ */
    
Update single parent to child relationship.

Parameters:
parent The parent of the child.
oldChild The previous value of the child. If this is non null and differs from child, then a remove event is generated.
child The current child. If this is non null and differs from oldChild, then an add event is generated.
relationship The name of the relationship
addRemove If true add/remove is called for the new/old children as well as the relationships
 
     public void update(Object parentObject oldChildfinal Object childString relationship,boolean addRemove)
     {
         if (oldChild!=null && !oldChild.equals(child))
         {
             remove(parent,oldChild,relationship);
             if (addRemove)
                 removeBean(oldChild);
         }
         
         if (child!=null && !child.equals(oldChild))
         {
             if (addRemove)
                 addBean(child);
             add(parent,child,relationship);
         }
     }
    /* ------------------------------------------------------------ */
    
Update multiple parent to child relationship.

Parameters:
parent The parent of the child.
oldChildren The previous array of children. A remove event is generated for any child in this array but not in the children array. This array is modified and children that remain in the new children array are nulled out of the old children array.
children The current array of children. An add event is generated for any child in this array but not in the oldChildren array.
relationship The name of the relationship
    public void update(Object parentObject[] oldChildrenfinal Object[] childrenString relationship)
    {
        update(parent,oldChildren,children,relationship,false);
    }
    
    /* ------------------------------------------------------------ */
    
Update multiple parent to child relationship.

Parameters:
parent The parent of the child.
oldChildren The previous array of children. A remove event is generated for any child in this array but not in the children array. This array is modified and children that remain in the new children array are nulled out of the old children array.
children The current array of children. An add event is generated for any child in this array but not in the oldChildren array.
relationship The name of the relationship
addRemove If true add/remove is called for the new/old children as well as the relationships
    public void update(Object parentObject[] oldChildrenfinal Object[] childrenString relationshipboolean addRemove)
    {
        Object[] newChildren = null;
        if (children!=null)
        {
            newChildren = new Object[children.length];
        
            for (int i=children.length;i-->0;)
            {
                boolean new_child=true;
                if (oldChildren!=null)
                {
                    for (int j=oldChildren.length;j-->0;)
                    {
                        if (children[i]!=null && children[i].equals(oldChildren[j]))
                        {
                            oldChildren[j]=null;
                            new_child=false;
                        }
                    }
                }
                if (new_child)
                    newChildren[i]=children[i];
            }
        }
        
        if (oldChildren!=null)
        {
            for (int i=oldChildren.length;i-->0;)
            {
                if (oldChildren[i]!=null)
                {
                    remove(parent,oldChildren[i],relationship);
                    if (addRemove)
                        removeBean(oldChildren[i]);
                }
            }
        }
        
        if (newChildren!=null)
        {
            for (int i=0;i<newChildren.length;i++)
                if (newChildren[i]!=null)
                {
                    if (addRemove)
                        addBean(newChildren[i]);
                    add(parent,newChildren[i],relationship);
                }
        }
    }
    /* ------------------------------------------------------------ */
    public void addBean(Object obj)
    {
        if (!=null)
        {
            for (int i=0; i<LazyList.size(); i++)
            {
                Listener listener=(Listener)LazyList.get(i);
                listener.addBean(obj);
            }
        }
    }
    /* ------------------------------------------------------------ */
    public void removeBean(Object obj)
    {
        if (!=null)
        {
            for (int i=0; i<LazyList.size(); i++)
                ((Listener)LazyList.get(i)).removeBean(obj);
        }
    }
    
    /* ------------------------------------------------------------ */
    
Add a parent child relationship

Parameters:
parent
child
relationship
    private void add(Object parentObject childString relationship)
    {
        if (.isDebugEnabled())
            .debug("Container "+parent+" + "+child+" as "+relationship);
        if (!=null)
        {
            Relationship event=new Relationship(this,parent,child,relationship);
            for (int i=0; i<LazyList.size(); i++)
                ((Listener)LazyList.get(i)).add(event);
        }
    }
    
    /* ------------------------------------------------------------ */
    
remove a parent child relationship

Parameters:
parent
child
relationship
    private void remove(Object parentObject childString relationship)
    {
        if (.isDebugEnabled())
            .debug("Container "+parent+" - "+child+" as "+relationship);
        if (!=null)
        {
            Relationship event=new Relationship(this,parent,child,relationship);
            for (int i=0; i<LazyList.size(); i++)
                ((Listener)LazyList.get(i)).remove(event);
        }
    }
    
    /* ------------------------------------------------------------ */
    
A Container event.

    public static class Relationship
    {
        private final WeakReference<Object_parent;
        private final WeakReference<Object_child;
        private String _relationship;
        private Container _container;
        
        private Relationship(Container containerObject parent,Object childString relationship)
        {
            =container;
            =new WeakReference<Object>(parent);
            =new WeakReference<Object>(child);
            =relationship;
        }
        
        public Container getContainer()
        {
            return ;
        }
        
        public Object getChild()
        {
            return .get();
        }
        
        public Object getParent()
        {
            return .get();
        }
        
        public String getRelationship()
        {
            return ;
        }
        
        @Override
        public String toString()
        {
            return +"---"++"-->"+;
        }
        
        @Override
        public int hashCode()
        {
            return .hashCode()+.hashCode()+.hashCode();
        }
        
        @Override
        public boolean equals(Object o)
        {
            if (o==null || !(o instanceof Relationship))
                return false;
            Relationship r = (Relationship)o;
            return r._parent.get()==.get() && r._child.get()==.get() && r._relationship.equals();
        }
    }
    
    /* ------------------------------------------------------------ */
    
Listener. A listener for Container events.
    public interface Listener extends EventListener
    {
        public void addBean(Object bean);
        public void removeBean(Object bean);
        public void add(Container.Relationship relationship);
        public void remove(Container.Relationship relationship);
    }
New to GrepCode? Check out our FAQ X