|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.utexas.its.eis.tools.qwicap.util.ArrayToString
public class ArrayToString
Converts an array or Collection
to a neatly formatted String when its toString
method is
invoked. This class is useful anytime an array or collection of objects is to be converted to a string, but it is
particularly useful in conjunction with the Logger
, since it allows an array of objects
to be incorporated into a log message in a human-readable fashion. Moreover, because it defers all of the
nitty-gritty work of the string conversions, formatting and appending until its toString
method is
invoked, an instance of this class will do no work if the Logger discards the log entry in which it is used.
Note that when performing quoting this class attempts to avoid redundant quoting, so, before any item is quoted it is checked to see if it already begins and ends with the relevant opening- and closing-quote sequences. If it does, no additional quotes are added.
Also note that any type of object may be passed to this class' constructors in place of an array or
Collection
. Therefore, this class can be used to supply deferred string conversion for any other class,
and from time-to-time the quoting logic might come in handy, too.
Constructor Summary | |
---|---|
ArrayToString(Object A)
Constructs a new ArrayToString object that will create a String representation of the
supplied array or Collection object when this object's toString method is invoked, and not
before then. |
|
ArrayToString(Object A,
String FinalSeparatorStr)
Constructs a new ArrayToString object that will create a String representation of the
supplied array or Collection object when this object's toString method is invoked, and not
before then. |
|
ArrayToString(Object A,
String SeparatorStr,
String FinalSeparatorStr,
String QuoteStr)
Constructs a new ArrayToString object that will create a String representation of the
supplied array or Collection object when this object's toString method is invoked, and not
before then. |
Method Summary | |
---|---|
ArrayToString |
alwaysQuote()
Causes all elements to be quoted, whether or not this class would ordinarily judge quoting to be necessary. |
int |
length()
The number of elements in the array or Collection that was converted to a String by this
object. |
ArrayToString |
neverQuote()
Prevents all elements from being quoted, whether or not this class would ordinarily judge quoting to be necessary. |
ArrayToString |
setFinalSeparator(String FinalSeparatorStr)
Sets the string that should be placed between the final two elements in the array or Collection . |
ArrayToString |
setQuote(String QuoteStr)
Sets the string that should be placed around elements that would benefit from being quoted. |
ArrayToString |
setQuotes(char OpenQuote,
char CloseQuote)
Sets the characters that should be placed to the left and right of elements that would benefit from being quoted. |
ArrayToString |
setQuotes(String OpenQuote,
String CloseQuote)
Sets the strings that should be placed to the left and right of elements that would benefit from being quoted. |
ArrayToString |
setSeparator(String SeparatorStr)
Sets the separator string to be placed between all elements except the final two (see setFinalSeparator ). |
String |
toString()
Returns a String representation of the array or Collection object passed to the
constructor. |
ArrayToString |
trim()
Causes the whitespace at the beginning or end of each element in the array or Collection to be
trimmed away. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public ArrayToString(Object A)
ArrayToString
object that will create a String
representation of the
supplied array or Collection
object when this object's toString
method is invoked, and not
before then. Commas will be placed between all elements except for the final two which will be separated by the
string " and ". Elements that will benefit from quoting are surrounded by the double-quote ('"') character. An
item is judged to benefit from being quoted if it contains any whitespace, or a comma.
A
- An array or Collection
object whose contents should be converted to a
String
. At this time, arrays or Collection
objects whose elements
are other arrays or Collection
objects are not supported in a useful manner.public ArrayToString(Object A, String FinalSeparatorStr)
ArrayToString
object that will create a String
representation of the
supplied array or Collection
object when this object's toString
method is invoked, and not
before then. Commas will be placed between all elements except for the final two which will be separated by the
string specified in the FinalSeparatorStr
parameter. Elements that will benefit from quoting are
surrounded by the double-quote ('"') character. An item is judged to benefit from being quoted if it contains
any whitespace, or a comma.
A
- An array or Collection
object whose contents should be converted to a
String
. At this time, arrays or Collection
objects whose elements
are other arrays or Collection
objects are not supported in a useful manner.FinalSeparatorStr
- The string that should be placed between the final two elements in the array or
Collection
, for example: " and " or " or ".public ArrayToString(Object A, String SeparatorStr, String FinalSeparatorStr, String QuoteStr)
ArrayToString
object that will create a String
representation of the
supplied array or Collection
object when this object's toString
method is invoked, and not
before then.
A
- An array or Collection
object whose contents should be converted to a
String
. At this time, arrays or Collection
objects whose elements
are other arrays or Collection
objects are not supported in a useful manner.SeparatorStr
- The string that should be placed between elements in the array or Collection
,
for example: a comma with trailing space (", ").FinalSeparatorStr
- The string that should be placed between the final two elements in the array or
Collection
, for example: " and " or " or ".QuoteStr
- The string to be placed around items in the array or Collection
that would
benefit from being quoted. An item is judged to benefit from being quoted if it contains
any whitespace, or a comma. To disable all quoting behavior, pass null
in
this parameter. To provide separate open- and close-quote strings, use setQuotes(java.lang.String, java.lang.String)
.Method Detail |
---|
public ArrayToString setSeparator(String SeparatorStr)
setFinalSeparator
).
SeparatorStr
- The string that should be placed between elements in the array or Collection
,
for example: a comma with trailing space (", ").
ArrayToString
class.public ArrayToString setFinalSeparator(String FinalSeparatorStr)
Collection
.
FinalSeparatorStr
- The string that should be placed between the final two elements in the array or
Collection
, for example: " and " or " or ".
ArrayToString
class.public ArrayToString setQuote(String QuoteStr)
null
, or invoke neverQuote()
. To force quoting under all circumstances, invoke
alwaysQuote()
. To provide separate open- and close-quote strings, use setQuotes
.
QuoteStr
- The string to be placed around items in the array or Collection
that would
benefit from being quoted. To disable all quoting behavior, pass null
in
this parameter.
ArrayToString
class.public ArrayToString setQuotes(String OpenQuote, String CloseQuote)
null
in both parameters, or invoke neverQuote()
. To force quoting under all
circumstances, invoke alwaysQuote()
.
OpenQuote
- The string to be placed to the left of items in the array or Collection
that would benefit from being quoted.CloseQuote
- The string to be placed to the right of items in the array or Collection
that would benefit from being quoted.
ArrayToString
class.public ArrayToString setQuotes(char OpenQuote, char CloseQuote)
neverQuote()
. To force quoting under all circumstances, invoke alwaysQuote()
.
OpenQuote
- The character to be placed to the left of items in the array or Collection
that would benefit from being quoted.CloseQuote
- The character to be placed to the right of items in the array or Collection
that would benefit from being quoted.
ArrayToString
class.public ArrayToString alwaysQuote()
ArrayToString
class.public ArrayToString neverQuote()
ArrayToString
class.public ArrayToString trim()
Collection
to be
trimmed away. By default, trimming does not occur.
ArrayToString
class.public int length()
Collection
that was converted to a String
by this
object. Note that this information isn't available until this object's toString
method has been
called once, so length
calls toString
automatically, if it hasn't already been called.
Collection
that was passed to this
object's constructor.public String toString()
String
representation of the array or Collection
object passed to the
constructor. The String
representation is not created until this method is called for the first time,
and is thereafter cached, so that all future toString
invocations will return the same
String
object as the first invocation, and will do so essentially instantly. Note, however, that if
any of the methods that control the way the string is generated are invoked (for example setQuotes
or trim()
), they will discard the cached String
object to ensure that the next time this
toString
method is invoked, the String
that is returned will be formatted in a manner
consistent with the caller's expressed intent.
toString
in class Object
String
representation of the array or Collection
object passed to the
constructor.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |