Module org.eclipse.persistence.core
Class IndirectSet<E>
java.lang.Object
org.eclipse.persistence.indirection.IndirectSet<E>
- Type Parameters:
E- the type of elements maintained by this set
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,Set<E>,ChangeTracker,CollectionChangeTracker,IndirectCollection<E,,Set<E>> IndirectContainer<Set<E>>
public class IndirectSet<E>
extends Object
implements CollectionChangeTracker, Set<E>, IndirectCollection<E,Set<E>>, Cloneable, Serializable
IndirectSet is an example implementation of the Set protocol that
allows a domain class to take advantage of TopLink Indirection
without having to declare its instance variable as a ValueHolderInterface.
To use an IndirectSet:
- Declare the appropriate instance variable with type Set (or Collection).
- Send the message #useTransparentCollection() to the appropriate CollectionMapping.
- Send the message #useCollectionClass(IndirectSet.class) to the same CollectionMapping. (The order of these two message sends is significant.)
Implementation notes:
- The Set interface is implemented by delegating nearly every message to the Set held on to by the 'delegate' instance variable. (The 'delegate' will be either a HashSet or yet another IndirectSet.)
- The IndirectContainer interface is implemented in a straightforward
fashion:
- #get- and #setValueHolder() are implemented as simple accessors for the 'valueHolder' instance variable. (Note that #setValueHolder() clears out the 'delegate' instance variable, since its contents are invalidated by the arrival of a new value holder.)
- #isInstantiated() is simply delegated to the value holder.
- TopLink requires that the Cloneable interface be implemented. The #clone() method must clone the 'delegate'. (The implementation here uses reflection to invoke the #clone() method because it is not included in the common interface shared by IndirectSet and its base delegate class, HashSet; namely, Set.)
- TopLink requires that the Serializable interface be implemented.
- The database read is ultimately triggered when one of the "delegated" methods makes the first call to #getDelegate(), which in turn calls #buildDelegate(), which sends the message #getValue() to the value holder. The value holder performs the database read.
- For debugging purposes, #toString() will not trigger a database read. This is not required behavior.
- Since:
- TOPLink/Java 3.0+
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intStore initial size for lazy init.protected floatStore load factor for lazy init. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty IndirectSet.IndirectSet(int initialCapacity) Construct an empty IndirectSet with the specified initial capacity.IndirectSet(int initialCapacity, float loadFactor) Construct an empty IndirectSet with the specified initial capacity and load factor.IndirectSet(Collection<? extends E> c) Construct an IndirectSet containing the elements of the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionINTERNAL: Return the property change listener for change tracking.void_persistence_setPropertyChangeListener(PropertyChangeListener changeListener) INTERNAL: Set the property change listener for change tracking.booleanbooleanaddAll(Collection<? extends E> c) INTERNAL: Return the freshly-built delegate.voidclear()voidINTERNAL: clear any changes that have been deferred to instantiation.clone()INTERNAL: Clone the delegate.booleanbooleancontainsAll(Collection<?> c) booleanvoidINTERNAL: Return the elements that have been added before instantiation.INTERNAL: Check whether the contents have been read from the database.INTERNAL: Return the real collection object.INTERNAL: Return the elements that have been removed before instantiation.INTERNAL: Return the mapping attribute name, used to raise change events.INTERNAL: Return the valueHolder.booleanINTERNAL: Return if any elements that have been added before instantiation.booleanINTERNAL: Return whether this IndirectSet has been registered in a UnitOfWorkbooleanINTERNAL: Return if any elements that have been added or removed before instantiation.inthashCode()booleanINTERNAL: Return if any elements that have been removed before instantiation.booleanINTERNAL: Return if the collection has a property change listener for change tracking.booleanisEmpty()booleanReturn whether the contents have been read from the database.protected booleaniterator()protected voidraiseAddChangeEvent(Object element) Raise the add change event and relationship maintainence.protected voidraiseRemoveChangeEvent(Object element) Raise the remove change event.booleanbooleanremoveAll(Collection<?> c) booleanbooleanretainAll(Collection<?> c) voidsetTrackedAttributeName(String attributeName) INTERNAL: Set the mapping attribute name, used to raise change events.voidsetUseLazyInstantiation(boolean useLazyInstantiation) INTERNAL Set whether this collection should attempt do deal with adds and removes without retrieving the collection from the dBvoidsetValueHolder(ValueHolderInterface<Set<E>> valueHolder) INTERNAL: Set the value holder.protected booleanINTERNAL: Return if add/remove should trigger instantiation or avoid.protected booleanReturn whether this collection should attempt do deal with adds and removes without retrieving the collection from the dBintsize()stream()Object[]toArray()<T> T[]toArray(T[] a) toString()Use the delegate's #toString(); but wrap it with braces to indicate there is a bit of indirection.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
toArray
-
Field Details
-
initialCapacity
protected int initialCapacityStore initial size for lazy init. -
loadFactor
protected float loadFactorStore load factor for lazy init.
-
-
Constructor Details
-
IndirectSet
public IndirectSet()Construct an empty IndirectSet. -
IndirectSet
public IndirectSet(int initialCapacity) Construct an empty IndirectSet with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the set- Throws:
IllegalArgumentException- if the specified initial capacity is negative
-
IndirectSet
public IndirectSet(int initialCapacity, float loadFactor) Construct an empty IndirectSet with the specified initial capacity and load factor.- Parameters:
initialCapacity- the initial capacity of the setloadFactor- the load factor of the set- Throws:
IllegalArgumentException- if the specified initial capacity is negative
-
IndirectSet
Construct an IndirectSet containing the elements of the specified collection.- Parameters:
c- the initial elements of the set
-
-
Method Details
-
isRelationshipMaintenanceRequired
protected boolean isRelationshipMaintenanceRequired() -
add
-
addAll
-
buildDelegate
INTERNAL: Return the freshly-built delegate. -
clear
public void clear() -
clearDeferredChanges
public void clearDeferredChanges()INTERNAL: clear any changes that have been deferred to instantiation. Indirect collections with change tracking avoid instantiation on add/remove.- Specified by:
clearDeferredChangesin interfaceIndirectCollection<E,Set<E>>
-
clone
-
cloneDelegate
INTERNAL: Clone the delegate. -
contains
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>- See Also:
-
equals
-
spliterator
- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>- Specified by:
spliteratorin interfaceSet<E>
-
parallelStream
- Specified by:
parallelStreamin interfaceCollection<E>
-
stream
- Specified by:
streamin interfaceCollection<E>
-
removeIf
- Specified by:
removeIfin interfaceCollection<E>
-
forEach
-
getDelegate
INTERNAL: Check whether the contents have been read from the database. If they have not, read them and set the delegate. -
getDelegateObject
INTERNAL: Return the real collection object. This will force instantiation.- Specified by:
getDelegateObjectin interfaceIndirectCollection<E,Set<E>>
-
getValueHolder
INTERNAL: Return the valueHolder.- Specified by:
getValueHolderin interfaceIndirectContainer<E>- Returns:
- org.eclipse.persistence.indirection.ValueHolderInterface A representation of the valueholder * which this container uses
-
hasBeenRegistered
public boolean hasBeenRegistered()INTERNAL: Return whether this IndirectSet has been registered in a UnitOfWork -
hashCode
public int hashCode() -
isEmpty
public boolean isEmpty() -
isInstantiated
public boolean isInstantiated()Return whether the contents have been read from the database.- Specified by:
isInstantiatedin interfaceIndirectContainer<E>
-
iterator
-
remove
-
removeAll
-
retainAll
-
setValueHolder
INTERNAL: Set the value holder. Note that the delegate must be cleared out.- Specified by:
setValueHolderin interfaceIndirectContainer<E>
-
setUseLazyInstantiation
public void setUseLazyInstantiation(boolean useLazyInstantiation) INTERNAL Set whether this collection should attempt do deal with adds and removes without retrieving the collection from the dB- Specified by:
setUseLazyInstantiationin interfaceIndirectCollection<E,Set<E>>
-
size
public int size() -
shouldUseLazyInstantiation
protected boolean shouldUseLazyInstantiation()Return whether this collection should attempt do deal with adds and removes without retrieving the collection from the dB -
toArray
-
toArray
public <T> T[] toArray(T[] a) -
toString
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. -
raiseAddChangeEvent
Raise the add change event and relationship maintainence. -
raiseRemoveChangeEvent
Raise the remove change event. -
_persistence_getPropertyChangeListener
INTERNAL: Return the property change listener for change tracking.- Specified by:
_persistence_getPropertyChangeListenerin interfaceChangeTracker
-
hasTrackedPropertyChangeListener
public boolean hasTrackedPropertyChangeListener()INTERNAL: Return if the collection has a property change listener for change tracking. -
_persistence_setPropertyChangeListener
INTERNAL: Set the property change listener for change tracking.- Specified by:
_persistence_setPropertyChangeListenerin interfaceChangeTracker
-
getTrackedAttributeName
INTERNAL: Return the mapping attribute name, used to raise change events.- Specified by:
getTrackedAttributeNamein interfaceCollectionChangeTracker
-
setTrackedAttributeName
INTERNAL: Set the mapping attribute name, used to raise change events. This is required if the change listener is set.- Specified by:
setTrackedAttributeNamein interfaceCollectionChangeTracker
-
getRemovedElements
INTERNAL: Return the elements that have been removed before instantiation.- Specified by:
getRemovedElementsin interfaceIndirectCollection<E,Set<E>>
-
getAddedElements
INTERNAL: Return the elements that have been added before instantiation.- Specified by:
getAddedElementsin interfaceIndirectCollection<E,Set<E>>
-
hasAddedElements
public boolean hasAddedElements()INTERNAL: Return if any elements that have been added before instantiation. -
hasRemovedElements
public boolean hasRemovedElements()INTERNAL: Return if any elements that have been removed before instantiation. -
hasDeferredChanges
public boolean hasDeferredChanges()INTERNAL: Return if any elements that have been added or removed before instantiation.- Specified by:
hasDeferredChangesin interfaceIndirectCollection<E,Set<E>>
-
shouldAvoidInstantiation
protected boolean shouldAvoidInstantiation()INTERNAL: Return if add/remove should trigger instantiation or avoid. Current instantiation is avoided is using change tracking.
-