FileDocCategorySizeDatePackage
PersistenceElementCollection.javaAPI DocGlassfish v2 API8105Fri May 04 22:34:44 BST 2007com.sun.jdo.api.persistence.model.jdo.impl

PersistenceElementCollection

public class PersistenceElementCollection extends Object
author
raccah
version
%I%

Fields Summary
private PersistenceElementImpl
_owner
Owner of the collection.
private PersistenceElement[]
_elements
Elements of the collection.
private Object[]
_template
Array template for typed returns
private String
_propertyName
Property name.
Constructors Summary
public PersistenceElementCollection()
Create new PersistenceElementCollection with no owner, property, or template. This constructor should only be used for cloning and archiving.

		this(null, null, null);
	
public PersistenceElementCollection(PersistenceElementImpl owner, String propertyName, Object[] template)
Creates new PersistenceElementCollection

		_owner = owner;
		_propertyName = propertyName;
		_template = template;
	
Methods Summary
public voidchangeElements(PersistenceElement[] elements, int action)
Change the set of elements.

param
elements the new elements
param
action {@link PersistenceElement.Impl#ADD}, {@link PersistenceElement.Impl#REMOVE}, or {@link PersistenceElement.Impl#SET}
exception
ModelException if impossible

		changeElements(Arrays.asList(elements), action);
	
public voidchangeElements(java.util.List elements, int action)
Change the set of elements.

param
elements the new elements
param
action {@link PersistenceElement.Impl#ADD}, {@link PersistenceElement.Impl#REMOVE}, or {@link PersistenceElement.Impl#SET}
exception
ModelException if impossible

		boolean changed = false;

		try
		{
			PersistenceElement[] oldElements = getElements();
			int oldLength = (oldElements == null) ? 0 : oldElements.length;
			int newLength = (elements == null) ? 0 : elements.size();
			List list = null;

			switch (action)
			{
				case PersistenceElement.Impl.SET:
					list = elements;
					changed = true;
					break;
				case PersistenceElement.Impl.ADD:
					if (newLength > 0)
					{
						list = ((oldLength == 0) ? new ArrayList() : 
							new ArrayList(Arrays.asList(oldElements)));
						list.addAll(elements);
						changed = true;
					}
					break;
				case PersistenceElement.Impl.REMOVE:
					if ((newLength > 0) && (oldLength > 0))
					{
						list = new ArrayList(Arrays.asList(oldElements));
						list.removeAll(elements);
						changed = true;
					}
					break;
			}
			if (changed)
			{
				try
				{
					_owner.fireVetoableChange(_propertyName, null, null);
					_elements = (PersistenceElement[])list.toArray(_template);
				}
				catch (PropertyVetoException e)
				{
					throw new ModelVetoException(e);
				}
			}
		}
		finally
		{
			if (changed)
				_owner.firePropertyChange(_propertyName, null, null);
		}
	
public PersistenceElementgetElement(java.lang.String name)
Returns the element with the supplied name from the collection of elements maintained by this collection.

param
name the name to match
return
the element with the supplied name, null if none exists

		PersistenceElement[] elements = getElements();
		int i, count = ((elements != null) ? elements.length : 0);
		
		for (i = 0; i < count; i++)
		{
			PersistenceElement element = elements[i];

			if (name.equals(element.getName()))
				return element;
		}

		return null;
	
public PersistenceElement[]getElements()
Returns the collection of elements maintained by this holder in the form of an array.

return
the elements maintained by this collection

 return _elements; 
public PersistenceElementImplgetOwner()
Returns the owner of this collection. This method should only be used internally and for cloning and archiving.

return
the owner of this collection

 return _owner; 
public java.lang.StringgetPropertyName()
Returns the property name of this collection. This method should only be used internally and for cloning and archiving.

return
the property name for this collection

 return _propertyName; 
public java.lang.Object[]getTemplate()
Returns the template for the array of this collection. This method should only be used internally and for cloning and archiving.

return
the typed template of this collection

 return _template; 
public voidsetElements(PersistenceElement[] elements)
Set the collection of elements maintained by this holder to the supplied array. This method should only be used internally and for cloning and archiving.

param
elements the collection of elements maintained by this holder

		_elements = elements;
	
public voidsetOwner(PersistenceElementImpl owner)
Set the owner of this collection to the supplied implementation. This method should only be used internally and for cloning and archiving.

param
owner the owner of this collection

		_owner = owner;
	
public voidsetPropertyName(java.lang.String propertyName)
Set the property name of this collection to the supplied name. This name is used to generate the correct property change event on changes to the collection. This method should only be used internally and for cloning and archiving.

param
propertyName the property name for this collection

		_propertyName = propertyName;
	
public voidsetTemplate(java.lang.Object[] template)
Set the template for the array of this collection to the supplied array. This template is used so the array returned by getElements is properly typed. This method should only be used internally and for cloning and archiving.

param
template the typed template of this collection

 _template = template;