info.monitorenter.util
Class StringUtil

java.lang.Object
  extended by info.monitorenter.util.StringUtil

public final class StringUtil
extends Object

Nice static helpers for working with Strings.

Maybe not always the fastest solution to call in here, but working. Also usable for seeing examples and cutting code for manual inlining.

Version:
$Revision: 1.3 $
Author:
Achim.Westermann@gmx.de

Method Summary
static String appendSpaces(String s, int count)
          Appends the given amount of spaces to the String.
static void arraysToString(List objects)
          Little String output - helper that modifies the given LinkedList by getting it's Objects and replace them by their toString() - representation.
static String arrayToString(Object isArr)
          If the given Object is no Array, it's toString - method is invoked.
static String getNewLine()
          Returns the system - dependant line separator.
static StringUtil instance()
          Returns the singleton instance of this class.
static boolean isEmpty(String test)
          Returns true if the argument is null or consists of whitespaces only.
static int longestStringRepresentation(List objects)
          Returns the maximum length of a Object.toString() result in characters within the given List.
static String setSize(String s, int length)
          Appends the necessary amount of spaces to the string until it has the givn length.
static void toLongestString(List objects)
          Modifies the given LinkedList by getting it's Objects and replace them by their toString() - representation concatenated with the necessary amount of whitespaces that every String in the List will have the same amount of characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

appendSpaces

public static final String appendSpaces(String s,
                                        int count)
Appends the given amount of spaces to the String.

Not intended for big append -operations because in a loop alway just one space is added.

Parameters:
s - the base String to append spaces to.
count - the amount of spaces to append.
Returns:
a String consisting of s and count trailing whitespaces.

arraysToString

public static final void arraysToString(List objects)
Little String output - helper that modifies the given LinkedList by getting it's Objects and replace them by their toString() - representation.

What is special?
If an Object in the given List is an Array (of Objects or primitive datatypes) reflection will be used to create a String - representation of them. The changes are reflected in the Objects that are in the given List. So keep a reference to it. If you are sure, that your List does not contain Arrays do not use this method to avoid overhead.

Avoid structural modifications (remove) of the list while using this method. This method or better: the given List is only thread - safe if the list is synchronized.

A clever VM (hotspot) will be able to inline this function because of void return.

Parameters:
objects - the List of objects that will be changed to a list of the String representation of the Objects with respect to special array treatment.

arrayToString

public static final String arrayToString(Object isArr)
If the given Object is no Array, it's toString - method is invoked. Primitive type - Arrays and Object - Arrays are introspected using java.lang.reflect.Array. Convention for creation fo String - representation:
// Primitive arrays: "["+isArr[0]+","+isArr[1]+.. ..+isArr[isArr.length-1]+"]" //Object arrays : "["+isArr[0].toString()+","+.. ..+isArr[isArr.length-1].toString+"]" // Two or three - dimensional Arrays are not supported //(should be reflected in a special output method, e.g.as a field) // other Objects: toString()

Parameters:
isArr - The Array to represent as String.
Returns:
a String-represetation of the Object.

getNewLine

public static String getNewLine()
Returns the system - dependant line separator.

Only call this method once (not in a loop) and keep the result.

Returns:
the system - dependant line separator.

instance

public static StringUtil instance()
Returns the singleton instance of this class.

This method is useless for now as all methods are static. It may be used in future if VM-global configuration will be put to the state of the instance.

#

Returns:
the singleton instance of this class.

isEmpty

public static boolean isEmpty(String test)
Returns true if the argument is null or consists of whitespaces only.

Parameters:
test - the String to test.
Returns:
true if the argument is null or consists of whitespaces only.

longestStringRepresentation

public static final int longestStringRepresentation(List objects)
Returns the maximum length of a Object.toString() result in characters within the given List.

No data is changed in the given List at all. But the String - representation of all Objects, even Arrays is build to inspect.
Convention for creation fo String - representation:

        Primitive Arrays : as performed by this classes @see #ArrayToString.
        Object Arrays    :  as performed by this classes @see #ArrayToString
        other Objects    :  toString()  (as performed by this classes @see #ArrayToString).
 

Parameters:
objects - the List<Object> to inspect for the maximum lenght of a Object.toString() result.
Returns:
The length of the longest String - representation of an Object in the List or 0 if objects was null or of size 0.

setSize

public static final String setSize(String s,
                                   int length)
Appends the necessary amount of spaces to the string until it has the givn length. No Exception is thrown, if the length of the String is shorter than the given length, but nothing will happen and a message will be printed to the System.out.

Parameters:
s - the String to expand.
length - the desired length of the String to be returned.
Returns:
A String that represents the content of the old String including extra whitespaces.

toLongestString

public static final void toLongestString(List objects)
Modifies the given LinkedList by getting it's Objects and replace them by their toString() - representation concatenated with the necessary amount of whitespaces that every String in the List will have the same amount of characters.

Only use this method in following case:

What happens?

At least this method will be speeded up by a hotspot VM by inlining this method.
An example:
You just retrieved data from a db using jdbc and a generic class (resultset.getObject()). You want to print the data to System.out or to save it to a file, but do not know, if Arrays are contained. You want the output to be formatted (each line).

Parameters:
objects - contains the Objects to replace by their toString() representation.


Copyright © 2001 - 2007 LGPL, All Rights Footloose.