com.j256.ormlite.dao
Class LazyForeignCollection<T,ID>

java.lang.Object
  extended by com.j256.ormlite.dao.BaseForeignCollection<T,ID>
      extended by com.j256.ormlite.dao.LazyForeignCollection<T,ID>
All Implemented Interfaces:
CloseableIterable<T>, ForeignCollection<T>, Serializable, Iterable<T>, Collection<T>

public class LazyForeignCollection<T,ID>
extends BaseForeignCollection<T,ID>
implements ForeignCollection<T>, Serializable

Collection that is set on a field that as been marked with the ForeignCollectionField annotation when an object is refreshed or queried (i.e. not created). Most of the methods here require a pass through the database. Operations such as size() therefore should most likely not be used because of their expense. Chances are you only want to use the iterator(), toArray(), and toArray(Object[]) methods.

WARNING: Most likely for(;;) loops should not be used here since we need to be careful about closing the iterator.

Author:
graywatson
See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.j256.ormlite.dao.BaseForeignCollection
dao
 
Constructor Summary
LazyForeignCollection(Dao<T,ID> dao, Object parent, Object parentId, String columnName, String orderColumn)
           
 
Method Summary
 CloseableIterator<T> closeableIterator()
          Returns an iterator over a set of elements of type T which can be closed.
 void closeLastIterator()
          This will close the last iterator returned by the Collection.iterator() method.
 boolean contains(Object obj)
           
 boolean containsAll(Collection<?> collection)
           
 boolean equals(Object other)
          This is just a call to Object.equals(Object).
 CloseableWrappedIterable<T> getWrappedIterable()
          This makes a one time use iterable class that can be closed afterwards.
 int hashCode()
          This is just a call to Object.hashCode().
 boolean isEager()
          Returns true if this an eager collection otherwise false.
 boolean isEmpty()
           
 CloseableIterator<T> iterator()
          The iterator returned from a lazy collection keeps a connection open to the database as it iterates across the collection.
 CloseableIterator<T> iteratorThrow()
          This is the same as iterator() except it throws.
 int refreshAll()
          Call to refresh on all of the items currently in the collection with the database.
 int refreshCollection()
          This re-issues the query that initially built the collection replacing any underlying result collection with a new one build from the database.
 boolean remove(Object data)
          Remove the item from the collection and the associated database table.
 boolean removeAll(Collection<?> collection)
          Remove the items in the collection argument from the foreign collection and the associated database table.
 CloseableIterator<T> seperateIteratorThrow()
          Same as iteratorThrow() except this doesn't set the last iterator which can be closed with the closeLastIterator().
 int size()
           
 Object[] toArray()
           
<E> E[]
toArray(E[] array)
           
 int updateAll()
          Update all of the items currently in the collection with the database.
 
Methods inherited from class com.j256.ormlite.dao.BaseForeignCollection
add, addAll, clear, getPreparedQuery, refresh, retainAll, update
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.j256.ormlite.dao.ForeignCollection
refresh, update
 
Methods inherited from interface java.util.Collection
add, addAll, clear, retainAll
 

Constructor Detail

LazyForeignCollection

public LazyForeignCollection(Dao<T,ID> dao,
                             Object parent,
                             Object parentId,
                             String columnName,
                             String orderColumn)
Method Detail

iterator

public CloseableIterator<T> iterator()
The iterator returned from a lazy collection keeps a connection open to the database as it iterates across the collection. You will need to call CloseableIterator.close() or go all the way through the loop to ensure that the connection has been closed. You can also call closeLastIterator() on the collection itself which will close the last iterator returned. See the reentrant warning.

Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface Collection<T>

closeableIterator

public CloseableIterator<T> closeableIterator()
Description copied from interface: CloseableIterable
Returns an iterator over a set of elements of type T which can be closed.

Specified by:
closeableIterator in interface CloseableIterable<T>

iteratorThrow

public CloseableIterator<T> iteratorThrow()
                                   throws SQLException
This is the same as iterator() except it throws.

Specified by:
iteratorThrow in interface ForeignCollection<T>
Throws:
SQLException

seperateIteratorThrow

public CloseableIterator<T> seperateIteratorThrow()
                                           throws SQLException
Same as iteratorThrow() except this doesn't set the last iterator which can be closed with the closeLastIterator().

Throws:
SQLException

getWrappedIterable

public CloseableWrappedIterable<T> getWrappedIterable()
Description copied from interface: ForeignCollection
This makes a one time use iterable class that can be closed afterwards. The ForeignCollection itself is CloseableWrappedIterable but multiple threads can each call this to get their own closeable iterable.

Specified by:
getWrappedIterable in interface ForeignCollection<T>

closeLastIterator

public void closeLastIterator()
                       throws SQLException
Description copied from interface: ForeignCollection
This will close the last iterator returned by the Collection.iterator() method.

NOTE: For lazy collections, this is not reentrant. If multiple threads are getting iterators from a lazy collection from the same object then you should use ForeignCollection.getWrappedIterable() to get a reentrant wrapped iterable for each thread instead.

Specified by:
closeLastIterator in interface ForeignCollection<T>
Throws:
SQLException

isEager

public boolean isEager()
Description copied from interface: ForeignCollection
Returns true if this an eager collection otherwise false.

Specified by:
isEager in interface ForeignCollection<T>

size

public int size()
Specified by:
size in interface Collection<T>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<T>

contains

public boolean contains(Object obj)
Specified by:
contains in interface Collection<T>

containsAll

public boolean containsAll(Collection<?> collection)
Specified by:
containsAll in interface Collection<T>

remove

public boolean remove(Object data)
Description copied from class: BaseForeignCollection
Remove the item from the collection and the associated database table. NOTE: we can't just do a dao.delete(data) because it has to be in the collection.

Specified by:
remove in interface Collection<T>
Specified by:
remove in class BaseForeignCollection<T,ID>
Returns:
True if the item was found in the collection otherwise false.

removeAll

public boolean removeAll(Collection<?> collection)
Description copied from class: BaseForeignCollection
Remove the items in the collection argument from the foreign collection and the associated database table. NOTE: we can't just do a for (...) dao.delete(item) because the items have to be in the collection.

Specified by:
removeAll in interface Collection<T>
Specified by:
removeAll in class BaseForeignCollection<T,ID>
Returns:
True if the item was found in the collection otherwise false.

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<T>

toArray

public <E> E[] toArray(E[] array)
Specified by:
toArray in interface Collection<T>

updateAll

public int updateAll()
Description copied from interface: ForeignCollection
Update all of the items currently in the collection with the database. This is only applicable for eager collections.

Specified by:
updateAll in interface ForeignCollection<T>
Returns:
The number of rows updated.

refreshAll

public int refreshAll()
Description copied from interface: ForeignCollection
Call to refresh on all of the items currently in the collection with the database. This is only applicable for eager collections. If you want to see new objects in the collection then you should use ForeignCollection.refreshCollection().

Specified by:
refreshAll in interface ForeignCollection<T>
Returns:
The number of rows refreshed.

refreshCollection

public int refreshCollection()
Description copied from interface: ForeignCollection
This re-issues the query that initially built the collection replacing any underlying result collection with a new one build from the database. This is only applicable for eager collections and is a no-op for lazy collections.

Specified by:
refreshCollection in interface ForeignCollection<T>
Returns:
The number of objects loaded into the new collection.

equals

public boolean equals(Object other)
This is just a call to Object.equals(Object).

NOTE: This method is here for documentation purposes because EagerForeignCollection.equals(Object) is defined.

Specified by:
equals in interface Collection<T>
Overrides:
equals in class Object

hashCode

public int hashCode()
This is just a call to Object.hashCode().

NOTE: This method is here for documentation purposes because EagerForeignCollection.equals(Object) is defined.

Specified by:
hashCode in interface Collection<T>
Overrides:
hashCode in class Object


This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.