org.oddjob.structural
Class ChildHelper<E>

java.lang.Object
  extended by org.oddjob.structural.ChildHelper<E>
All Implemented Interfaces:
Iterable<E>, Structural, ChildList<E>

public class ChildHelper<E>
extends Object
implements Structural, Iterable<E>, ChildList<E>

Helper for managing child Objects. This class will track structural changes and notify listeners.

Author:
Rob Gordon

Constructor Summary
ChildHelper(Structural source)
          Constructor.
 
Method Summary
 int addChild(E child)
          Add a child to the end of the list.
 void addStructuralListener(StructuralListener listener)
          Add a listener.
 boolean contains(E child)
          Is this child ours?
 E getChild()
          Return an only child.
 E getChildAt(int index)
          Return a child.
 Object[] getChildren()
          Return an array of children.
 E[] getChildren(E[] array)
          Return an array of children.
static Object[] getChildren(Structural structural)
           
 void hardResetChildren()
          Perform a hard reset.
 void insertChild(int index, E child)
          Insert a child.
 boolean isNoListeners()
          Returns true if there are no listeners listening for StructuralEvents.
 Iterator<E> iterator()
           
 void removeAllChildren()
          Allows a sub class to remove all children from itself.
 int removeChild(Object child)
          Remove a child.
 E removeChildAt(int index)
          Remove a child by index.
 void removeStructuralListener(StructuralListener listener)
          Remove a listener.
 int size()
          The number of children.
 void softResetChildren()
          Perform a soft reset.
 void stopChildren()
          Stops all the child jobs.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ChildHelper

public ChildHelper(Structural source)
Constructor.

Parameters:
source - The source used as the source of the event.
Method Detail

insertChild

public void insertChild(int index,
                        E child)
Insert a child.

Specified by:
insertChild in interface ChildList<E>
Parameters:
index - The index.
child - The child.

addChild

public int addChild(E child)
Add a child to the end of the list.

Specified by:
addChild in interface ChildList<E>
Parameters:
child - The child. Must not be null.
Returns:
The index the child was added at.

removeChildAt

public E removeChildAt(int index)
                throws IndexOutOfBoundsException
Remove a child by index. This method fires the appropriate event in accordance with the Strucutral interface.

Specified by:
removeChildAt in interface ChildList<E>
Parameters:
index - The index of the child to remove.
Returns:
The child removed.
Throws:
IndexOutOfBoundsException - If there is no child at the index.

removeChild

public int removeChild(Object child)
                throws IllegalStateException
Remove a child.

Specified by:
removeChild in interface ChildList<E>
Parameters:
child - The child to be removed.
Returns:
The index the child was removed from.
Throws:
IllegalStateException - If the child is not our child.

removeAllChildren

public void removeAllChildren()
Allows a sub class to remove all children from itself. This method fires the appropriate events in accordance with the structural interface.

This method isn't synchronized. Simultaneous removal of children by a different thread could result in an IndexOutOfBoundsException.


stopChildren

public void stopChildren()
                  throws FailedToStopException
Stops all the child jobs. Jobs are stopped in reverse order.

Throws:
FailedToStopException

softResetChildren

public void softResetChildren()
Perform a soft reset. This method propagates the soft reset message down to all child jobs. This is a convenience method that a sub class can choose to use.


hardResetChildren

public void hardResetChildren()
Perform a hard reset. This method propergates the hard reset message down to all child jobs. This is a convenience method a sub class can choose to use.


getChildren

public Object[] getChildren()
Return an array of children.

Returns:
An array of child objects.

getChildren

public E[] getChildren(E[] array)
Return an array of children.

Returns:
An array of child objects.

getChildAt

public E getChildAt(int index)
Return a child.

Returns:
A child.

getChild

public E getChild()
Return an only child.

Returns:
A child.

contains

public boolean contains(E child)
Is this child ours?

Parameters:
child -
Returns:
true if it is, false if it isn't.

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>

addStructuralListener

public void addStructuralListener(StructuralListener listener)
Description copied from interface: Structural
Add a listener. The listener will immediately receive add notifications for all existing children.

Specified by:
addStructuralListener in interface Structural
Parameters:
listener - The listener.

removeStructuralListener

public void removeStructuralListener(StructuralListener listener)
Description copied from interface: Structural
Remove a listener.

Specified by:
removeStructuralListener in interface Structural
Parameters:
listener - The listener.

isNoListeners

public boolean isNoListeners()
Returns true if there are no listeners listening for StructuralEvents.

Returns:
true/false.

size

public int size()
The number of children.

Returns:
The number of children.

getChildren

public static Object[] getChildren(Structural structural)

toString

public String toString()
Overrides:
toString in class Object