org.infinispan.util
Class Util

java.lang.Object
  extended by org.infinispan.util.Util

public final class Util
extends Object

General utility methods used throughout the Infinispan code base.

Since:
4.0
Author:
Brian Stansberry, Galder ZamarreƱo

Field Summary
static byte[] EMPTY_BYTE_ARRAY
           
static Object[] EMPTY_OBJECT_ARRAY
           
 
Method Summary
static
<T> Set<T>
asSet(T... a)
           
static
<T> T
cloneWithMarshaller(Marshaller marshaller, T x)
          Clones parameter x of type T with a given Marshaller reference;
static void close(Closeable... cls)
           
static void close(Closeable cl)
           
static void close(Context ctx)
           
static void close(Socket s)
           
static Double constructDouble(Class<?> type, Object o)
           
static long currentMillisFromNanotime()
          System.nanoTime() is less expensive than System.currentTimeMillis() and better suited to measure time intervals.
static void flushAndCloseOutput(ObjectOutput o)
           
static void flushAndCloseStream(OutputStream o)
           
static String formatString(Object message, Object... params)
           
static ClassLoader[] getClassLoaders(ClassLoader appClassLoader)
           
static
<T> T
getInstance(Class<T> clazz)
          Instantiates a class by first attempting a static factory method named getInstance() on the class and then falling back to an empty constructor.
static
<T> T
getInstance(String classname, ClassLoader cl)
          Instantiates a class based on the class name provided.
static
<T> T
getInstanceStrict(Class<T> clazz)
          Similar to getInstance(Class) except that exceptions are propagated to the caller.
static
<T> T
getInstanceStrict(String classname, ClassLoader cl)
          Similar to getInstance(String, ClassLoader) except that exceptions are propagated to the caller.
static int getNormalizedHash(Object key, Hash hashFct)
           
static String hexDump(byte[] buffer)
           
static String hexDump(ByteBuffer buffer)
           
static String hexIdHashCode(Object o)
          Prints the identity hash code of the object passed as parameter in an hexadecimal format in order to safe space.
static
<T> Class<T>
loadClass(String classname, ClassLoader cl)
           Loads the specified class using the passed classloader, or, if it is null the Infinispan classes' classloader.
static
<T> Class<T>
loadClassStrict(String classname, ClassLoader userClassLoader)
           Loads the specified class using the passed classloader, or, if it is null the Infinispan classes' classloader.
static String padString(String s, int minWidth)
           
static String prettyPrintTime(long millis)
          Prints a time for display
static String prettyPrintTime(long time, TimeUnit unit)
           
static String printArray(byte[] array, boolean withHash)
           
static CacheException rewrapAsCacheException(Throwable t)
           
static boolean safeEquals(Object a, Object b)
          Null-safe equality test.
static String threadDump()
           
static String toHexString(byte[] input)
           
static String toHexString(byte[] input, int limit)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_OBJECT_ARRAY

public static final Object[] EMPTY_OBJECT_ARRAY

EMPTY_BYTE_ARRAY

public static final byte[] EMPTY_BYTE_ARRAY
Method Detail

loadClass

public static <T> Class<T> loadClass(String classname,
                                     ClassLoader cl)

Loads the specified class using the passed classloader, or, if it is null the Infinispan classes' classloader.

If loadtime instrumentation via GenerateInstrumentedClassLoader is used, this class may be loaded by the bootstrap classloader.

If the class is not found, the ClassNotFoundException or NoClassDefFoundError is wrapped as a CacheConfigurationException and is re-thrown.

Parameters:
classname - name of the class to load
cl - the application classloader which should be used to load the class, or null if the class is always packaged with Infinispan
Returns:
the class
Throws:
CacheConfigurationException - if the class cannot be loaded

getClassLoaders

public static ClassLoader[] getClassLoaders(ClassLoader appClassLoader)

loadClassStrict

public static <T> Class<T> loadClassStrict(String classname,
                                           ClassLoader userClassLoader)
                                throws ClassNotFoundException

Loads the specified class using the passed classloader, or, if it is null the Infinispan classes' classloader.

If loadtime instrumentation via GenerateInstrumentedClassLoader is used, this class may be loaded by the bootstrap classloader.

Parameters:
classname - name of the class to load
userClassLoader - the application classloader which should be used to load the class, or null if the class is always packaged with Infinispan
Returns:
the class
Throws:
ClassNotFoundException - if the class cannot be loaded

getInstance

public static <T> T getInstance(Class<T> clazz)
Instantiates a class by first attempting a static factory method named getInstance() on the class and then falling back to an empty constructor.

Any exceptions encountered are wrapped in a CacheConfigurationException and rethrown.

Parameters:
clazz - class to instantiate
Returns:
an instance of the class

getInstanceStrict

public static <T> T getInstanceStrict(Class<T> clazz)
                           throws IllegalAccessException,
                                  InstantiationException
Similar to getInstance(Class) except that exceptions are propagated to the caller.

Parameters:
clazz - class to instantiate
Returns:
an instance of the class
Throws:
IllegalAccessException
InstantiationException

getInstance

public static <T> T getInstance(String classname,
                                ClassLoader cl)
Instantiates a class based on the class name provided. Instantiation is attempted via an appropriate, static factory method named getInstance() first, and failing the existence of an appropriate factory, falls back to an empty constructor.

Any exceptions encountered loading and instantiating the class is wrapped in a CacheConfigurationException.

Parameters:
classname - class to instantiate
Returns:
an instance of classname

getInstanceStrict

public static <T> T getInstanceStrict(String classname,
                                      ClassLoader cl)
                           throws ClassNotFoundException,
                                  InstantiationException,
                                  IllegalAccessException
Similar to getInstance(String, ClassLoader) except that exceptions are propagated to the caller.

Parameters:
classname - class to instantiate
Returns:
an instance of classname
Throws:
ClassNotFoundException
InstantiationException
IllegalAccessException

cloneWithMarshaller

public static <T> T cloneWithMarshaller(Marshaller marshaller,
                                        T x)
Clones parameter x of type T with a given Marshaller reference;

Returns:
a deep clone of an object parameter x

safeEquals

public static boolean safeEquals(Object a,
                                 Object b)
Null-safe equality test.

Parameters:
a - first object to compare
b - second object to compare
Returns:
true if the objects are equals or both null, false otherwise.

prettyPrintTime

public static String prettyPrintTime(long time,
                                     TimeUnit unit)

currentMillisFromNanotime

public static final long currentMillisFromNanotime()
System.nanoTime() is less expensive than System.currentTimeMillis() and better suited to measure time intervals. It's NOT suited to know the current time, for example to be compared with the time of other nodes.

Returns:
the value of System.nanoTime(), but converted in Milliseconds.

prettyPrintTime

public static String prettyPrintTime(long millis)
Prints a time for display

Parameters:
millis - time in millis
Returns:
the time, represented as millis, seconds, minutes or hours as appropriate, with suffix

close

public static void close(Closeable cl)

close

public static void close(Socket s)

close

public static void close(Closeable... cls)

close

public static void close(Context ctx)

flushAndCloseStream

public static void flushAndCloseStream(OutputStream o)

flushAndCloseOutput

public static void flushAndCloseOutput(ObjectOutput o)

formatString

public static String formatString(Object message,
                                  Object... params)

printArray

public static String printArray(byte[] array,
                                boolean withHash)

toHexString

public static String toHexString(byte[] input)

toHexString

public static String toHexString(byte[] input,
                                 int limit)

padString

public static String padString(String s,
                               int minWidth)

threadDump

public static String threadDump()

rewrapAsCacheException

public static CacheException rewrapAsCacheException(Throwable t)

asSet

public static <T> Set<T> asSet(T... a)

hexIdHashCode

public static String hexIdHashCode(Object o)
Prints the identity hash code of the object passed as parameter in an hexadecimal format in order to safe space.


hexDump

public static String hexDump(byte[] buffer)

hexDump

public static String hexDump(ByteBuffer buffer)

constructDouble

public static Double constructDouble(Class<?> type,
                                     Object o)

getNormalizedHash

public static int getNormalizedHash(Object key,
                                    Hash hashFct)

-->

Copyright © 2012 JBoss, a division of Red Hat. All Rights Reserved.