com.metsci.glimpse.util
Class GeneralUtils

java.lang.Object
  extended by com.metsci.glimpse.util.GeneralUtils

public class GeneralUtils
extends java.lang.Object

This class holds miscellaneous static methods that are simple but broadly useful. In particular, workarounds for Java defects can go here.

Author:
hogye

Field Summary
static java.lang.String LINE_SEPARATOR
           
 
Method Summary
static
<T> T[]
array(T... values)
          Terse (especially as a static import) way to create a T[] literal.
static
<K> java.util.ArrayList<K>
asList(java.util.Collection<K> values)
          Creates a new ArrayList from a collection by building an enumeration over the collection.
static
<K> java.util.HashSet<K>
asSet(K... values)
           
static boolean[] booleans(boolean... values)
          Terse (especially as a static import) way to create a boolean[] literal.
static
<T,U> T
cast(U u)
          Type casts from one type to another.
static
<T> java.lang.Class<? extends T>
classForName(java.lang.String className)
          This method returns the correct type as specified by the caller, unlike Class.forName(String).
static int compare(int intVal1, int intVal2)
          Compare two ints, according to the standard Comparable interface.
static int compare(long longVal1, long longVal2)
          Compare two longs, according to the standard Comparable interface.
static short compare(short shortVal1, short shortVal2)
          Compare two shorts, according to the standard Comparable interface.
static int compareInts(int intVal1, int intVal2)
          Compare two ints, according to the standard Comparable interface.
static int compareLongs(long longVal1, long longVal2)
          Compare two longs, according to the standard Comparable interface.
static short compareShorts(short shortVal1, short shortVal2)
          Compare two shorts, according to the standard Comparable interface.
static double[] doubles(double... values)
          Terse (especially as a static import) way to create a double[] literal.
static
<T extends java.lang.Enum<T>>
java.util.EnumSet<T>
enumSet(T... elements)
          Terse (especially as a static import) way to create an EnumSet.
static float[] floats(float... values)
          Terse (especially as a static import) way to create a float[] literal.
static java.lang.String getWorkingDir()
          Returns the directory in which the application was started (this is the working/current directory).
static int hashCode(boolean booleanVal)
          Get standard Java hashCode for a boolean without the extra object creation.
static int hashCode(double doubleVal)
          Get standard Java hashCode for a float without the extra object creation.
static int hashCode(float floatVal)
          Get standard Java hashCode for a float without the extra object creation.
static int hashCode(long longVal)
          Get standard Java hashCode for a long without the extra object creation.
static int[] ints(int... values)
          Terse (especially as a static import) way to create an int[] literal.
static long[] longs(long... values)
          Terse (especially as a static import) way to create a long[] literal.
static
<K> java.util.ArrayList<K>
newArrayList()
          Creates a new ArrayList by examining the expected return type.
static
<K> java.util.ArrayList<K>
newArrayList(java.util.Collection<? extends K> c)
          Creates a new ArrayList containing the elements of the specified collection, in order.
static
<K,V> java.util.HashMap<K,V>
newHashMap()
          Creates a new HashMap by examining the expected return type.
static
<K,V> java.util.HashMap<K,V>
newHashMap(java.util.Map<? extends K,? extends V> m)
          Creates a new HashMap containing the same mappings as the specified map.
static
<K> java.util.HashSet<K>
newHashSet()
          Creates a new HashSet by examining the expected return type.
static
<K> java.util.HashSet<K>
newHashSet(java.util.Collection<? extends K> c)
          Creates a new HashSet containing the elements of the specified collection.
static
<K,V> java.util.LinkedHashMap<K,V>
newLinkedHashMap()
          Creates a new LinkedHashMap by examining the expected return type.
static
<K,V> java.util.TreeMap<K,V>
newTreeMap()
          Creates a new TreeMap by examining the expected return type.
static
<K> java.util.TreeSet<K>
newTreeSet()
          Creates a new TreeSet by examining the expected return type.
static
<K> java.util.List<K>
newUnmodifiableList(java.util.Collection<? extends K> c)
          Creates a new unmodifiable List containing the elements of the specified collection, in order.
static void stringBufferAppend(java.lang.StringBuffer sb, java.lang.String format, java.lang.Object... args)
          Convenience method for formatting and appending data to an existing StringBuffer.
static void stringBuilderAppend(java.lang.StringBuilder sb, java.lang.String format, java.lang.Object... args)
          Convenience method for formatting and appending data to an existing StringBuilder.
static java.math.BigDecimal stripTrailingZeros(java.math.BigDecimal value)
          Workaround for bug #6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0").
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final java.lang.String LINE_SEPARATOR
Method Detail

stripTrailingZeros

public static java.math.BigDecimal stripTrailingZeros(java.math.BigDecimal value)
Workaround for bug #6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0").


hashCode

public static int hashCode(long longVal)
Get standard Java hashCode for a long without the extra object creation.

Equivalent to (new Long(longVal)).hashCode() in jdk1.6.

Parameters:
longVal - value for which to compute hashCode
Returns:
hashCode

hashCode

public static int hashCode(float floatVal)
Get standard Java hashCode for a float without the extra object creation.

Equivalent to (new Float(floatVal)).hashCode() in jdk1.6.

Parameters:
floatVal - value for which to compute hashCode
Returns:
hashCode

hashCode

public static int hashCode(double doubleVal)
Get standard Java hashCode for a float without the extra object creation.

Equivalent to (new Double(doubleVal)).hashCode() in jdk1.6.

Parameters:
doubleVal - value for which to compute hashCode
Returns:
hashCode

hashCode

public static int hashCode(boolean booleanVal)
Get standard Java hashCode for a boolean without the extra object creation.

Equivalent to (new Boolean(booleanVal)).hashCode() in jdk1.6.

Parameters:
booleanVal - value for which to compute hashCode
Returns:
hashCode

compare

public static short compare(short shortVal1,
                            short shortVal2)
Compare two shorts, according to the standard Comparable interface. Since 1.4, the JDK provides Double.compare( double, double ) and Float.compare( float, float ) but not variants for Integer, Long, and Short.

Parameters:
shortVal1 -
shortVal2 -
Returns:
-1, 0, or 1 if shortVal1 is less than, equal to, or greater than shortVal2 respectively.

compareShorts

public static short compareShorts(short shortVal1,
                                  short shortVal2)
Compare two shorts, according to the standard Comparable interface. Since 1.4, the JDK provides Double.compare( double, double ) and Float.compare( float, float ) but not variants for Integer, Long, and Short.

Parameters:
shortVal1 -
shortVal2 -
Returns:
-1, 0, or 1 if shortVal1 is less than, equal to, or greater than shortVal2 respectively.

compare

public static int compare(int intVal1,
                          int intVal2)
Compare two ints, according to the standard Comparable interface. Since 1.4, the JDK provides Double.compare( double, double ) and Float.compare( float, float ) but not variants for Integer, Long, and Short.

Parameters:
intVal1 -
intVal2 -
Returns:
-1, 0, or 1 if intVal1 is less than, equal to, or greater than intVal2 respectively.

compareInts

public static int compareInts(int intVal1,
                              int intVal2)
Compare two ints, according to the standard Comparable interface. Since 1.4, the JDK provides Double.compare( double, double ) and Float.compare( float, float ) but not variants for Integer, Long, and Short.

Parameters:
intVal1 -
intVal2 -
Returns:
-1, 0, or 1 if intVal1 is less than, equal to, or greater than intVal2 respectively.

compare

public static int compare(long longVal1,
                          long longVal2)
Compare two longs, according to the standard Comparable interface. Since 1.4, the JDK provides Double.compare( double, double ) and Float.compare( float, float ) but not variants for Integer, Long, and Short.

Parameters:
longVal1 -
longVal2 -
Returns:
-1, 0, or 1 if longVal1 is less than, equal to, or greater than longVal2 respectively.

compareLongs

public static int compareLongs(long longVal1,
                               long longVal2)
Compare two longs, according to the standard Comparable interface. Since 1.4, the JDK provides Double.compare( double, double ) and Float.compare( float, float ) but not variants for Integer, Long, and Short.

Parameters:
longVal1 -
longVal2 -
Returns:
-1, 0, or 1 if longVal1 is less than, equal to, or greater than longVal2 respectively.

classForName

public static <T> java.lang.Class<? extends T> classForName(java.lang.String className)
                                                 throws java.lang.ClassNotFoundException
This method returns the correct type as specified by the caller, unlike Class.forName(String). It also helps localizing the "Type safety" warnings to a single place.

Throws:
java.lang.ClassNotFoundException

getWorkingDir

public static java.lang.String getWorkingDir()
Returns the directory in which the application was started (this is the working/current directory).


cast

public static <T,U> T cast(U u)
Type casts from one type to another. Provided mostly to reduce the number of "type safety" warnings.

NOTE: When using the Sun Java compiler, this method should be invoked as GeneralUtils.< T, U >cast(u) to avoid the following bug.


newHashSet

public static final <K> java.util.HashSet<K> newHashSet()
Creates a new HashSet by examining the expected return type.


newHashSet

public static final <K> java.util.HashSet<K> newHashSet(java.util.Collection<? extends K> c)
Creates a new HashSet containing the elements of the specified collection.

See Also:
HashSet#HashSet(Collection)}

newTreeSet

public static final <K> java.util.TreeSet<K> newTreeSet()
Creates a new TreeSet by examining the expected return type.


newHashMap

public static final <K,V> java.util.HashMap<K,V> newHashMap()
Creates a new HashMap by examining the expected return type.


newHashMap

public static final <K,V> java.util.HashMap<K,V> newHashMap(java.util.Map<? extends K,? extends V> m)
Creates a new HashMap containing the same mappings as the specified map.

See Also:
HashMap#HashMap(Map)}

newLinkedHashMap

public static final <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap()
Creates a new LinkedHashMap by examining the expected return type.


newTreeMap

public static final <K,V> java.util.TreeMap<K,V> newTreeMap()
Creates a new TreeMap by examining the expected return type.


newArrayList

public static final <K> java.util.ArrayList<K> newArrayList()
Creates a new ArrayList by examining the expected return type.


newArrayList

public static final <K> java.util.ArrayList<K> newArrayList(java.util.Collection<? extends K> c)
Creates a new ArrayList containing the elements of the specified collection, in order.

See Also:
ArrayList#ArrayList(Collection)}.

newUnmodifiableList

public static final <K> java.util.List<K> newUnmodifiableList(java.util.Collection<? extends K> c)
Creates a new unmodifiable List containing the elements of the specified collection, in order.


asList

public static final <K> java.util.ArrayList<K> asList(java.util.Collection<K> values)
Creates a new ArrayList from a collection by building an enumeration over the collection.


asSet

public static final <K> java.util.HashSet<K> asSet(K... values)

ints

public static int[] ints(int... values)
Terse (especially as a static import) way to create an int[] literal.


longs

public static long[] longs(long... values)
Terse (especially as a static import) way to create a long[] literal.


floats

public static float[] floats(float... values)
Terse (especially as a static import) way to create a float[] literal.


doubles

public static double[] doubles(double... values)
Terse (especially as a static import) way to create a double[] literal.


booleans

public static boolean[] booleans(boolean... values)
Terse (especially as a static import) way to create a boolean[] literal.


array

public static <T> T[] array(T... values)
Terse (especially as a static import) way to create a T[] literal.


enumSet

public static <T extends java.lang.Enum<T>> java.util.EnumSet<T> enumSet(T... elements)
Terse (especially as a static import) way to create an EnumSet.


stringBufferAppend

public static void stringBufferAppend(java.lang.StringBuffer sb,
                                      java.lang.String format,
                                      java.lang.Object... args)
Convenience method for formatting and appending data to an existing StringBuffer.


stringBuilderAppend

public static void stringBuilderAppend(java.lang.StringBuilder sb,
                                       java.lang.String format,
                                       java.lang.Object... args)
Convenience method for formatting and appending data to an existing StringBuilder.



Copyright © 2012 Metron, Inc.. All Rights Reserved.