/** * Raise the add change event and relationship maintainence. */ protected void raiseAddChangeEvent(Object element) { if (hasTrackedPropertyChangeListener()) { _persistence_getPropertyChangeListener().propertyChange(new CollectionChangeEvent(this, getTrackedAttributeName(), this, element, CollectionChangeEvent.ADD, true)); } if (isRelationshipMaintenanceRequired()) { ((UnitOfWorkQueryValueHolder)getValueHolder()).updateForeignReferenceSet(element, null); } }
/** * @see java.util.Set#addAll(java.util.Collection) */ public boolean addAll(Collection c) { // Must trigger add events if tracked or uow. if (hasBeenRegistered() || hasTrackedPropertyChangeListener()) { Iterator objects = c.iterator(); while (objects.hasNext()) { this.add(objects.next()); } return true; } return getDelegate().addAll(c); }
/** * INTERNAL: * Check whether the contents have been read from the database. * If they have not, read them and set the delegate. */ protected Set getDelegate() { if (delegate == null) { synchronized(this){ if (delegate == null) { delegate = this.buildDelegate(); } } } return delegate; }
/** * INTERNAL: * Return if any elements that have been added or removed before instantiation. */ public boolean hasDeferredChanges() { return hasRemovedElements() || hasAddedElements(); }
/** * INTERNAL: * Return if add/remove should trigger instantiation or avoid. * Current instantiation is avoided is using change tracking. */ protected boolean shouldAvoidInstantiation() { return (!isInstantiated()) && (shouldUseLazyInstantiation()) && (_persistence_getPropertyChangeListener() instanceof AttributeChangeListener) && ((WeavedAttributeValueHolderInterface)getValueHolder()).shouldAllowInstantiationDeferral(); } }
/** * INTERNAL: * Return if add/remove should trigger instantiation or avoid. * Current instantiation is avoided is using change tracking. */ protected boolean shouldAvoidInstantiation() { return (!isInstantiated()) && (_persistence_getPropertyChangeListener() instanceof AttributeChangeListener) && ((WeavedAttributeValueHolderInterface)getValueHolder()).shouldAllowInstantiationDeferral(); } }
/** * @see java.util.Set#contains(java.lang.Object) */ public boolean contains(Object element) { // PERF: Avoid instantiation if not required. if (hasAddedElements()) { if (getAddedElements().contains(element)) { return true; } } if (hasRemovedElements()) { if (getRemovedElements().contains(element)) { return false; } } return this.getDelegate().contains(element); }
/** * @see java.util.Set#clear() */ public void clear() { if (hasBeenRegistered() || hasTrackedPropertyChangeListener()) { Iterator objects = this.iterator(); while (objects.hasNext()) { Object o = objects.next(); objects.remove(); this.raiseRemoveChangeEvent(o); } } else { this.getDelegate().clear(); } }
/** * @see java.util.Set#add(java.lang.Object) */ @Override public boolean add(E element) { boolean added = true; // PERF: If not instantiated just record the add to avoid the instantiation. if (shouldAvoidInstantiation()) { if (hasRemovedElements() && getRemovedElements().contains(element)) { getRemovedElements().remove(element); } else if (isRelationshipMaintenanceRequired() && getAddedElements().contains(element)) { // Must avoid recursion for relationship maintenance. return false; } else { getAddedElements().add(element); } } else { added = getDelegate().add(element); } if (added) { raiseAddChangeEvent(element); } return added; }
/** * @see java.util.Set#remove(java.lang.Object) */ public boolean remove(Object element) { // PERF: If not instantiated just record the removal to avoid the instantiation. if (shouldAvoidInstantiation()) { if (hasAddedElements() && getAddedElements().contains(element)) { getAddedElements().remove(element); } else if (getRemovedElements().contains(element)) { // Must avoid recursion for relationship maintenance. return false; } else { getRemovedElements().add(element); } this.raiseRemoveChangeEvent(element); return true; } else if (this.getDelegate().remove(element)) { this.raiseRemoveChangeEvent(element); return true; } return false; }
/** * Raise the remove change event. */ protected void raiseRemoveChangeEvent(Object element) { if (hasTrackedPropertyChangeListener()) { _persistence_getPropertyChangeListener().propertyChange(new CollectionChangeEvent(this, getTrackedAttributeName(), this, element, CollectionChangeEvent.REMOVE)); } if (hasBeenRegistered()) { ((UnitOfWorkQueryValueHolder)getValueHolder()).updateForeignReferenceRemove(element); } }
/** * @see java.util.Set#removeAll(java.util.Collection) */ public boolean removeAll(Collection c) { // Must trigger remove events if tracked or uow. if (hasBeenRegistered() || hasTrackedPropertyChangeListener()) { Iterator objects = c.iterator(); while (objects.hasNext()) { this.remove(objects.next()); } return true; } return this.getDelegate().removeAll(c); }
/** * INTERNAL: * Return the real collection object. * This will force instantiation. */ @Override public Object getDelegateObject() { return getDelegate(); }
/** * Use the delegate's #toString(); but wrap it with braces to indicate * there is a bit of indirection. * Don't allow this method to trigger a database read. * @see java.util.HashSet#toString() */ @Override public String toString() { if (ValueHolderInterface.shouldToStringInstantiate) { return this.getDelegate().toString(); } if (this.isInstantiated()) { return "{" + this.getDelegate().toString() + "}"; } else { return "{" + org.eclipse.persistence.internal.helper.Helper.getShortClassName(this.getClass()) + ": " + ToStringLocalization.buildMessage("not_instantiated", (Object[])null) + "}"; } }
/** * INTERNAL: * Return whether this IndirectSet has been registered in a UnitOfWork */ public boolean hasBeenRegistered() { return getValueHolder() instanceof org.eclipse.persistence.internal.indirection.UnitOfWorkQueryValueHolder; }
/** * @see java.lang.Object#clone() * This will result in a database query if necessary. */ /* There are 3 situations when #clone() is called: 1. The developer actually wants to clone the collection (typically to modify one of the 2 resulting collections). In which case the contents must be read from the database. 2. A UnitOfWork needs a clone (or backup clone) of the collection. But the UnitOfWork checks "instantiation" before cloning collections (i.e. "un-instantiated" collections are not cloned). 3. A MergeManager needs an extra copy of the collection (because the "backup" and "target" are the same object?). But the MergeManager also checks "instantiation" before merging collections (again, "un-instantiated" collections are not merged). */ public Object clone() { try { IndirectSet result = (IndirectSet)super.clone(); result.delegate = this.cloneDelegate(); result.attributeName = null; result.changeListener = null; return result; } catch (CloneNotSupportedException e) { throw new InternalError("clone not supported"); } }
/** * @see java.util.Set#contains(java.lang.Object) */ @Override public boolean contains(Object element) { // PERF: Avoid instantiation if not required. if (hasAddedElements()) { if (getAddedElements().contains(element)) { return true; } } if (hasRemovedElements()) { if (getRemovedElements().contains(element)) { return false; } } return this.getDelegate().contains(element); }
/** * @see java.util.Set#clear() */ @Override public void clear() { if (hasBeenRegistered() || hasTrackedPropertyChangeListener()) { Iterator<E> objects = iterator(); while (objects.hasNext()) { objects.next(); objects.remove(); } // clear delegate in case it's still not empty, see bug 338393 } getDelegate().clear(); }
/** * @see java.util.Set#add(java.lang.Object) */ public boolean add(Object element) { boolean added = true; // PERF: If not instantiated just record the add to avoid the instantiation. if (shouldAvoidInstantiation()) { if (hasRemovedElements() && getRemovedElements().contains(element)) { getRemovedElements().remove(element); } else if (getAddedElements().contains(element)) { // Must avoid recursion for relationship maintenance. return false; } else { getAddedElements().add(element); } } else { added = getDelegate().add(element); } raiseAddChangeEvent(element); return added; }
/** * @see java.util.Set#remove(java.lang.Object) */ @Override public boolean remove(Object element) { // PERF: If not instantiated just record the removal to avoid the instantiation. if (shouldAvoidInstantiation()) { if (hasAddedElements() && getAddedElements().contains(element)) { getAddedElements().remove(element); } else if (getRemovedElements().contains(element)) { // Must avoid recursion for relationship maintenance. return false; } else { getRemovedElements().add((E) element); } this.raiseRemoveChangeEvent(element); return true; } else if (this.getDelegate().remove(element)) { this.raiseRemoveChangeEvent(element); return true; } return false; }