org.apache.ojb.broker.core.proxy
Class CollectionProxyDefaultImpl

java.lang.Object
  extended by org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl
All Implemented Interfaces:
Serializable, Iterable, Collection, CollectionProxy, ManageableCollection
Direct Known Subclasses:
ListProxyDefaultImpl, SetProxyDefaultImpl

public class CollectionProxyDefaultImpl
extends Object
implements Collection, ManageableCollection, CollectionProxy

A place holder for a whole collection to support deferred loading of relationships. The complete collection is loaded on access to the data.

Version:
$Id: CollectionProxyDefaultImpl.java,v 1.1 2007-08-24 22:17:32 ewestfal Exp $
Author:
Jakob Braeuchi
See Also:
Serialized Form

Constructor Summary
CollectionProxyDefaultImpl(PBKey brokerKey, Class collClass, Query query)
          Creates a new collection proxy that uses the given collection type.
CollectionProxyDefaultImpl(PBKey brokerKey, Query query)
          Creates a new collection proxy (uses RemovalAwareCollection as the collection class).
 
Method Summary
 boolean add(Object o)
           
 boolean addAll(Collection c)
           
 void addListener(CollectionProxyListener listener)
          Adds a listener to this collection.
protected  void afterLoading()
          Notifies all listeners that the data has been loaded.
 void afterStore(PersistenceBroker broker)
          A callback method to implement 'removal-aware' (track removed objects and delete them by its own) collection implementations.
protected  void beforeLoading()
          Notifies all listeners that the data is about to be loaded.
 void clear()
          Clears the proxy.
 boolean contains(Object o)
           
 boolean containsAll(Collection c)
           
protected  PersistenceBroker getBroker()
          Acquires a broker instance.
 PBKey getBrokerKey()
          Returns the key of the persistence broker used by this collection.
 Class getCollectionClass()
          Returns the collection type.
 Collection getData()
          Returns the collection data, load it if not already done so.
protected  Object getProfileKey()
          Returns the metadata profile key used when creating this proxy.
 Query getQuery()
          Returns the defining query.
 boolean isEmpty()
           
 boolean isLoaded()
          Determines whether the collection data already has been loaded from the database.
 Iterator iterator()
           
protected  Collection loadData()
          Loads the data from the database.
protected  void loadProfileIfNeeded()
          Reactivates metadata profile used when creating proxy, if needed.
protected  int loadSize()
          Determines the number of elements that the query would return.
 void ojbAdd(Object anObject)
          Adds a single object to the Collection.
 void ojbAddAll(ManageableCollection otherCollection)
          Adds another collection to this collection.
 Iterator ojbIterator()
          Returns an iterator over all elements in the collection.
protected  void releaseBroker(PersistenceBroker broker)
          Release the broker instance.
 boolean remove(Object o)
           
 boolean removeAll(Collection c)
           
 void removeListener(CollectionProxyListener listener)
          Removes the given listener from this collecton.
 boolean retainAll(Collection c)
           
protected  void setBrokerKey(PBKey brokerKey)
          Sets the key of the persistence broker used by this collection.
protected  void setCollectionClass(Class collClass)
          Sets the collection type.
 void setData(Collection data)
          Sets the collection data.
 void setProfileKey(Object profileKey)
          Sets the metadata profile key used when creating this proxy.
protected  void setQuery(Query query)
          Sets the defining query.
protected  void setSize(int size)
          Sets the size internally.
 int size()
           
 Object[] toArray()
           
 Object[] toArray(Object[] a)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

CollectionProxyDefaultImpl

public CollectionProxyDefaultImpl(PBKey brokerKey,
                                  Query query)
Creates a new collection proxy (uses RemovalAwareCollection as the collection class).

Parameters:
brokerKey - The key of the persistence broker
query - The defining query

CollectionProxyDefaultImpl

public CollectionProxyDefaultImpl(PBKey brokerKey,
                                  Class collClass,
                                  Query query)
Creates a new collection proxy that uses the given collection type.

Parameters:
brokerKey - The key of the persistence broker
collClass - The collection type
query - The defining query
Method Detail

loadProfileIfNeeded

protected void loadProfileIfNeeded()
Reactivates metadata profile used when creating proxy, if needed. Calls to this method should be guarded by checking _perThreadDescriptorsEnabled since the profile never needs to be reloaded if not using pre-thread metadata changes.


isLoaded

public boolean isLoaded()
Determines whether the collection data already has been loaded from the database.

Specified by:
isLoaded in interface CollectionProxy
Returns:
true if the data is already loaded

loadSize

protected int loadSize()
                throws PersistenceBrokerException
Determines the number of elements that the query would return. Override this method if the size shall be determined in a specific way.

Returns:
The number of elements
Throws:
PersistenceBrokerException

setSize

protected void setSize(int size)
Sets the size internally.

Parameters:
size - The new size

loadData

protected Collection loadData()
                       throws PersistenceBrokerException
Loads the data from the database. Override this method if the objects shall be loaded in a specific way.

Returns:
The loaded data
Throws:
PersistenceBrokerException

beforeLoading

protected void beforeLoading()
Notifies all listeners that the data is about to be loaded.


afterLoading

protected void afterLoading()
Notifies all listeners that the data has been loaded.


size

public int size()
Specified by:
size in interface Collection
See Also:
Collection.size()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection
See Also:
Collection.isEmpty()

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection
See Also:
Collection.contains(Object)

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
See Also:
Collection.iterator()

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection
See Also:
Collection.toArray()

toArray

public Object[] toArray(Object[] a)
Specified by:
toArray in interface Collection
See Also:
Collection.toArray(Object[])

add

public boolean add(Object o)
Specified by:
add in interface Collection
See Also:
Collection.add(Object)

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection
See Also:
Collection.remove(Object)

containsAll

public boolean containsAll(Collection c)
Specified by:
containsAll in interface Collection
See Also:
Collection.containsAll(Collection)

addAll

public boolean addAll(Collection c)
Specified by:
addAll in interface Collection
See Also:
Collection.addAll(Collection)

removeAll

public boolean removeAll(Collection c)
Specified by:
removeAll in interface Collection
See Also:
Collection.removeAll(Collection)

retainAll

public boolean retainAll(Collection c)
Specified by:
retainAll in interface Collection
See Also:
Collection.retainAll(Collection)

clear

public void clear()
Clears the proxy. A cleared proxy is defined as loaded

Specified by:
clear in interface Collection
See Also:
Collection.clear()

getQuery

public Query getQuery()
Returns the defining query.

Returns:
The query

setQuery

protected void setQuery(Query query)
Sets the defining query.

Parameters:
query - The query

releaseBroker

protected void releaseBroker(PersistenceBroker broker)
Release the broker instance.


getBroker

protected PersistenceBroker getBroker()
                               throws PBFactoryException
Acquires a broker instance. If no PBKey is available a runtime exception will be thrown.

Returns:
A broker instance
Throws:
PBFactoryException

getData

public Collection getData()
Returns the collection data, load it if not already done so.

Returns:
The data

setData

public void setData(Collection data)
Sets the collection data.

Parameters:
data - The data

getCollectionClass

public Class getCollectionClass()
Returns the collection type.

Returns:
The collection type

setCollectionClass

protected void setCollectionClass(Class collClass)
Sets the collection type.

Parameters:
collClass - The collection type

ojbAdd

public void ojbAdd(Object anObject)
Description copied from interface: ManageableCollection
Adds a single object to the Collection. This method is used during reading collection elements from the database. Thus it is safe to cast the object to the underlying element type of the collection.

Specified by:
ojbAdd in interface ManageableCollection
Parameters:
anObject - The object to add
See Also:
ManageableCollection.ojbAdd(Object)

ojbAddAll

public void ojbAddAll(ManageableCollection otherCollection)
Description copied from interface: ManageableCollection
Adds another collection to this collection. Used in reading extents from the database. Thus it is safe to cast the given collection to this class.

Specified by:
ojbAddAll in interface ManageableCollection
Parameters:
otherCollection - The added collection
See Also:
ManageableCollection.ojbAddAll(ManageableCollection)

ojbIterator

public Iterator ojbIterator()
Description copied from interface: ManageableCollection
Returns an iterator over all elements in the collection. Used during store and delete operations. If the implementor does not return an iterator over ALL elements, OJB cannot store and delete all elements properly.

Specified by:
ojbIterator in interface ManageableCollection
Returns:
The iterator
See Also:
ManageableCollection.ojbIterator()

afterStore

public void afterStore(PersistenceBroker broker)
                throws PersistenceBrokerException
Description copied from interface: ManageableCollection
A callback method to implement 'removal-aware' (track removed objects and delete them by its own) collection implementations.

Specified by:
afterStore in interface ManageableCollection
Parameters:
broker - The persistence broker
Throws:
PersistenceBrokerException
See Also:
ManageableCollection.afterStore(PersistenceBroker broker)

getBrokerKey

public PBKey getBrokerKey()
Returns the key of the persistence broker used by this collection.

Returns:
The broker key

setBrokerKey

protected void setBrokerKey(PBKey brokerKey)
Sets the key of the persistence broker used by this collection.

Parameters:
brokerKey - The key of the broker

getProfileKey

protected Object getProfileKey()
Returns the metadata profile key used when creating this proxy.

Returns:
brokerKey The key of the broker

setProfileKey

public void setProfileKey(Object profileKey)
Sets the metadata profile key used when creating this proxy.

Parameters:
profileKey - the metadata profile key

addListener

public void addListener(CollectionProxyListener listener)
Adds a listener to this collection.

Specified by:
addListener in interface CollectionProxy
Parameters:
listener - The listener to add

removeListener

public void removeListener(CollectionProxyListener listener)
Removes the given listener from this collecton.

Specified by:
removeListener in interface CollectionProxy
Parameters:
listener - The listener to remove


Copyright © 2004-2012 The Kuali Foundation. All Rights Reserved.