net.sf.mmm.util.reflect.base
Class CollectionReflectionUtilImpl

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractComponent
      extended by net.sf.mmm.util.component.base.AbstractLoggableComponent
          extended by net.sf.mmm.util.reflect.base.CollectionReflectionUtilImpl
All Implemented Interfaces:
CollectionReflectionUtil

@Singleton
@Named
public class CollectionReflectionUtilImpl
extends AbstractLoggableComponent
implements CollectionReflectionUtil

This is the implementation of the CollectionReflectionUtil interface.

Since:
1.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
getInstance()

Field Summary
private  CollectionFactoryManager collectionFactoryManager
           
static int DEFAULT_MAXIMUM_LIST_GROWTH
          The default value for the maximum growth of the size of an array or List: 128
private static CollectionReflectionUtilImpl instance
           
private  int maximumListGrowth
           
 
Constructor Summary
protected CollectionReflectionUtilImpl()
          The constructor.
 
Method Summary
 Object add(Object arrayOrCollection, Object item)
          This method adds the given item to the given arrayOrCollection.
<C extends Collection>
C
create(Class<C> type)
          This method creates a collection implementing the given collection type.
protected  void doInitialize()
          This method performs the actual initialization.
 Object get(Object arrayOrList, int index)
          This method gets the item at the given index from arrayOrCollection.
 Object get(Object arrayOrList, int index, boolean ignoreIndexOverflow)
          This method gets the item at the given index from arrayOrCollection.
 CollectionFactoryManager getCollectionFactoryManager()
          This method gets the CollectionFactoryManager instance used by this util.
static CollectionReflectionUtilImpl getInstance()
          This method gets the singleton instance of this CollectionReflectionUtilImpl.
 int getMaximumListGrowth()
          This method gets the maximum growth for arrays or Lists.
 int getSize(Object arrayMapOrCollection)
          This method gets the size of the given arrayMapOrCollection.
 boolean isArrayOrList(Object object)
          This method determines if the given object is an array or List.
 Object remove(Object arrayOrCollection, Object item)
          This method removes the given item from the given arrayOrCollection.
 Object set(Object arrayOrList, int index, Object item)
          This method sets the given item at the given index in arrayOrCollection.
 Object set(Object arrayOrList, int index, Object item, GenericBean<Object> arrayReceiver)
          This method sets the given item at the given index in arrayOrCollection.
 Object set(Object arrayOrList, int index, Object item, GenericBean<Object> arrayReceiver, int maximumGrowth)
          This method sets the given item at the given index in arrayOrCollection.
 void setCollectionFactoryManager(CollectionFactoryManager collectionFactoryManager)
          This method sets the CollectionFactoryManager instance to use.
 void setMaximumListGrowth(int maximumListGrowth)
          This method sets the maximumListGrowth.
 Object toArray(Collection<?> collection, Class<?> componentType)
          This method converts the given Collection to an array of the given componentType.
<T> T[]
toArrayTyped(Collection<T> collection, Class<T> componentType)
          This method converts the given Collection to an array of the given componentType.
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent
getLogger, setLogger
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent
doInitialized, getInitializationState, initialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAXIMUM_LIST_GROWTH

public static final int DEFAULT_MAXIMUM_LIST_GROWTH
The default value for the maximum growth of the size of an array or List: 128

See Also:
Constant Field Values

instance

private static CollectionReflectionUtilImpl instance
See Also:
getInstance()

collectionFactoryManager

private CollectionFactoryManager collectionFactoryManager
See Also:
getCollectionFactoryManager()

maximumListGrowth

private int maximumListGrowth
See Also:
getMaximumListGrowth()
Constructor Detail

CollectionReflectionUtilImpl

protected CollectionReflectionUtilImpl()
The constructor.

Method Detail

getInstance

public static CollectionReflectionUtilImpl getInstance()
This method gets the singleton instance of this CollectionReflectionUtilImpl.
This design is the best compromise between easy access (via this indirection you have direct, static access to all offered functionality) and IoC-style design which allows extension and customization.
For IoC usage, simply ignore all static getInstance() methods and construct new instances via the container-framework of your choice (like plexus, pico, springframework, etc.). To wire up the dependent components everything is properly annotated using common-annotations (JSR-250). If your container does NOT support this, you should consider using a better one.

Returns:
the singleton instance.

doInitialize

protected void doInitialize()
This method performs the actual initialization. It is called when AbstractComponent.initialize() is invoked for the first time.
ATTENTION:
When you override this method from a sub-class you need to do a super.AbstractComponent.doInitialize().

Overrides:
doInitialize in class AbstractLoggableComponent

getCollectionFactoryManager

public CollectionFactoryManager getCollectionFactoryManager()
This method gets the CollectionFactoryManager instance used by this util.

Specified by:
getCollectionFactoryManager in interface CollectionReflectionUtil
Returns:
the CollectionFactoryManager instance.

setCollectionFactoryManager

@Inject
public void setCollectionFactoryManager(CollectionFactoryManager collectionFactoryManager)
This method sets the CollectionFactoryManager instance to use.

Parameters:
collectionFactoryManager - is the CollectionFactoryManager instance.

getMaximumListGrowth

public int getMaximumListGrowth()
This method gets the maximum growth for arrays or Lists.

Returns:
the maximumListGrowth
See Also:
set(Object, int, Object, GenericBean), setMaximumListGrowth(int)

setMaximumListGrowth

public void setMaximumListGrowth(int maximumListGrowth)
This method sets the maximumListGrowth.

Parameters:
maximumListGrowth - is the maximumListGrowth to set.

create

public <C extends Collection> C create(Class<C> type)
This method creates a collection implementing the given collection type.

Specified by:
create in interface CollectionReflectionUtil
Type Parameters:
C - is the generic type of the collection.
Parameters:
type - is the type of collection to create. This is either an interface (List, Set, Queue, etc.) or a non-abstract implementation of a Collection.
Returns:
the new instance of the given type.

isArrayOrList

public boolean isArrayOrList(Object object)
This method determines if the given object is an array or List.

Specified by:
isArrayOrList in interface CollectionReflectionUtil
Parameters:
object - is the object to check.
Returns:
true if the given object is an array or List, false otherwise.

getSize

public int getSize(Object arrayMapOrCollection)
            throws NlsIllegalArgumentException
This method gets the size of the given arrayMapOrCollection. If arrayMapOrCollection is an array, then its length is returned. If it is a Map or Collection, its size is returned.

Specified by:
getSize in interface CollectionReflectionUtil
Parameters:
arrayMapOrCollection - the array, Map or Collection.
Returns:
the length or size of arrayMapOrCollection.
Throws:
NlsIllegalArgumentException - if the given arrayMapOrCollection is invalid (null or neither array nor Map or Collection).

get

public Object get(Object arrayOrList,
                  int index)
           throws NlsIllegalArgumentException
This method gets the item at the given index from arrayOrCollection.
It sets ignoreIndexOverflow to true.

Specified by:
get in interface CollectionReflectionUtil
Parameters:
arrayOrList - is the array or List.
index - is the position of the requested item.
Returns:
the item at the given index. May be null if the item itself is null or the index is greater than the size or arrayOrCollection.
Throws:
NlsIllegalArgumentException - if the given arrayOrList is invalid (null or neither array nor List).
See Also:
CollectionReflectionUtil.get(Object, int, boolean)

get

public Object get(Object arrayOrList,
                  int index,
                  boolean ignoreIndexOverflow)
           throws NlsIllegalArgumentException
This method gets the item at the given index from arrayOrCollection.

Specified by:
get in interface CollectionReflectionUtil
Parameters:
arrayOrList - is the array or List.
index - is the position of the requested item.
ignoreIndexOverflow - - if false an the given index is greater or equal to the size of arrayOrCollection an IndexOutOfBoundsException will be thrown. Else if true, null is returned in this case.
Returns:
the item at the given index. May be null if the item itself is null or the index is greater or equal to the size of arrayOrCollection.
Throws:
NlsIllegalArgumentException - if the given arrayOrList is invalid (null or neither array nor List).
See Also:
List.get(int)

set

public Object set(Object arrayOrList,
                  int index,
                  Object item)
           throws NlsIllegalArgumentException
This method sets the given item at the given index in arrayOrCollection. It uses a default value for maximumGrowth and no arrayReceiver ( null).

Specified by:
set in interface CollectionReflectionUtil
Parameters:
arrayOrList - is the array or List.
index - is the position where to set the item.
item - is the item to set.
Returns:
the item at position index in arrayOrList that has been replaced by item. This can be null. Additional if the arrayOrList has been increased, null is returned.
Throws:
NlsIllegalArgumentException - if the given arrayOrList is invalid (null or neither array nor List).
See Also:
CollectionReflectionUtil.set(Object, int, Object, GenericBean, int)

set

public Object set(Object arrayOrList,
                  int index,
                  Object item,
                  GenericBean<Object> arrayReceiver)
           throws NlsIllegalArgumentException
This method sets the given item at the given index in arrayOrCollection. If a List is given that has a size less or equal to the given index, the size of the List will be increased to index + 1 by adding null values so the item can be set. It uses a default value for maximumGrowth.

Specified by:
set in interface CollectionReflectionUtil
Parameters:
arrayOrList - is the array or List.
index - is the position where to set the item.
item - is the item to set.
arrayReceiver - is a GenericBean that allows to receive an array-copy of arrayOrList with an increased length. It can be null to disable array-copying.
Returns:
the item at position index in arrayOrList that has been replaced by item. This can be null. Additional if the arrayOrList has been increased, null is returned.
Throws:
NlsIllegalArgumentException - if the given arrayOrList is invalid (null or neither array nor List).
See Also:
List.set(int, Object), CollectionReflectionUtil.set(Object, int, Object, GenericBean, int)

set

public Object set(Object arrayOrList,
                  int index,
                  Object item,
                  GenericBean<Object> arrayReceiver,
                  int maximumGrowth)
           throws NlsIllegalArgumentException
This method sets the given item at the given index in arrayOrCollection. If a List is given that has a size less or equal to the given index, the size of the List will be increased to index + 1 by adding null values so the item can be set. However the number of adds is limited to maximumGrowth.

Specified by:
set in interface CollectionReflectionUtil
Parameters:
arrayOrList - is the array or List.
index - is the position where to set the item.
item - is the item to set.
arrayReceiver - is a GenericBean that allows to receive an array-copy of arrayOrList with an increased length. It can be null to disable array-copying.
maximumGrowth - is the maximum number by which the size of arrayOrList will be increased (with null values) to reach index + 1 so the item can be set. Set this value to 0 to turn off this feature (and leave the size untouched). Please always specify a real maximum (<=65536) and do NOT use Integer.MAX_VALUE since this might cause memory holes if something goes wrong. If arrayOrList is an array, increasing can only happen by creating a new array. To receive such new array, you need to supply an arrayReceiver. Otherwise (if null) this method behaves for arrays as if maximumGrowth was 0. If an array should be increased, a new array with the size of index + 1 is created. The original items are copied, the given item is set on the copy instead while the original array remains unchanged. Then the new array is set to the arrayReceiver.
Returns:
the item at position index in arrayOrList that has been replaced by item. This can be null. Additional if the arrayOrList has been increased, null is returned.
Throws:
NlsIllegalArgumentException - if the given arrayOrList is invalid (null or neither array nor List).
See Also:
List.set(int, Object)

add

public Object add(Object arrayOrCollection,
                  Object item)
This method adds the given item to the given arrayOrCollection.

Specified by:
add in interface CollectionReflectionUtil
Parameters:
arrayOrCollection - is the array or Collection.
item - is the item to add.
Returns:
the given arrayOrCollection if it was a Collection. Otherwise, in case of an array, a new array with a length increased by 1 and the elements of arrayOrCollection appended with the given item is returned.

remove

public Object remove(Object arrayOrCollection,
                     Object item)
This method removes the given item from the given arrayOrCollection.

Specified by:
remove in interface CollectionReflectionUtil
Parameters:
arrayOrCollection - is the array or Collection.
item - is the item to remove.
Returns:
null if the given item was NOT contained in arrayOrCollection, the given arrayOrCollection if it was a Collection and the item has been removed. Otherwise, in case of an array, a new array with a length decreased by 1 and the elements of arrayOrCollection without the first occurrence of the given item is returned.

toArray

public Object toArray(Collection<?> collection,
                      Class<?> componentType)
               throws ClassCastException
This method converts the given Collection to an array of the given componentType. This method also allows to create primitive arrays. If NOT required please prefer using CollectionReflectionUtil.toArrayTyped(Collection, Class).

Specified by:
toArray in interface CollectionReflectionUtil
Parameters:
collection - is the Collection to convert to an array.
componentType - is the component type of the requested array. If this type is primitive, the according collection-values with be unboxed.
Returns:
the Array of the given componentType and with the values of the given Collection.
Throws:
ClassCastException - if the values of the Collection are NOT compatible with the given componentType.

toArrayTyped

public <T> T[] toArrayTyped(Collection<T> collection,
                            Class<T> componentType)
This method converts the given Collection to an array of the given componentType.

Specified by:
toArrayTyped in interface CollectionReflectionUtil
Type Parameters:
T - is the generic type of the componentType.
Parameters:
collection - is the Collection to convert to an array.
componentType - is the component type of the requested array.
Returns:
the Array of the given componentType and with the values of the given Collection.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.