info.monitorenter.util
Class StringUtil

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

public class StringUtil
extends Object

Nice static helpers for working with Strings. Maybe not always the fastest solution to call in here, but working. Also usabel for seeing examples and cutting code for manual inlining.

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()
          Only call this method once (not in a loop) and keep the result.
static StringUtil instance()
           
static int longestStringRepresentation(List objects)
          No data is changed in the given List at all.
static Map.Entry prefixIntersection(String a, String b)
          This may be useful for comparison of canonical paths.
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)
          Only use this method in following case:
You have got an AbstractList or subclass containing Objects you do not know. You want to transform all Elements in the List to Strings. There might be Array's in the Object (whose toString method will return only their hashcode).
What happens? All Objects, even primitive Arrays in your List will be turned to String- representation. The result will replace the old position of the Object in the given List.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

instance

public static StringUtil instance()

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.

appendSpaces

public static final String appendSpaces(String s,
                                        int count)
Appends the given amount of spaces to the String. Keep the param on the calling side. It's reference is modified. Not ment for big append -operations because in a loop alway just one space is added.

Parameters:
s -
count -

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[1].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

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.


toLongestString

public static final void toLongestString(List objects)
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).


longestStringRepresentation

public static final int longestStringRepresentation(List objects)
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 ).
 

Returns:
The length of the longest String - representation of an Object in the List or 0 if objects was null or of size 0.

getNewLine

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


prefixIntersection

public static Map.Entry prefixIntersection(String a,
                                           String b)
This may be useful for comparison of canonical paths. For two strings a,b the prefix intersection of a and b is returned on the key side and the tail of b that did not match on the value side.
Example:
a b .getKey() .getValue()
computer compare comp are
compare computer comp uter
computer put "" ""
c:/work/myproject/bin/ c:/work/myproject/bin/cpdetector/io/ClassFileFilter.class c:/work/myproject/bin/ cpdetector/io/ClassFileFilter.class
c:/work/myproject/bin/cpdetector/io/ClassFileFilter.class c:/work/myproject/bin/ c:/work/myproject/bin/ ""
Note that the prefix is not searched within the strings. Both strings have to have a common prefix (no skipping until identical parts are found).



Copyleft ㊢ 2003-2004 MPL 1.1, All Rights Footloose.