db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/core/proxy CollectionProxyDefaultImpl.java IndirectionHandlerDefaultImpl.java
Date Mon, 12 Apr 2004 18:03:43 GMT
tomdz       2004/04/12 11:03:43

  Modified:    src/java/org/apache/ojb/broker/core/proxy
                        CollectionProxyDefaultImpl.java
                        IndirectionHandlerDefaultImpl.java
  Log:
  Enhanced documentation and added setSize method for subclasses of CollectionProxyDefaultImpl
  
  Revision  Changes    Path
  1.3       +161 -91   db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxyDefaultImpl.java
  
  Index: CollectionProxyDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxyDefaultImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CollectionProxyDefaultImpl.java	12 Apr 2004 16:32:40 -0000	1.2
  +++ CollectionProxyDefaultImpl.java	12 Apr 2004 18:03:43 -0000	1.3
  @@ -31,55 +31,81 @@
   import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
   
   /**
  - * A placeHolder for a whole collection to support deferred loading of relationships.
  - * The complete relationship is loaded on request.
  + * A place holder for a whole collection to support deferred loading of relationships.
  + * The complete collection is loaded on access to the data.
    *
    * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
    * @version $Id$
    */
   public class CollectionProxyDefaultImpl implements Collection, ManageableCollection
   {
  -    private transient PersistenceBroker m_broker;
  -    private PBKey m_brokerKey;
  -    private Query m_query;
  -    private Collection m_data;
  -    private Class m_collectionClass;
  -    private int m_size = -1;
  +    /** Reference to the used PersistenceBroker */
  +    private transient PersistenceBroker _broker;
  +    /** The key for acquiring the above broker */
  +    private PBKey _brokerKey;
  +    /** The query that defines the values in the collection */
  +    private Query _query;
  +    /** The actual data (if already loaded) */
  +    private Collection _data;
  +    /** The collection type */
  +    private Class _collectionClass;
  +    /** The number of objects */
  +    private int _size = -1;
       /*
       arminw
       fix a bug, caused by closing PB instances
       obtained from PersistenceBrokerThreadMapping.
       TODO: Could we find a better solution for this?
       */
  -    private boolean needsClose;
  +    private boolean _needsClose;
  +    /** Objects that listen on this proxy for loading events */
  +    private transient ArrayList _listeners;
   
  -    private transient ArrayList m_listeners;
  -
  -    public CollectionProxyDefaultImpl(PBKey aKey, Query aQuery)
  +    /**
  +     * Creates a new collection proxy (uses
  +     * {@link org.apache.ojb.broker.util.collections.RemovalAwareCollection}
  +     * as the collection class).
  +     * 
  +     * @param brokerKey The key of the persistence broker
  +     * @param query     The defining query
  +     */
  +    public CollectionProxyDefaultImpl(PBKey brokerKey, Query query)
       {
  -        this(aKey, RemovalAwareCollection.class, aQuery);
  +        this(brokerKey, RemovalAwareCollection.class, query);
       }
   
  -    public CollectionProxyDefaultImpl(PBKey aKey, Class aCollClass, Query aQuery)
  +    /**
  +     * Creates a new collection proxy that uses the given collection type.
  +     * 
  +     * @param brokerKey The key of the persistence broker
  +     * @param collClass The collection type
  +     * @param query     The defining query
  +     */
  +    public CollectionProxyDefaultImpl(PBKey brokerKey, Class collClass, Query query)
       {
  -        setBrokerKey(aKey);
  -        setCollectionClass(aCollClass);
  -        setQuery(aQuery);
  +        setBrokerKey(brokerKey);
  +        setCollectionClass(collClass);
  +        setQuery(query);
       }
   
  +    /**
  +     * Determines whether the collection data already has been loaded from the database.
  +     * 
  +     * @return <code>true</code> if the data is already loaded
  +     */
       public boolean isLoaded()
       {
  -        return m_data != null;
  +        return _data != null;
       }
   
       /**
  -     * do a count(*) with the query
  +     * Determines the number of elements that the query would return.
        */
       protected synchronized void loadSize() throws PersistenceBrokerException
       {
           try
           {
  -            m_size = getBroker().getCount(getQuery());
  +            setSize(getBroker().getCount(getQuery()));
           }
           catch (Exception ex)
           {
  @@ -92,24 +118,37 @@
       }
   
       /**
  -     * loads the data from the database, if count(*) returned > 0
  +     * Sets the size internally.
  +     * 
  +     * @param size The new size
  +     */
  +    protected synchronized void setSize(int size)
  +    {
  +        _size = size;
  +    }
  +    
  +    /**
  +     * Loads the data from the database.
  +     * 
  +     * @return The loaded data
        */
       protected Collection loadData() throws PersistenceBrokerException
       {
           try
           {
               Collection result;
  -            if (m_data != null) // could be set by listener
  +
  +            if (_data != null) // could be set by listener
               {
  -                result = m_data;
  +                result = _data;
               }
  -            else if (m_size != 0)
  +            else if (_size != 0)
               {
  -                result = (Collection) getBroker().getCollectionByQuery(getCollectionClass(),
getQuery());
  +                result = (Collection)getBroker().getCollectionByQuery(getCollectionClass(),
getQuery());
               }
               else
               {
  -                result = (Collection) getCollectionClass().newInstance();
  +                result = (Collection)getCollectionClass().newInstance();
               }
               return result;
           }
  @@ -123,26 +162,36 @@
           }
       }
   
  +    /**
  +     * Notifies all listeners that the data is about to be loaded.
  +     */
       protected void beforeLoading()
       {
  -        if (m_listeners != null)
  +        if (_listeners != null)
           {
  -            for (int i = m_listeners.size() - 1; i >= 0; i--)
  +            CollectionProxyListener listener;
  +
  +            for (int idx = _listeners.size() - 1; idx >= 0; idx--)
               {
  -                CollectionProxyListener l = (CollectionProxyListener) m_listeners.get(i);
  -                l.beforeLoading(this);
  +                listener = (CollectionProxyListener)_listeners.get(idx);
  +                listener.beforeLoading(this);
               }
           }
       }
   
  +    /**
  +     * Notifies all listeners that the data has been loaded.
  +     */
       protected void afterLoading()
       {
  -        if (m_listeners != null)
  +        if (_listeners != null)
           {
  -            for (int i = m_listeners.size() - 1; i >= 0; i--)
  +            CollectionProxyListener listener;
  +
  +            for (int idx = _listeners.size() - 1; idx >= 0; idx--)
               {
  -                CollectionProxyListener l = (CollectionProxyListener) m_listeners.get(i);
  -                l.afterLoading(this);
  +                listener = (CollectionProxyListener)_listeners.get(idx);
  +                listener.afterLoading(this);
               }
           }
       }
  @@ -158,11 +207,11 @@
           }
           else
           {
  -            if (m_size < 0)
  +            if (_size < 0)
               {
                   loadSize();
               }
  -            return m_size;
  +            return _size;
           }
       }
   
  @@ -255,7 +304,7 @@
       }
   
       /**
  -     * Clear the proxy. A cleared proxy is defined as loaded
  +     * Clears the proxy. A cleared proxy is defined as loaded
        *
        * @see Collection#clear()
        */
  @@ -273,31 +322,32 @@
               coll = new ArrayList();
           }
           setData(coll);
  -        m_size = 0;
  +        _size = 0;
   
       }
   
       /**
  -     * Gets the query.
  -     * @return Returns a Query
  +     * Returns the defining query.
  +     * 
  +     * @return The query
        */
       public Query getQuery()
       {
  -        return m_query;
  +        return _query;
       }
   
       /**
  -     * Sets the query.
  -     * @param query The query to set
  +     * Sets the defining query.
  +     * 
  +     * @param query The query
        */
       protected void setQuery(Query query)
       {
  -        this.m_query = query;
  +        _query = query;
       }
   
  -
       /**
  -     * release the PersistenceBroker
  +     * Release the broker instance.
        */
       protected synchronized void releaseBroker()
       {
  @@ -307,22 +357,23 @@
           it from the PBF, do nothing if we obtain it from
           PBThreadMapping
           */
  -        if (m_broker != null && needsClose)
  +        if (_broker != null && _needsClose)
           {
  -            needsClose = false;
  -            m_broker.close();
  -            m_broker = null;
  +            _needsClose = false;
  +            _broker.close();
  +            _broker = null;
           }
       }
   
       /**
  -     * Gets the broker.
  -     * If no PBKey is available a runtime exception will be thrown.
  -     * @return a PersistenceBroker
  +     * Acquires a broker instance. If no PBKey is available a runtime exception will be
thrown.
  +     * 
  +     * @return A broker instance
        */
       protected synchronized PersistenceBroker getBroker() throws PBFactoryException
       {
           PersistenceBroker broker;
  +
           if (getBrokerKey() == null)
           {
               /*
  @@ -331,27 +382,28 @@
               know which PB (connection) should be used.
               */
               throw new OJBRuntimeException("Can't find associated PBKey. Need PBKey to obtain
a valid" +
  -                    "PersistenceBroker instance from intern resources.");
  +                                          "PersistenceBroker instance from intern resources.");
           }
           // first try to use the current threaded broker to avoid blocking
           broker = PersistenceBrokerThreadMapping.currentPersistenceBroker(getBrokerKey());
           // current broker not found, create a intern new one
  -        if(broker == null)
  +        if (broker == null)
           {
  -            if (m_broker == null)
  +            if (_broker == null)
               {
  -                m_broker = PersistenceBrokerFactory.createPersistenceBroker(getBrokerKey());
  +                _broker = PersistenceBrokerFactory.createPersistenceBroker(getBrokerKey());
                   // TODO: Better way?
  -                needsClose = true;
  -                broker = m_broker;
  +                _needsClose = true;
  +                broker      = _broker;
               }
           }
           return broker;
       }
   
       /**
  -     * Gets the data, load it if not already done.
  -     * @return Returns a Collection
  +     * Returns the collection data, load it if not already done so.
  +     * 
  +     * @return The data
        */
       public synchronized Collection getData()
       {
  @@ -362,38 +414,41 @@
               afterLoading();
           }
   
  -        return m_data;
  +        return _data;
       }
   
       /**
  -     * Sets the data.
  -     * @param data The data to set
  +     * Sets the collection data.
  +     * 
  +     * @param data The data
        */
       public void setData(Collection data)
       {
  -        this.m_data = data;
  +        _data = data;
       }
   
       /**
  -     * Gets the collectionClass.
  -     * @return Returns a Class
  +     * Returns the collection type.
  +     * 
  +     * @return The collection type
        */
       public Class getCollectionClass()
       {
  -        return m_collectionClass;
  +        return _collectionClass;
       }
   
       /**
  -     * Sets the collectionClass.
  -     * @param collectionClass The collectionClass to set
  +     * Sets the collection type.
  +     * 
  +     * @param collClass The collection type
        */
  -    protected void setCollectionClass(Class collectionClass)
  +    protected void setCollectionClass(Class collClass)
       {
  -        this.m_collectionClass = collectionClass;
  +        _collectionClass = collClass;
       }
   
       /**
  -     * @see ManageableCollection#ojbAdd(Object)
  +     * @see org.apache.ojb.broker.ManageableCollection#ojbAdd(Object)
        */
       public void ojbAdd(Object anObject)
       {
  @@ -401,15 +456,15 @@
       }
   
       /**
  -     * @see ManageableCollection#ojbAddAll(ManageableCollection)
  +     * @see org.apache.ojb.broker.ManageableCollection#ojbAddAll(ManageableCollection)
        */
       public void ojbAddAll(ManageableCollection otherCollection)
       {
  -        addAll((CollectionProxyDefaultImpl) otherCollection);
  +        addAll((CollectionProxyDefaultImpl)otherCollection);
       }
   
       /**
  -     * @see ManageableCollection#ojbIterator()
  +     * @see org.apache.ojb.broker.ManageableCollection#ojbIterator()
        */
       public Iterator ojbIterator()
       {
  @@ -424,42 +479,57 @@
           // If the real subject is a ManageableCollection
           // the afterStore() callback must be invoked !
           Collection c = getData();
  +
           if (c instanceof ManageableCollection)
  -            ((ManageableCollection) c).afterStore(broker);
  +        {
  +            ((ManageableCollection)c).afterStore(broker);
  +        }
       }
   
       /**
  -     * Returns the brokerKey.
  -     * @return PBKey
  +     * Returns the key of the persistence broker used by this collection.
  +     * 
  +     * @return The broker key
        */
       public PBKey getBrokerKey()
       {
  -        return m_brokerKey;
  +        return _brokerKey;
       }
   
       /**
  -     * Sets the brokerKey.
  -     * @param brokerKey The brokerKey to set
  +     * Sets the key of the persistence broker used by this collection.
  +     * 
  +     * @param brokerKey The key of the broker
        */
       protected void setBrokerKey(PBKey brokerKey)
       {
  -        this.m_brokerKey = brokerKey;
  +        _brokerKey = brokerKey;
       }
   
  -    public synchronized void addListener(CollectionProxyListener l)
  +    /**
  +     * Adds a listener to this collection.
  +     * 
  +     * @param listener The listener to add
  +     */
  +    public synchronized void addListener(CollectionProxyListener listener)
       {
  -        if (m_listeners == null)
  +        if (_listeners == null)
           {
  -            m_listeners = new ArrayList();
  +            _listeners = new ArrayList();
           }
  -        m_listeners.add(l);
  +        _listeners.add(listener);
       }
   
  -    public synchronized void removeListener(CollectionProxyListener l)
  +    /**
  +     * Removes the given listener from this collecton.
  +     * 
  +     * @param listener The listener to remove
  +     */
  +    public synchronized void removeListener(CollectionProxyListener listener)
       {
  -        if (m_listeners != null)
  +        if (_listeners != null)
           {
  -            m_listeners.remove(l);
  +            _listeners.remove(listener);
           }
       }
   
  
  
  
  1.2       +7 -1      db-ojb/src/java/org/apache/ojb/broker/core/proxy/IndirectionHandlerDefaultImpl.java
  
  Index: IndirectionHandlerDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/IndirectionHandlerDefaultImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IndirectionHandlerDefaultImpl.java	9 Apr 2004 13:22:29 -0000	1.1
  +++ IndirectionHandlerDefaultImpl.java	12 Apr 2004 18:03:43 -0000	1.2
  @@ -56,6 +56,12 @@
       */
       private boolean _needsClose;
   
  +    /**
  +     * Creates a new indirection handler for the indicated object.
  +     * 
  +     * @param brokerKey The key of the persistence broker
  +     * @param id        The identity of the subject
  +     */
       public IndirectionHandlerDefaultImpl(PBKey brokerKey, Identity id)
       {
           setBrokerKey(brokerKey);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message