classUtils.pack.util
Class ObjectLister

java.lang.Object
  extended by classUtils.pack.util.ObjectLister

public class ObjectLister
extends java.lang.Object

A class to list the elements of an array or a collection/enumeration into a string, each element divided by a separtor, by invoking either the toString() method or a given method with no parameters (and returning a String).

The default list(array) method uses toString to describe each object; the list(array, method name) method attempts to locate a method with signature

  public String <method name>()
 
in each object in the array, and invokes it to describe each object.

For example, for the array String [] array { "Hello", "World" } invoking new ObjectLister().list(array); will produce "Hello, World".

For the array Thread [] array = { new Thread("Thread 1"), new Thread("Thread 2") }, invoking new ObjectLister().list(array, "getName"); will produce "Thread 1, Thread 2".

Author:
Cristiano Sadun

Field Summary
static java.lang.String DEFAULT_SEPARATOR
          The default separator sequence ", "
 
Constructor Summary
ObjectLister()
          Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATOR as separator.
ObjectLister(boolean useToStringIfNotFound)
          Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATOR as separator.
ObjectLister(java.lang.String separator)
          Constructor for ObjectLister.
ObjectLister(java.lang.String separator, boolean useToStringIfNotFound)
          Constructor for ObjectLister.
 
Method Summary
static ObjectLister getInstance()
          Returns the default instance, which uses DEFAULT_SEPARATOR.
 boolean isUseToStringIfNotFound()
          Returns the useToStringIfNotFound.
 java.lang.String list(java.util.Collection coll)
          Invoke list() using the toString() method.
 java.lang.String list(java.util.Collection coll, java.lang.String methodToUse)
          Return a String containing a list of objects in the collection, obtained invoking the given method name on each element in the collection.
 java.lang.String list(java.util.Enumeration e)
          Invoke list() using the toString() method.
 java.lang.String list(java.util.Enumeration e, java.lang.String methodToUse)
          Return a String containing a list of objects in the enumeration, obtained invoking the given method name on each element in the enumeration.
 java.lang.String list(java.util.Map map)
          Invoke list() using the toString() method.
 java.lang.String list(java.util.Map map, java.lang.String methodToUse)
          Return a String containing a list of values in the map, obtained invoking the given method name on each element in the map.
 java.lang.String list(java.lang.Object[] array)
          Invoke list() using the toString() method.
 java.lang.String list(java.lang.Object[] array, java.lang.String methodToUse)
          Return a String containing a list of objects in the array, obtained invoking the given method name on each element in the array.
static void main(java.lang.String[] args)
          A test method
 void setUseToStringIfNotFound(boolean useToStringIfNotFound)
          Sets the useToStringIfNotFound.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SEPARATOR

public static final java.lang.String DEFAULT_SEPARATOR
The default separator sequence ", "

See Also:
Constant Field Values
Constructor Detail

ObjectLister

public ObjectLister(java.lang.String separator,
                    boolean useToStringIfNotFound)
Constructor for ObjectLister.

Parameters:
separator - the separator to use
useToStringIfNotFound - if true, when list(array, method name) is invoked, toString() will be used if the given method is not found in an object in the array to list. If false an exception will be raised.

ObjectLister

public ObjectLister(boolean useToStringIfNotFound)
Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATOR as separator.

Parameters:
useToStringIfNotFound - if true, when list(array, method name) is invoked, toString() will be used if the given method is not found in an object in the array to list. If false an exception will be raised.

ObjectLister

public ObjectLister(java.lang.String separator)
Constructor for ObjectLister. When list(array, method name) is invoked, toString() will be used if the given method is not found in an object in the array to list.

Parameters:
separator - the separator to use

ObjectLister

public ObjectLister()
Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATOR as separator. When list(array, method name) is invoked, toString() will be used if the given method is not found in an object in the array to list.

Method Detail

list

public java.lang.String list(java.lang.Object[] array,
                             java.lang.String methodToUse)
Return a String containing a list of objects in the array, obtained invoking the given method name on each element in the array.

The method must return a String and have no parameters.

Parameters:
array - the array to list
methodToUse - the name of the method to use
Returns:
a String containing a list of objects in the array
Throws:
java.lang.RuntimeException - if a method with the given name, which returns a String and has no parameter is not available in the objects of the array.

list

public java.lang.String list(java.lang.Object[] array)
Invoke list() using the toString() method.

Parameters:
array - to be listed
Returns:
a string listing the array

list

public java.lang.String list(java.util.Collection coll,
                             java.lang.String methodToUse)
Return a String containing a list of objects in the collection, obtained invoking the given method name on each element in the collection.

The method must return a String and have no parameters.

Parameters:
coll - the collection to list
methodToUse - the name of the method to use
Returns:
a String containing a list of objects in the collection
Throws:
java.lang.RuntimeException - if a method with the given name, which returns a String and has no parameter is not available in the objects of the collection.

list

public java.lang.String list(java.util.Collection coll)
Invoke list() using the toString() method.

Parameters:
coll - to be listed
Returns:
a string listing the collection

list

public java.lang.String list(java.util.Enumeration e,
                             java.lang.String methodToUse)
Return a String containing a list of objects in the enumeration, obtained invoking the given method name on each element in the enumeration.

The method must return a String and have no parameters.

Parameters:
e - the enumeration to list
methodToUse - the name of the method to use
Returns:
a String containing a list of objects in the enumeration
Throws:
java.lang.RuntimeException - if a method with the given name, which returns a String and has no parameter is not available in the objects of the enumeration.

list

public java.lang.String list(java.util.Enumeration e)
Invoke list() using the toString() method.

Parameters:
e - the enumeration to be listed
Returns:
a string listing the enumeration

list

public java.lang.String list(java.util.Map map,
                             java.lang.String methodToUse)
Return a String containing a list of values in the map, obtained invoking the given method name on each element in the map.

The method must return a String and have no parameters.

Parameters:
map - the map to list
methodToUse - the name of the method to use
Returns:
a String containing a list of values in the map
Throws:
java.lang.RuntimeException - if a method with the given name, which returns a String and has no parameter is not available in the objects of the map.

list

public java.lang.String list(java.util.Map map)
Invoke list() using the toString() method.

Parameters:
map - to be listed
Returns:
a string listing the map

isUseToStringIfNotFound

public boolean isUseToStringIfNotFound()
Returns the useToStringIfNotFound.

Returns:
boolean

setUseToStringIfNotFound

public void setUseToStringIfNotFound(boolean useToStringIfNotFound)
Sets the useToStringIfNotFound.

Parameters:
useToStringIfNotFound - The useToStringIfNotFound to set

main

public static void main(java.lang.String[] args)
A test method


getInstance

public static ObjectLister getInstance()
Returns the default instance, which uses DEFAULT_SEPARATOR.

Returns:
ObjectLister the default object lister.