com.sibvisions.util.type
Class CommonUtil

java.lang.Object
  extended by com.sibvisions.util.type.CommonUtil

public final class CommonUtil
extends Object

The CommonUtil contains utility methods for handling type independent operations.


Method Summary
static
<T> T
close(Object... pClosable)
          Close the given object(s) if closable.
static boolean containsException(Throwable pCause, Class<?> pClass)
          Gets whether an exception chain is or contains a specific error class.
static String dump(Throwable pCause, boolean pDeep)
          Writes the stack trace of an exception/throwable object into a string.
static boolean equals(Object pFirst, Object pSecond)
          Indicates whether two object are "equal".
static List<Throwable> getCauseList(Throwable pCause)
          Gets the list of all available exceptions.
static Throwable getException(Throwable pCause, Class<?> pClass)
          Gets an exception of the given type from an exception chain.
static Throwable getFirstCause(Throwable pCause)
          Gets the first cause.
static int getFreePort(int pMin, int pMax)
          Searchs a free *network* port in the given range.
static int getFreePort(int pMin, int pMax, boolean pCheckLocal)
          Searchs a free port in the given range.
static boolean isReachable(String pHost, int pPort)
          Tests if the given port is reachable on the given host.
static
<T> T
nvl(T pValue, T pNvlValue)
          Gets an alternative value for a null object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

dump

public static String dump(Throwable pCause,
                          boolean pDeep)
Writes the stack trace of an exception/throwable object into a string.

Parameters:
pCause - Exception/Throwable (StackTrace)
pDeep - true to dump the trace with all causes; false to dump only the exception
Returns:
stack trace

getFirstCause

public static Throwable getFirstCause(Throwable pCause)
Gets the first cause.

Parameters:
pCause - the origin cause
Returns:
the first cause

getCauseList

public static List<Throwable> getCauseList(Throwable pCause)
Gets the list of all available exceptions.

Parameters:
pCause - the start exception
Returns:
the list of all causes and the exception itself. The start exception is the first entry and the cause of the start exception is the second entry and so on.

containsException

public static boolean containsException(Throwable pCause,
                                        Class<?> pClass)
Gets whether an exception chain is or contains a specific error class. This methods checks whether the given class is assignable from the found exception cause.

Parameters:
pCause - the exception (chain)
pClass - the expected class
Returns:
true if the given class or a sub class was found in the exception chain, false otherwise
See Also:
Class.isAssignableFrom(Class)

getException

public static Throwable getException(Throwable pCause,
                                     Class<?> pClass)
Gets an exception of the given type from an exception chain. This methods checks whether the given class is assignable from the found exception cause.

Parameters:
pCause - the exception (chain)
pClass - the expected class
Returns:
the found exception of null if no exception with the given type was found
See Also:
Class.isAssignableFrom(Class)

nvl

public static <T> T nvl(T pValue,
                        T pNvlValue)
Gets an alternative value for a null object.

Type Parameters:
T - parameter type
Parameters:
pValue - desired value
pNvlValue - alternative value
Returns:
pValue or pNvlValue if pValue == null

equals

public static boolean equals(Object pFirst,
                             Object pSecond)
Indicates whether two object are "equal". Two objects are equals if both are null or the Object.equals(Object) returns true.

Parameters:
pFirst - an object
pSecond - another object
Returns:
true if both objects are equal

getFreePort

public static int getFreePort(int pMin,
                              int pMax)
Searchs a free *network* port in the given range. The local interface (127.0.0.1) won't be checked.

Parameters:
pMin - the min port
pMax - the max port
Returns:
-1 if no free port is available in the given range

getFreePort

public static int getFreePort(int pMin,
                              int pMax,
                              boolean pCheckLocal)
Searchs a free port in the given range.

Parameters:
pMin - the min port
pMax - the max port
pCheckLocal - true to check local (127.0.0.1) interface as well as network interface (0.0.0.0), false to check only the network interface
Returns:
-1 if no free port is available in the given range

isReachable

public static boolean isReachable(String pHost,
                                  int pPort)
Tests if the given port is reachable on the given host.

Parameters:
pHost - the hostname or IP
pPort - th port number
Returns:
true if the port is reachable, false otherwise

close

public static <T> T close(Object... pClosable)
Close the given object(s) if closable. An object is closable if it contains a close() method.

Type Parameters:
T - the closable type
Parameters:
pClosable - the object(s) to close
Returns:
null


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.