org.javalite.activejdbc
Class LazyList<T extends Model>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<T>
          extended by org.javalite.activejdbc.LazyList<T>
All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>
Direct Known Subclasses:
SuperLazyList

public class LazyList<T extends Model>
extends AbstractList<T>

While this class is public, it is never instantiated directly. This class provides a number of APIs for augmenting the query.

Author:
Igor Polevoy

Field Summary
protected  ArrayList<T> delegate
           
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
protected LazyList()
           
protected LazyList(boolean forPaginator, MetaModel metaModel, String fullQuery, Object[] params)
           
protected LazyList(String subQuery, Object[] params, MetaModel metaModel)
           
 
Method Summary
 void add(int index, T element)
           
 boolean add(T o)
           
 boolean addAll(Collection c)
           
 boolean addAll(int index, Collection c)
           
 void clear()
           
 List collect(String columnName)
          Collects values from a result set that correspond to a column name.
 List collect(String columnName, String filterColumn, Object filterValue)
           
 boolean contains(Object o)
           
 boolean containsAll(Collection c)
           
 void dump()
          Dumps contents of this list to System.out.
 void dump(OutputStream out)
          Dumps content of list to a stream.
 T get(int index)
           
 int hashCode()
          This is only to test caching.
protected  void hydrate()
           
<E extends Model>
LazyList<E>
include(Class<? extends Model>... classes)
          Use this method includes associated objects.
 int indexOf(Object o)
           
 boolean isEmpty()
           
 Iterator<T> iterator()
           
 int lastIndexOf(Object o)
           
<E extends Model>
LazyList<E>
limit(long limit)
          This method limits the number of results in the resultset.
 ListIterator<T> listIterator()
           
 ListIterator<T> listIterator(int index)
           
<E extends Model>
LazyList<E>
load()
          This method exists to force immediate load from DB.
<E extends Model>
LazyList<E>
offset(long offset)
          This method sets an offset of a resultset.
<E extends Model>
LazyList<E>
orderBy(String orderBy)
          Use this method to order results by a column.
 T remove(int index)
           
 boolean remove(Object o)
           
 boolean removeAll(Collection c)
           
 boolean retainAll(Collection c)
           
 T set(int index, T element)
           
 int size()
           
 List<T> subList(int fromIndex, int toIndex)
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toJson(boolean pretty, String... attrs)
          Generates JSON from content of this list
 List<Map> toMaps()
          Converts the resultset to list of maps, where each map represents a row in the resultset keyed off column names.
 String toSql()
          Same as toSql(true), see toSql(boolean);
 String toSql(boolean showParameters)
          Use to see what SQL will be sent to the database.
 String toString()
           
 String toXml(int spaces, boolean declaration, String... attrs)
          Generates a XML document from content of this list.
 
Methods inherited from class java.util.AbstractList
equals, removeRange
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

delegate

protected ArrayList<T extends Model> delegate
Constructor Detail

LazyList

protected LazyList(String subQuery,
                   Object[] params,
                   MetaModel metaModel)

LazyList

protected LazyList(boolean forPaginator,
                   MetaModel metaModel,
                   String fullQuery,
                   Object[] params)
Parameters:
metaModel -
fullQuery -
forPaginator - true is this list should not check usage of limit() and offset() methods.
params -

LazyList

protected LazyList()
Method Detail

limit

public <E extends Model> LazyList<E> limit(long limit)
This method limits the number of results in the resultset. It can be used in combination with the offset like this: List<Event> events = Event.find("mnemonic = ?", "GLUC").offset(101).limit(20).orderBy("history_event_id"); This will produce 20 records, starting from record 101. This is an efficient method, it will only retrieve records that are necessary.

Parameters:
limit - how many records to retrieve.
Returns:
instance of this LazyList

offset

public <E extends Model> LazyList<E> offset(long offset)
This method sets an offset of a resultset. For instance, if the offset is 101, then the resultset will skip the first 100 records. It can be used in combination wit the limit like this: List events = Event.find("mnemonic = ?", "GLUC").offset(101).limit(20).orderBy("history_event_id"); This will produce 20 records, starting from record 101. This is an efficient method, it will only retrieve records that are necessary.

Parameters:
offset -
Returns:
instance of this LazyList

orderBy

public <E extends Model> LazyList<E> orderBy(String orderBy)
Use this method to order results by a column. These methods can be chained: Person.find(...).orderBy("department").orderBy("age")

Parameters:
orderBy - order by clause. Examples: "department", "age desc", etc.
Returns:
instance of this LazyList

include

public <E extends Model> LazyList<E> include(Class<? extends Model>... classes)
Use this method includes associated objects. This method will eagerly load associated models of models selected by the query. For instance, if there are models Author, Post and Comment, where Author has many Posts and Post has many Comments, then this query:
 List todayPosts = Post.where("post_date = ?", today).include(Author.class, Comment.class);
 
will generate only three queries to database - one per model. All the dependencies (includes) will be eagerly loaded, and iteration via the todayPosts list will not generate any more queries, even when a post author and comments are requested. Use this with caution as this method can allocate a lot of memory (obviously).

This method will not follow relationships of related models, but rather only relationships of the current one.

Parameters:
classes - list of dependent classes. These classes represent models with which a current model has a relationship.
Returns:
instance of this LazyList

toMaps

public List<Map> toMaps()
Converts the resultset to list of maps, where each map represents a row in the resultset keyed off column names.

Returns:
list of maps, where each map represents a row in the resultset keyed off column names.

toXml

public String toXml(int spaces,
                    boolean declaration,
                    String... attrs)
Generates a XML document from content of this list.

Parameters:
spaces - by how many spaces to indent.
declaration - true to include XML declaration at the top
attrs - list of attributes to include. No arguments == include all attributes.
Returns:
generated XML.

toJson

public String toJson(boolean pretty,
                     String... attrs)
Generates JSON from content of this list

Parameters:
pretty - true if you want pretty format, false if not
attrs - attributes to include, not providing any will include all.
Returns:
generated JSON

load

public <E extends Model> LazyList<E> load()
This method exists to force immediate load from DB. Example; Person.find("name = ?", "Smith").load();. It is not possible to call other methods after load(). The load() method should be the last to be called in the chain: Person.find("name = ?", "Smith").limit(10).load();. This: will generate exception: Person.find("name = ?", "Smith").load().limit();.

Returns:
fully loaded list.

toSql

public String toSql()
Same as toSql(true), see toSql(boolean);

Returns:
SQL in a dialect for current connection which will be used if you start querying this list.

toSql

public String toSql(boolean showParameters)
Use to see what SQL will be sent to the database.

Parameters:
showParameters - true to see parameter values, false not to.
Returns:
SQL in a dialect for current connection which will be used if you start querying this list.

hydrate

protected void hydrate()

collect

public List collect(String columnName)
Collects values from a result set that correspond to a column name. For example, if a list contains collection of Person models, then you can collect first names like this:
 List firstNames = Person.findAll().collect("first_name");
 
provided that the corresponding table has a column first_name.

Keep in mind, that if all you need is a one column data, this method of getting it is not the most efficient (because since you are using a model, you will query all columns from a table, but will use only one). In these cases, you might want to consider Base.firstColumn(String, Object...) and DB.firstColumn(String, Object...).

Parameters:
columnName - name of column to collect.
Returns:
list of collected values for a column.

collect

public List collect(String columnName,
                    String filterColumn,
                    Object filterValue)

get

public T get(int index)
Specified by:
get in interface List<T extends Model>
Specified by:
get in class AbstractList<T extends Model>

size

public int size()
Specified by:
size in interface Collection<T extends Model>
Specified by:
size in interface List<T extends Model>
Specified by:
size in class AbstractCollection<T extends Model>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<T extends Model>
Specified by:
isEmpty in interface List<T extends Model>
Overrides:
isEmpty in class AbstractCollection<T extends Model>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<T extends Model>
Specified by:
contains in interface List<T extends Model>
Overrides:
contains in class AbstractCollection<T extends Model>

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T extends Model>
Specified by:
iterator in interface Collection<T extends Model>
Specified by:
iterator in interface List<T extends Model>
Overrides:
iterator in class AbstractList<T extends Model>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<T extends Model>
Specified by:
toArray in interface List<T extends Model>
Overrides:
toArray in class AbstractCollection<T extends Model>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<T extends Model>
Specified by:
toArray in interface List<T extends Model>
Overrides:
toArray in class AbstractCollection<T extends Model>

add

public boolean add(T o)
Specified by:
add in interface Collection<T extends Model>
Specified by:
add in interface List<T extends Model>
Overrides:
add in class AbstractList<T extends Model>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<T extends Model>
Specified by:
remove in interface List<T extends Model>
Overrides:
remove in class AbstractCollection<T extends Model>

containsAll

public boolean containsAll(Collection c)
Specified by:
containsAll in interface Collection<T extends Model>
Specified by:
containsAll in interface List<T extends Model>
Overrides:
containsAll in class AbstractCollection<T extends Model>

addAll

public boolean addAll(Collection c)
Specified by:
addAll in interface Collection<T extends Model>
Specified by:
addAll in interface List<T extends Model>
Overrides:
addAll in class AbstractCollection<T extends Model>

addAll

public boolean addAll(int index,
                      Collection c)
Specified by:
addAll in interface List<T extends Model>
Overrides:
addAll in class AbstractList<T extends Model>

removeAll

public boolean removeAll(Collection c)
Specified by:
removeAll in interface Collection<T extends Model>
Specified by:
removeAll in interface List<T extends Model>
Overrides:
removeAll in class AbstractCollection<T extends Model>

retainAll

public boolean retainAll(Collection c)
Specified by:
retainAll in interface Collection<T extends Model>
Specified by:
retainAll in interface List<T extends Model>
Overrides:
retainAll in class AbstractCollection<T extends Model>

clear

public void clear()
Specified by:
clear in interface Collection<T extends Model>
Specified by:
clear in interface List<T extends Model>
Overrides:
clear in class AbstractList<T extends Model>

set

public T set(int index,
             T element)
Specified by:
set in interface List<T extends Model>
Overrides:
set in class AbstractList<T extends Model>

add

public void add(int index,
                T element)
Specified by:
add in interface List<T extends Model>
Overrides:
add in class AbstractList<T extends Model>

remove

public T remove(int index)
Specified by:
remove in interface List<T extends Model>
Overrides:
remove in class AbstractList<T extends Model>

indexOf

public int indexOf(Object o)
Specified by:
indexOf in interface List<T extends Model>
Overrides:
indexOf in class AbstractList<T extends Model>

lastIndexOf

public int lastIndexOf(Object o)
Specified by:
lastIndexOf in interface List<T extends Model>
Overrides:
lastIndexOf in class AbstractList<T extends Model>

listIterator

public ListIterator<T> listIterator()
Specified by:
listIterator in interface List<T extends Model>
Overrides:
listIterator in class AbstractList<T extends Model>

listIterator

public ListIterator<T> listIterator(int index)
Specified by:
listIterator in interface List<T extends Model>
Overrides:
listIterator in class AbstractList<T extends Model>

subList

public List<T> subList(int fromIndex,
                       int toIndex)
Specified by:
subList in interface List<T extends Model>
Overrides:
subList in class AbstractList<T extends Model>

hashCode

public int hashCode()
This is only to test caching.

Specified by:
hashCode in interface Collection<T extends Model>
Specified by:
hashCode in interface List<T extends Model>
Overrides:
hashCode in class AbstractList<T extends Model>
Returns:

toString

public String toString()
Overrides:
toString in class AbstractCollection<T extends Model>

dump

public void dump()
Dumps contents of this list to System.out.


dump

public void dump(OutputStream out)
Dumps content of list to a stream. Use for debugging.

Parameters:
out -


Copyright © 2013. All Rights Reserved.