Dashboard Builder Commons 6.2.0.CR4

org.jboss.dashboard.commons.collections
Class LazyList

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList
              extended by org.jboss.dashboard.commons.collections.LazyList
All Implemented Interfaces:
Serializable, Cloneable, Iterable, Collection, List, RandomAccess

public abstract class LazyList
extends ArrayList

List designed to manage large amount of instances.

Internally, it only stores identifiers (as a list passed at initialization). Instances are only retrieved when invoking the get(int index) method of the list. This load-on-demand technique is also known as lazy materialization.

Instances can be added or removed from list via class methods. You can both add the instance to the list or instance identifier. If you need to retrieve the list of identifiers stored internally use the getIds method which returns an array of strings.

See Also:
Serialized Form

Field Summary
protected  boolean discardDuplicates
          Flag indicating if duplicated elements are allowed.
protected  int fetchSize
          The number of elements fetched when invoking the get(index) method.
protected  List instanceList
          List of instances already retrieved by the lazy calls.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
LazyList(boolean discardDuplicates)
          Constructs an empty list.
LazyList(Collection c, boolean discardDuplicates)
          Constructs a list from the identifiers of the specified collection, in the order they are returned by the collection's iterator.
 
Method Summary
 void add(int index, Object o)
          Inserts the specified element at the specified position in this list.
 boolean add(Object o)
          Appends the specified element to the end of this list.
 boolean addAll(Collection c)
          Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
 boolean addAll(int index, Collection c)
          Not supported.
 boolean contains(Object o)
          Returns true if this list contains the specified element.
abstract  LazyList createLazyList(Collection c)
          Creates a brand new lazy list initialiaed with the given list of identifiers.
 Object get(int index)
          Retrieves the instance at specified position.
 int getFetchSize()
          The number of elements fetched when invoking the get(index) method.
 String getId(int index)
          Retrieve identifier stored at given position.
 String[] getIds()
          Retrieve identifiers stored internally in the lazy list.
abstract  String getInstanceId(Object o)
          Retreives the identifier of an instance.
 int indexOf(Object elem)
          Searches for the first occurence of the given argument, testing for equality using the equals method.
abstract  boolean isInstance(Object o)
          Check if object is an business object or an identifier.
 int lastIndexOf(Object elem)
          Returns the index of the last occurrence of the specified object in this list.
abstract  List loadInstances(String[] ids)
          Loads the instances with the given identifiers.
 boolean remove(Object o)
          Removes the process from the collection
 boolean removeAll(Collection c)
          Removes from this collection all of its elements that are contained in the specified collection (optional operation).
 boolean removeId(String id)
          Remove identifier from list.
 Object set(int index, Object element)
          Replaces the element at the specified position in this list with the specified element.
 void setFetchSize(int fetchSize)
          The number of elements fetched when invoking the get(index) method.
 List subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.
 
Methods inherited from class java.util.ArrayList
clear, clone, ensureCapacity, isEmpty, remove, removeRange, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator
 
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, retainAll
 

Field Detail

instanceList

protected List instanceList
List of instances already retrieved by the lazy calls. It acts as an instance cache and an instance prefetch buffer.


discardDuplicates

protected boolean discardDuplicates
Flag indicating if duplicated elements are allowed.


fetchSize

protected int fetchSize
The number of elements fetched when invoking the get(index) method.

Constructor Detail

LazyList

public LazyList(boolean discardDuplicates)
Constructs an empty list.

Parameters:
discardDuplicates - Ensure list not contains duplicates.

LazyList

public LazyList(Collection c,
                boolean discardDuplicates)
Constructs a list from the identifiers of the specified collection, in the order they are returned by the collection's iterator. The ArrayList instance has an initial capacity of 110% the size of the specified collection.

Parameters:
c - the collection of ids.
discardDuplicates - Ensure list not contains duplicates.
Method Detail

getFetchSize

public int getFetchSize()
The number of elements fetched when invoking the get(index) method.


setFetchSize

public void setFetchSize(int fetchSize)
The number of elements fetched when invoking the get(index) method.


isInstance

public abstract boolean isInstance(Object o)
Check if object is an business object or an identifier.


getInstanceId

public abstract String getInstanceId(Object o)
Retreives the identifier of an instance.

Returns:
An identifier.

loadInstances

public abstract List loadInstances(String[] ids)
Loads the instances with the given identifiers.

Returns:
A list of full instances (no lazy).

createLazyList

public abstract LazyList createLazyList(Collection c)
Creates a brand new lazy list initialiaed with the given list of identifiers.


getIds

public String[] getIds()
Retrieve identifiers stored internally in the lazy list.

Returns:
An array of identifiers.

getId

public String getId(int index)
Retrieve identifier stored at given position.

Parameters:
index -
Returns:
An identifier.

removeId

public boolean removeId(String id)
Remove identifier from list. This implementation is based in the ArrayList.indexOf(String)/remove(index) methods We do not use remove(Object) method because invokes internally to the get(i) method which forces the retrieval of instances and causes a low performace.


get

public Object get(int index)
Retrieves the instance at specified position.

Specified by:
get in interface List
Overrides:
get in class ArrayList
Parameters:
index -
Returns:
A instance.

remove

public boolean remove(Object o)
Removes the process from the collection

Specified by:
remove in interface Collection
Specified by:
remove in interface List
Overrides:
remove in class ArrayList
Parameters:
o - The instance to remove (the identifier is also accepted).
Returns:
true if the collection contained the specified element.

contains

public boolean contains(Object o)
Returns true if this list contains the specified element.

Specified by:
contains in interface Collection
Specified by:
contains in interface List
Overrides:
contains in class ArrayList
Parameters:
o - element whose presence in this List is to be tested.

indexOf

public int indexOf(Object elem)
Searches for the first occurence of the given argument, testing for equality using the equals method.

Specified by:
indexOf in interface List
Overrides:
indexOf in class ArrayList
Parameters:
elem - an object.
Returns:
the index of the first occurrence of the argument in this list; returns -1 if the object is not found.
See Also:
Object.equals(Object)

lastIndexOf

public int lastIndexOf(Object elem)
Returns the index of the last occurrence of the specified object in this list.

Specified by:
lastIndexOf in interface List
Overrides:
lastIndexOf in class ArrayList
Parameters:
elem - the desired element.
Returns:
the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.

add

public boolean add(Object o)
Appends the specified element to the end of this list.

Specified by:
add in interface Collection
Specified by:
add in interface List
Overrides:
add in class ArrayList
Parameters:
o - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

add

public void add(int index,
                Object o)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Specified by:
add in interface List
Overrides:
add in class ArrayList
Parameters:
index - index at which the specified element is to be inserted.
o - element to be inserted. If trying to add an instance then its identifier is added instead of the whole instance.
Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).

addAll

public boolean addAll(Collection c)
Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)

Specified by:
addAll in interface Collection
Specified by:
addAll in interface List
Overrides:
addAll in class ArrayList
Parameters:
c - the collection of both instances or ids. If trying to add an instance then its identifier is added instead of the whole instance.
Throws:
IndexOutOfBoundsException - if index out of range (index < 0 || index > size()).

addAll

public boolean addAll(int index,
                      Collection c)
Not supported.

Specified by:
addAll in interface List
Overrides:
addAll in class ArrayList

removeAll

public boolean removeAll(Collection c)
Removes from this collection all of its elements that are contained in the specified collection (optional operation).

This implementation retrieves the elements from the target collection and performs single remove operations for each one of these elements.

Specified by:
removeAll in interface Collection
Specified by:
removeAll in interface List
Overrides:
removeAll in class AbstractCollection
Parameters:
c - elements to be removed from this collection.
Returns:
true if this collection changed as a result of the call.

set

public Object set(int index,
                  Object element)
Replaces the element at the specified position in this list with the specified element.

Specified by:
set in interface List
Overrides:
set in class ArrayList
Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

subList

public List subList(int fromIndex,
                    int toIndex)
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is new and changes in the returned list are NOT reflected in this list, and vice-versa.

Specified by:
subList in interface List
Overrides:
subList in class AbstractList

Dashboard Builder Commons 6.2.0.CR4

Copyright © 2012-2015 JBoss by Red Hat. All Rights Reserved.