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

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

public class EagerForeignCollection<T,ID>
extends BaseForeignCollection<T,ID>
implements ForeignCollection<T>, CloseableWrappedIterable<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).

Author:
graywatson
See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.j256.ormlite.dao.BaseForeignCollection
dao
 
Constructor Summary
EagerForeignCollection(Dao<T,ID> dao, Object parent, Object parentId, FieldType foreignFieldType, String orderColumn, boolean orderAscending)
           
 
Method Summary
 boolean add(T data)
          Add an element to the collection.
 boolean addAll(Collection<? extends T> collection)
          Add the collection of elements to this collection.
 void clear()
          Clears the collection and uses the iterator to run through the dao and delete all of the items in the collection from the associated database table.
 void close()
          This will close the last iterator returned by the Iterable.iterator() method.
 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 o)
           
 boolean containsAll(Collection<?> c)
           
 boolean equals(Object obj)
          This is just a call to the equals method of the internal results list.
 CloseableWrappedIterable<T> getWrappedIterable()
          This makes a one time use iterable class that can be closed afterwards.
 int hashCode()
          This is just a call to the hashcode method of the internal results list.
 boolean isEager()
          Returns true if this an eager collection otherwise false.
 boolean isEmpty()
           
 CloseableIterator<T> iterator()
           
 CloseableIterator<T> iteratorThrow()
          Like Collection.iterator() but returns a closeable iterator instead and can throw a SQLException.
 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.
 boolean retainAll(Collection<?> collection)
          Uses the iterator to run through the dao and retain only the items that are in the passed in collection.
 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
getPreparedQuery, refresh, 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
 

Constructor Detail

EagerForeignCollection

public EagerForeignCollection(Dao<T,ID> dao,
                              Object parent,
                              Object parentId,
                              FieldType foreignFieldType,
                              String orderColumn,
                              boolean orderAscending)
                       throws SQLException
Throws:
SQLException
Method Detail

iterator

public CloseableIterator<T> iterator()
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()
Description copied from interface: ForeignCollection
Like Collection.iterator() but returns a closeable iterator instead and can throw a SQLException.

Specified by:
iteratorThrow in interface ForeignCollection<T>

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>

close

public void close()
Description copied from interface: CloseableWrappedIterable
This will close the last iterator returned by the Iterable.iterator() method.

Specified by:
close in interface CloseableWrappedIterable<T>

closeLastIterator

public void closeLastIterator()
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>

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 o)
Specified by:
contains in interface Collection<T>

containsAll

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

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>

add

public boolean add(T data)
Description copied from class: BaseForeignCollection
Add an element to the collection. This will also add the item to the associated database table.

Specified by:
add in interface ForeignCollection<T>
Specified by:
add in interface Collection<T>
Overrides:
add in class BaseForeignCollection<T,ID>
Returns:
Returns true if the item did not already exist in the collection otherwise false.
See Also:
Collection.add(Object)

addAll

public boolean addAll(Collection<? extends T> collection)
Description copied from class: BaseForeignCollection
Add the collection of elements to this collection. This will also them to the associated database table.

Specified by:
addAll in interface Collection<T>
Overrides:
addAll in class BaseForeignCollection<T,ID>
Returns:
Returns true if the item did not already exist in the collection otherwise false.

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.

retainAll

public boolean retainAll(Collection<?> collection)
Description copied from class: BaseForeignCollection
Uses the iterator to run through the dao and retain only the items that are in the passed in collection. This will remove the items from the associated database table as well.

Specified by:
retainAll in interface Collection<T>
Overrides:
retainAll in class BaseForeignCollection<T,ID>
Returns:
Returns true of the collection was changed at all otherwise false.

clear

public void clear()
Description copied from class: BaseForeignCollection
Clears the collection and uses the iterator to run through the dao and delete all of the items in the collection from the associated database table. This is different from removing all of the elements in the table since this iterator is across just one item's foreign objects.

Specified by:
clear in interface Collection<T>
Overrides:
clear in class BaseForeignCollection<T,ID>

updateAll

public int updateAll()
              throws SQLException
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.
Throws:
SQLException

refreshAll

public int refreshAll()
               throws SQLException
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.
Throws:
SQLException

refreshCollection

public int refreshCollection()
                      throws SQLException
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.
Throws:
SQLException

equals

public boolean equals(Object obj)
This is just a call to the equals method of the internal results list.

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

hashCode

public int hashCode()
This is just a call to the hashcode method of the internal results list.

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.