org.apache.commons.clazz.reflect.common
Class ReflectedList

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.AbstractList
          extended by org.apache.commons.clazz.reflect.common.ReflectedList
All Implemented Interfaces:
Iterable, Collection, List

public class ReflectedList
extends AbstractList

This is an implementation of the List interface that is based on a List property. Whenever possible, it uses concrete methods on the owner of the property to manipulate the list or array.

Consider the following example:

      List list = (List)clazz.getProperty("fooList").get(instance);
      Object value = list.get(3);
 
If instance has a getFoo(int index) method, this code will implicitly invoke it like this: getFoo(3), otherwise it will obtain the whole list or array and extract the requested element.

Version:
$Id: ReflectedList.java 155436 2005-02-26 13:17:48Z dirkv $
Author:
Dmitri Plotnikov

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ReflectedList(Object instance, ReflectedListProperty property)
          Constructor for ReflectedList.
 
Method Summary
 void add(int index, Object element)
          Will perform the following steps: If the instance has an add(index,element), calls that method. Othewise, if the instance has an add(element) method and index == size(), calls that method. Othewise, if the instance has a List getFoo [plural suffix]() method, calls that and inserts the element into the list. Othewise, if the instance has a Foo[] getFoo[plural suffix] () method as well as a setFoo[plural suffix] (Foo[]) method, calls the read method, copies the array into a new, one-longer, array inserting the additional element and calls the write method to assign the new array to the property.
 boolean add(Object element)
          Will perform the following steps: If the instance has an addFoo(element), calls that method. Otherwise, if the instance has an add(index,element), computes the size of the list and calls add(size(),element) . Othewise, if the instance has a List getFoo[plural suffix] () method, calls that and adds the element to the list. Othewise, if the instance has a Foo[] getFoo[plural suffix] () method as well as a setFoo[plural suffix] (Foo[]) method, calls the read method, copies the array into a new array with an additional element and calls the write method to assign the new array to the property.
 Object get(int index)
           
 Object getPropertyValue()
           
 Iterator iterator()
           
 ListIterator listIterator()
           
 ListIterator listIterator(int index)
           
 Object remove(int index)
          Will perform the following steps: If the instance has a remove(index), calls that method.
 boolean remove(Object element)
          Will perform the following steps: If the instance has an removeFoo(element), calls that method. Otherwise, if iterates over elements of the collection until it finds one equal to the supplied value.
 Object set(int index, Object element)
           
 void setPropertyValue(Object value)
           
 int size()
           
 
Methods inherited from class java.util.AbstractList
addAll, clear, equals, hashCode, indexOf, lastIndexOf, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

ReflectedList

public ReflectedList(Object instance,
                     ReflectedListProperty property)
Constructor for ReflectedList.

Method Detail

getPropertyValue

public Object getPropertyValue()

setPropertyValue

public void setPropertyValue(Object value)

size

public int size()
Specified by:
size in interface Collection
Specified by:
size in interface List
Specified by:
size in class AbstractCollection
See Also:
Collection.size()

get

public Object get(int index)
Specified by:
get in interface List
Specified by:
get in class AbstractList
See Also:
List.get(int)

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface List
Overrides:
iterator in class AbstractList
See Also:
Collection.iterator()

listIterator

public ListIterator listIterator()
Specified by:
listIterator in interface List
Overrides:
listIterator in class AbstractList
See Also:
List.listIterator()

listIterator

public ListIterator listIterator(int index)
Specified by:
listIterator in interface List
Overrides:
listIterator in class AbstractList
See Also:
List.listIterator(int)

set

public Object set(int index,
                  Object element)
Specified by:
set in interface List
Overrides:
set in class AbstractList
See Also:
List.set(int, java.lang.Object)

add

public boolean add(Object element)
Will perform the following steps:
  1. If the instance has an addFoo(element), calls that method.
  2. Otherwise, if the instance has an add(index,element), computes the size of the list and calls add(size(),element) .
  3. Othewise, if the instance has a List getFoo[plural suffix] () method, calls that and adds the element to the list.
  4. Othewise, if the instance has a Foo[] getFoo[plural suffix] () method as well as a setFoo[plural suffix] (Foo[]) method, calls the read method, copies the array into a new array with an additional element and calls the write method to assign the new array to the property.

Specified by:
add in interface Collection
Specified by:
add in interface List
Overrides:
add in class AbstractList
See Also:
Collection.add(java.lang.Object)

add

public void add(int index,
                Object element)
Will perform the following steps:
  1. If the instance has an add(index,element), calls that method.
  2. Othewise, if the instance has an add(element) method and index == size(), calls that method.
  3. Othewise, if the instance has a List getFoo [plural suffix]() method, calls that and inserts the element into the list.
  4. Othewise, if the instance has a Foo[] getFoo[plural suffix] () method as well as a setFoo[plural suffix] (Foo[]) method, calls the read method, copies the array into a new, one-longer, array inserting the additional element and calls the write method to assign the new array to the property.

Specified by:
add in interface List
Overrides:
add in class AbstractList
See Also:
List.add(int, java.lang.Object)

remove

public boolean remove(Object element)
Will perform the following steps:
  1. If the instance has an removeFoo(element), calls that method.
  2. Otherwise, if iterates over elements of the collection until it finds one equal to the supplied value. Then it removes it by calling remove(index)

Specified by:
remove in interface Collection
Specified by:
remove in interface List
Overrides:
remove in class AbstractCollection
See Also:
Collection.remove(java.lang.Object)

remove

public Object remove(int index)
Will perform the following steps:
  1. If the instance has a remove(index), calls that method.
  2. Othewise, if the instance has an add(element) method and index == size(), calls that method.
  3. Othewise, if the instance has a List getFoo [plural suffix]() method, calls that and removes the element from the list.
  4. Othewise, if the instance has a Foo[] getFoo[plural suffix] () method as well as a setFoo[plural suffix] (Foo[]) method, calls the read method, copies the array into a new, one-shorter, array removing the supplied element and calls the write method to assign the new array to the property.

Specified by:
remove in interface List
Overrides:
remove in class AbstractList
See Also:
List.add(int, java.lang.Object)


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.