Class StringUtil

java.lang.Object
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.10 $
Author:
Achim.Westermann@gmx.de
  • Method Details

    • 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.
    • listOfArraysToString

      public static final void listOfArraysToString(List<Object> 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 data types) 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:

      Parameters:
      isArr - The Array to represent as String.
      Returns:
      a String-representation of the Object.
      See Also:
    • arrayToString

      public static final String arrayToString(Object isArr, String separator)
      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 for String - representation:
      // Primitive arrays: "["+isArr[0]+"<separator>"+isArr[1]+.. ..+isArr[isArr.length-1]+"]" //Object arrays : "["+isArr[0].toString()+"<separator>"+.. ..+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:
      separator - put in-between each array element in the resulting string.
      isArr - The Array to represent as String.
      Returns:
      a String-representation of the Object.
    • getNewLine

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

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

      Returns:
      the system - dependent 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<Object> 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 length 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<Object> objects)
      Modifies the given LinkedList by getting it's Objects and replace them by their toString() - representation concatenated with the necessary amount of white spaces that every String in the List will have the same amount of characters.

      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. The old Objects will get lost!
      • All Strings will be filled with whitespaces until each String has the same length.
      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.