FileDocCategorySizeDatePackage
HashSet.javaAPI DocGlassfish v2 API28139Fri May 04 22:35:12 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.sco

HashSet

public class HashSet extends HashSet implements com.sun.jdo.spi.persistence.support.sqlstore.SCOCollection
A mutable 2nd class object date.
author
Marina Vatkina
version
1.0
see
java.util.HashSet

Fields Summary
private transient com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable
owner
private transient String
fieldName
private transient Class
elementType
private transient boolean
allowNulls
private transient HashSet
added
private transient HashSet
removed
private transient boolean
isDeferred
private static final ResourceBundle
messages
I18N message handlers
private static final ResourceBundle
messages1
Constructors Summary
public HashSet(Object owner, String fieldName, Class elementType, boolean allowNulls)
Creates a new empty HashSet object. Assigns owning object and field name.

param
owner the owning object
param
fieldName the owning field name
param
elementType the element types allowed
param
allowNulls true if nulls are allowed



                                             
            
    
        super();
    if (owner instanceof PersistenceCapable)
        {
                this.owner = (PersistenceCapable)owner;
            this.fieldName = fieldName;
        }
    this.elementType = elementType;
        this.allowNulls = allowNulls;
    
public HashSet(Object owner, String fieldName, Class elementType, boolean allowNulls, int initialCapacity)
Creates a new empty HashSet object that has the specified initial capacity.Assigns owning object and field name.

param
owner the owning object
param
fieldName the owning field name
param
elementType the element types allowed
param
allowNulls true if nulls are allowed
param
initialCapacity the initial capacity of the hash map.
throws
IllegalArgumentException if the initial capacity is less than zero.
see
java.util.HashSet

    super(initialCapacity);
    if (owner instanceof PersistenceCapable)
        {
                this.owner = (PersistenceCapable)owner;
            this.fieldName = fieldName;
        }
    this.elementType = elementType;
        this.allowNulls = allowNulls;
    
Methods Summary
public booleanadd(java.lang.Object o)
Adds the specified element to this set if it is not already present.

param
o element to be added to this set.
return
true if the set did not already contain the specified element.
see
java.util.HashSet

        if (allowNulls == false && o == null)
        {
            throw new JDOUserException(I18NHelper.getMessage(messages,
                        "sco.nulls_not_allowed")); // NOI18N
        }

        if (elementType != null && !elementType.isAssignableFrom(o.getClass()))
        {
            throw new JDOUserException(I18NHelper.getMessage(messages,
                                     "sco.classcastexception", elementType.getName()), // NOI18N
                                       new ClassCastException(), new Object[] {o});
        }

        if (owner != null)
        {
            StateManager stateManager = owner.jdoGetStateManager();

            if (stateManager != null)
            {
                PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();

                pm.acquireShareLock();

                boolean modified = false;

                try
                {
                    pm.acquireFieldUpdateLock();
                    try
                    {
                       // Mark the field as dirty
                       stateManager.makeDirty(fieldName);

                       modified = super.add(o);

                       if (modified)
                       {
                           if (removed.remove(o) == false)
                           {
                               added.add(o);
                           }
                           stateManager.applyUpdates(fieldName, this);
                       }
                       return modified;
                    }
                    finally
                    {
                        pm.releaseFieldUpdateLock();
                    }

                }
                catch (JDOUserException e)
                {
                    Object[] failedObjects = e.getFailedObjectArray();

                    if (modified && (failedObjects != null))
                    {
                        //
                        // The failedObjects array may contain objects other
                        // than the one added. We iterate through it to find
                        // the one added and remove it from the collection.
                        //
                        for (int i = 0; i < failedObjects.length; i++)
                        {
                            Object failedObject = failedObjects[i];

                            if (failedObject == o)
                            {
                                super.remove(failedObject);
                                break;
                            }
                        }
                    }

                    throw e;
                }
                finally
                {
                    pm.releaseShareLock();
                }
            }
        }

        return super.add(o);
    
public booleanaddAll(java.util.Collection c)
Adds all of the elements in the specified collection to this collection

param
c collection whose elements are to be added to this collection.
return
true if this collection changed as a result of the call.
throws
UnsupportedOperationException if the addAll method is not supported by this collection.
see
java.util.AbstractCollection
see
java.util.HashSet

        if (allowNulls == false && c.contains(null))
        {
            throw new JDOUserException(I18NHelper.getMessage(messages,
                        "sco.nulls_not_allowed")); // NOI18N
        }

        ArrayList errc = new ArrayList();

        if (elementType != null)
        {
            // iterate the collection and make a list of wrong elements.
            Iterator i = c.iterator();
            while (i.hasNext())
            {
                Object o = i.next();
                if (!elementType.isAssignableFrom(o.getClass()))
                    errc.add(o);
            }
        }

        if (errc != null && errc.size() > 0)
        {
            throw new JDOUserException(I18NHelper.getMessage(messages,
                                     "sco.classcastexception", elementType.getName()), // NOI18N
                                       new ClassCastException(), errc.toArray());
        }

        boolean modified = false;

        if (owner != null)
        {
            StateManager stateManager = owner.jdoGetStateManager();

            if (stateManager != null)
            {
                PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();

                pm.acquireShareLock();

                try
                {
                    pm.acquireFieldUpdateLock();
                    try
                    {
                        // Mark the field as dirty
                        stateManager.makeDirty(fieldName);

                        for (Iterator iter = c.iterator(); iter.hasNext();)
                        {
                            Object o = iter.next();
                            if (!super.contains(o))
                            {
                                if (removed.remove(o) == false)
                                {
                                    added.add(o);
                                }

                                super.add(o);
                                modified = true;
                            }
                        }

                        // Apply updates
                        if (modified)
                        {
                            stateManager.applyUpdates(fieldName, this);
                        }
                        return modified;
                    }
                    finally
                    {
                        pm.releaseFieldUpdateLock();
                    }
                }
                catch (JDOUserException e)
                {
                    Object[] failedObjects = e.getFailedObjectArray();

                    if (modified && (failedObjects != null))
                    {
                        for (int i = 0; i < failedObjects.length; i++)
                        {
                            super.remove(failedObjects[i]);
                        }
                    }

                    throw e;
                }
                finally
                {
                    pm.releaseShareLock();
                }
            }
        }

        return super.addAll(c);
    
public voidaddAllInternal(java.util.Collection c)
Adds a Collection to the list without recording changes if the HashSet is not deferred. Otherwise, add o to the removed list.

        if (c == null)
        {
            return;
        }

        Iterator iter = c.iterator();

        while (iter.hasNext())
        {
            addInternal(iter.next());
        }
    
public voidaddInternal(java.lang.Object o)
Adds an object to the list without recording changes if the HashSet is not deferred. Otherwise, add o to the added list.

        if (isDeferred)
        {
            if (removed.remove(o) == false)
            {
                added.add(o);
            }
        }
        else
        {
            super.add(o);
        }
    
public voidaddToBaseCollection(java.lang.Object o)

inheritDoc

        super.add(o);
    
public voidapplyDeferredUpdates(java.util.Collection c)
If the HashSet is deferred, we first initialize the internal collection with c and they apply any deferred updates specified by the added and removed lists.

        if (!isDeferred)
        {
            // should throw an exception??
            return;
        }

        isDeferred = false;
        addAllInternal(c);

        addAllInternal(added);
        removeAllInternal(removed);
        added.clear();
        removed.clear();
    
public voidapplyUpdates(com.sun.jdo.spi.persistence.support.sqlstore.StateManager sm, boolean modified)
Apply changes (can be a no-op)

         if (modified && sm != null)
        {
            sm.applyUpdates(fieldName, this);
        }
    
public voidclear()
Removes all of the elements from this set.

see
java.util.HashSet

        if (owner != null)
        {
            StateManager stateManager = owner.jdoGetStateManager();

            if (stateManager != null)
            {
                PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();

                pm.acquireShareLock();

                try
                {
                    pm.acquireFieldUpdateLock();
                    try
                    {
                        // Mark the field as dirty
                        stateManager.makeDirty(fieldName);

                        removed.clear();
                        added.clear();

                        for (Iterator iter = super.iterator(); iter.hasNext();)
                        {
                            removed.add(iter.next());
                        }

                        super.clear();

                        // Apply updates
                        stateManager.applyUpdates(fieldName, this);
                        return;
                    }
                    finally
                    {
                        pm.releaseFieldUpdateLock();
                    }
                }
                finally
                {
                    pm.releaseShareLock();
                }
            }
        }

        super.clear();
    
public voidclearInternal()
Clears Collection without notifing the owner

        super.clear();
        this.reset();
    
public java.lang.Objectclone()
Creates and returns a copy of this object.

Mutable Second Class Objects are required to provide a public clone method in order to allow for copying PersistenceCapable objects. In contrast to Object.clone(), this method must not throw a CloneNotSupportedException.

        HashSet obj = (HashSet) super.clone();

        // RESOLVE: check if added/removed should not be cleared
        // for a deferred collection, but applyDeferredUpdates logic
        // be used?
    obj.unsetOwner();

        return obj;
    
public java.lang.ObjectcloneInternal()
Creates and returns a copy of this object without resetting the owner and field value.

        return super.clone();
    
public java.util.CollectiongetAdded()
Returns added collection

return
added collection of added elements

        return (Collection)added;
    
public java.lang.StringgetFieldName()
Returns the field name

return
field name as java.lang.String

        return this.fieldName;
    
public java.lang.ObjectgetOwner()
Returns the owner object of the SCO instance

return
owner object

        return this.owner;
    
public java.util.CollectiongetRemoved()
Returns removed collection

return
removed collection of removed elements

        return (Collection)removed;
    
public booleanisDeferred()
Return true is this HashSet is deferred, false otherwise.

        return isDeferred;
    
public java.util.Iteratoriterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.

return
an Iterator over the elements in this set.
see
java.util.ConcurrentModificationException

        return new SCOHashIterator(super.iterator(), this);
    
public com.sun.jdo.spi.persistence.support.sqlstore.StateManagermakeDirty()
Marks object dirty

        StateManager stateManager = owner.jdoGetStateManager();

        if (stateManager != null)
        {
            stateManager.makeDirty(fieldName);
        }

        return stateManager;
    
public voidmarkDeferred()
Mark this HashSet as deferred.

        isDeferred = true;
    
public booleanremove(java.lang.Object o)
Removes the given element from this set if it is present.

param
o object to be removed from this set, if present.
return
true if the set contained the specified element.
see
java.util.HashSet

    // Mark the field as dirty

        if (owner != null)
        {
            StateManager stateManager = owner.jdoGetStateManager();

            if (stateManager != null)
            {
                PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();

                pm.acquireShareLock();

                try
                {
                    pm.acquireFieldUpdateLock();
                    try
                    {
                        stateManager.makeDirty(fieldName);

                        boolean modified = super.remove(o);

                        if (modified)
                        {
                            if (added.remove(o) == false)
                            {
                                removed.add(o);
                            }

                            stateManager.applyUpdates(fieldName, this);
                        }

                        return modified;
                    }
                    finally
                    {
                        pm.releaseFieldUpdateLock();
                    }
                }
                finally
                {
                    pm.releaseShareLock();
                }
            }
        }

        return super.remove(o);
    
public booleanremoveAll(java.util.Collection c)
Removes from this collection all of its elements that are contained in the specified collection (optional operation).

param
c elements to be removed from this collection.
return
true if this collection changed as a result of the call.
throws
UnsupportedOperationException removeAll is not supported by this collection.
see
java.util.HashSet
see
java.util.AbstractCollection

        // Mark the field as dirty

        if (owner != null)
        {
            StateManager stateManager = owner.jdoGetStateManager();

            if (stateManager != null)
            {
                PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();

                pm.acquireShareLock();

                try
                {
                    pm.acquireFieldUpdateLock();
                    try
                    {
                        stateManager.makeDirty(fieldName);

                        for (Iterator iter = c.iterator(); iter.hasNext();)
                        {
                            Object o = iter.next();
                            if (super.contains(o))
                            {
                                if (added.remove(o) == false)
                                {
                                    removed.add(o);
                                }
                            }
                        }

                        boolean modified = super.removeAll(c);

                        // Apply updates
                        if (modified)
                        {
                            stateManager.applyUpdates(fieldName, this);
                        }

                        return modified;
                    }
                    finally
                    {
                        pm.releaseFieldUpdateLock();
                    }
                }
                finally
                {
                    pm.releaseShareLock();
                }
            }
        }

        return super.removeAll(c);
    
public voidremoveAllInternal(java.util.Collection c)

        if (c == null)
        {
            return;
        }

        Iterator iter = c.iterator();

        while (iter.hasNext())
        {
            removeInternal(iter.next());
        }
    
public voidremoveInternal(java.lang.Object o)
Removes an element without notifing the owner

        if (isDeferred)
        {
            if (added.remove(o) == false)
            {
                removed.add(o);
            }

        }
        else
        {
            super.remove(o);
        }
    
public voidreset()
Cleans removed and added lists

        // RESOLVE: do we need to synchronize this??
        if (added != null)
            added.clear();

        if (removed != null)
            removed.clear();
    
public booleanretainAll(java.util.Collection c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).

return
true if this collection changed as a result of the call.
throws
UnsupportedOperationException if the retainAll method is not supported by this collection.
see
java.util.HashSet
see
java.util.AbstractCollection

        if (owner != null)
        {
            StateManager stateManager = owner.jdoGetStateManager();

            if (stateManager != null)
            {
                PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();

                pm.acquireShareLock();

                try
                {
                    pm.acquireFieldUpdateLock();
                    try
                    {
                        // Mark the field as dirty
                        stateManager.makeDirty(fieldName);

                        for (Iterator iter = super.iterator(); iter.hasNext();)
                        {
                            Object o = iter.next();
                            if (!c.contains(o))
                            {
                                if (added.remove(o) == false)
                                {
                                    removed.add(o);
                                }
                            }
                        }

                        boolean modified = super.retainAll(c);

                        // Apply updates

                        if (modified)
                        {
                            stateManager.applyUpdates(fieldName, this);
                        }

                        return modified;
                    }
                    finally
                    {
                        pm.releaseFieldUpdateLock();
                    }
                }
                finally
                {
                    pm.releaseShareLock();
                }
            }
        }

        return super.retainAll(c);
    
public voidsetOwner(java.lang.Object owner, java.lang.String fieldName, java.lang.Class elementType)
Set the owner if this instance is not owned.

see
SCOCollection#setOwner
param
owner the new owner.
param
fieldName the new field name.
param
elementType the new element type as Class, or null if type is not checked or not supported.


        if (this.owner != null) {
            throw new JDOUserException(I18NHelper.getMessage(
                messages1, "core.statemanager.anotherowner"), // NOI18N
                new Object[]{this.owner, this.fieldName});
        }
        if (owner instanceof PersistenceCapable) {
                this.owner = (PersistenceCapable)owner;
                this.fieldName = fieldName;
                this.elementType = elementType;
        }
    
public voidunsetOwner()
Nullifies references to the owner Object and Field

        this.owner = null;
        this.fieldName = null;
        this.elementType = null;
                added.clear();
                removed.clear();