org.sadun.util
Class ObjectLister

java.lang.Object
  extended byorg.sadun.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".

By default, the method to invoke is made accessible to the client code regardless of its protection status. Use setSetAccessible() to disable such behaviour. If disabled, the failOnUnaccessibleproperty may be used to cause the list to raise an excepion when a method is not accessible (by default, an explicit mention of the fact will appear in the list).

Author:
Cristiano Sadun

Field Summary
static char DEFAULT_QUOTE_CHAR
          The default quote char, '"e;'
static java.lang.String DEFAULT_SEPARATOR
          The default separator sequence ", "
 
Constructor Summary
ObjectLister()
          Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATORas separator.
ObjectLister(boolean useToStringIfNotFound)
          Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATORas separator.
ObjectLister(char quoteChar)
          Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATORas separator.
ObjectLister(java.lang.String separator)
          Constructor for ObjectLister.
ObjectLister(java.lang.String separator, boolean useToStringIfNotFound)
          Constructor for ObjectLister, which does not fail if the method to use is not accessible.
ObjectLister(java.lang.String separator, boolean useToStringIfNotFound, boolean failIfUnaccessible)
          Constructor for ObjectLister.
ObjectLister(java.lang.String separator, char quoteChar)
          Constructor for ObjectLister.
ObjectLister(java.lang.String separator, char quoteChar, boolean useToStringIfNotFound)
          Constructor for ObjectLister, which does not fail if the method to use is not accessible.
ObjectLister(java.lang.String separator, char quoteChar, boolean useToStringIfNotFound, boolean failIfUnaccessible)
          Constructor for ObjectLister.
 
Method Summary
static ObjectLister getDefaultHTMLInstance()
          Return the default instance separating listed elements with a &ĝlt;br> HTML element
static ObjectLister getInstance()
          Returns the default instance, which uses DEFAULT_SEPARATOR.
 boolean isFailOnUnaccessible()
           
 boolean isSetAccessible()
           
 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 enum)
          Invoke list() using the toString() method.
 java.lang.String list(java.util.Enumeration enum, 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.Iterator iterator)
          Invoke list() using the toString() method.
 java.lang.String list(java.util.Iterator iterator, java.lang.String methodToUse)
          Return a String containing a list of objects in the iterator, obtained invoking the given method name on each element.
 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 println(java.util.Collection coll)
          Invoke list()and print the result on System.out.
 void println(java.util.Collection coll, java.lang.String methodToUse)
          Invoke list()and print the result on System.out.
 void println(java.util.Enumeration enum)
          Invoke list()and print the result on System.out.
 void println(java.util.Enumeration enum, java.lang.String methodToUse)
          Invoke list()and print the result on System.out.
 void println(java.util.Map map)
          Invoke list()and print the result on System.out.
 void println(java.util.Map map, java.lang.String methodToUse)
          Invoke list()and print the result on System.out.
 void println(java.lang.Object[] array)
          Invoke list()and print the result on System.out.
 void println(java.lang.Object[] array, java.lang.String methodToUse)
          Invoke list()and print the result on System.out.
 void setFailOnUnaccessible(boolean failOnUnaccessible)
           
 void setSetAccessible(boolean setAccessible)
           
 void setUseToStringIfNotFound(boolean useToStringIfNotFound)
          Sets the useToStringIfNotFound.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_QUOTE_CHAR

public static final char DEFAULT_QUOTE_CHAR
The default quote char, '"e;'

See Also:
Constant Field Values

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,
                    char quoteChar,
                    boolean useToStringIfNotFound,
                    boolean failIfUnaccessible)
Constructor for ObjectLister.

Parameters:
separator - the separator to use
quoteChar - the quotation charachter to use. If 0, no quotes are used.
useToStringIfNotFound - if true, when {@link #list(java.lang.Object[], java.lang.String)
failIfUnaccessible - if true an exception is thrown if the method to use for listing is not accessible. 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,
                    boolean useToStringIfNotFound,
                    boolean failIfUnaccessible)
Constructor for ObjectLister.

Parameters:
separator - the separator to use
useToStringIfNotFound - if true, when {@link #list(java.lang.Object[], java.lang.String)
failIfUnaccessible - if true an exception is thrown if the method to use for listing is not accessible. 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,
                    boolean useToStringIfNotFound)
Constructor for ObjectLister, which does not fail if the method to use is not accessible.

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(java.lang.String separator,
                    char quoteChar,
                    boolean useToStringIfNotFound)
Constructor for ObjectLister, which does not fail if the method to use is not accessible.

Parameters:
separator - the separator to use
quoteChar - the quotation charachter to use. If 0, no quotes are used.
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_SEPARATORas 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(java.lang.String separator,
                    char quoteChar)
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
quoteChar - the quotation charachter to use. If 0, no quotes are used.

ObjectLister

public ObjectLister()
Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATORas 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.


ObjectLister

public ObjectLister(char quoteChar)
Constructor for ObjectLister, which uses the default sequence DEFAULT_SEPARATORas 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.

Parameters:
quoteChar - the quotation charachter to use. If 0, no quotes are used.
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.

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.

Returns:
a string listing the collection

list

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

The method must return a String and have no parameters.

Parameters:
iterator - the iterator to be listed
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.Iterator iterator)
Invoke list() using the toString() method.

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

list

public java.lang.String list(java.util.Enumeration enum,
                             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:
enum - 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 enum)
Invoke list() using the toString() method.

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

println

public void println(java.lang.Object[] array,
                    java.lang.String methodToUse)
Invoke list()and print the result on System.out.

Parameters:
array - see list()
methodToUse - see list()

println

public void println(java.lang.Object[] array)
Invoke list()and print the result on System.out.

Parameters:
array - see list()

println

public void println(java.util.Collection coll,
                    java.lang.String methodToUse)
Invoke list()and print the result on System.out.

Parameters:
coll - see list()
methodToUse - see list()

println

public void println(java.util.Collection coll)
Invoke list()and print the result on System.out.

Parameters:
coll - see list()

println

public void println(java.util.Enumeration enum,
                    java.lang.String methodToUse)
Invoke list()and print the result on System.out.

Parameters:
enum - see list()
methodToUse - see list()

println

public void println(java.util.Enumeration enum)
Invoke list()and print the result on System.out.

Parameters:
enum - see list()

println

public void println(java.util.Map map,
                    java.lang.String methodToUse)
Invoke list()and print the result on System.out.

Parameters:
map - see list()
methodToUse - see list()

println

public void println(java.util.Map map)
Invoke list()and print the result on System.out.


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.

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

getInstance

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

Returns:
ObjectLister the default object lister.

main

public static void main(java.lang.String[] args)
                 throws java.lang.NoSuchMethodException,
                        java.lang.reflect.InvocationTargetException
A test method

Throws:
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

isSetAccessible

public boolean isSetAccessible()
Returns:

setSetAccessible

public void setSetAccessible(boolean setAccessible)
Parameters:
setAccessible -

isFailOnUnaccessible

public boolean isFailOnUnaccessible()
Returns:

setFailOnUnaccessible

public void setFailOnUnaccessible(boolean failOnUnaccessible)
Parameters:
failOnUnaccessible -

getDefaultHTMLInstance

public static ObjectLister getDefaultHTMLInstance()
Return the default instance separating listed elements with a &ĝlt;br> HTML element

Returns: