com.kuka.common
Class CommonUtil

java.lang.Object
  extended by com.kuka.common.CommonUtil

public final class CommonUtil
extends Object

Utility class with some helper methods.

This element is an undocumented internal feature. It is not intended to be used by applications as it might change or be removed in future versions.

Field Summary
static double EPSILON
          Epsilon for equality check in doubles.
static String LOGSERVICE_SURFIX
          Surfix of the log service.
static String USERTASK_PACKAGENAME
          Name of the user task package.
 
Method Summary
static URI appendToURI(URI uri, String pathSuffix)
          Returns a new URI with the given path suffix appended.
static boolean areNearlyEqual(double[] value1, double[] value2)
           
static boolean areNearlyEqual(double[] value1, double[] value2, double epsilon)
          Checks if the two double arrays are nearly equal.
static boolean areNearlyEqual(double value1, double value2)
           
static boolean areNearlyEqual(double value1, double value2, double epsilon)
          Checks if the two double values are nearly equal.
static boolean areNearlyEqual(long value1, long value2, long epsilon)
          Checks if the two long values are nearly equal.
static int booleanToInt(boolean value)
          Converts a boolean value into an integer value.
static String capitalize(String string)
          Capitalizes the given string.
static boolean close(Closeable closeable)
          Closes the given Closeable.
static void close(MulticastSocket socket)
          Closes the given socket.
static void close(ServerSocket socket)
          Closes the given socket.
static void close(Socket socket)
          Closes the given socket.
static Date convertSunriseTime(int[] time)
          Converts the timestamp received from Sunrise Framework to a Date object.
static String decapitalize(String string)
          Decapitalizes the given string.
static Logger getConsoleLogger(Class<?> clazz)
          Deprecated. 
static Logger getConsoleLogger(String className)
          Deprecated. 
static Logger getLogger(Class<?> loggerClass)
          Deprecated. Please use 'LoggerFactory.getLogger(Class loggerClass)'
static Logger getLogger(String loggerName)
          Deprecated. Please use 'LoggerFactory.getLogger(String loggerName)'
static boolean hasSameData(Collection<? extends IDataEquatable> one, Collection<? extends IDataEquatable> other)
          Compares all elements in the committed containers.
static boolean isNullOrEmpty(String str)
          Checks if the given string is null or empty.
static void milliSleep(long millis)
          Deprecated. use ThreadUtil.milliSleep(long) instead
static boolean numberToBoolean(Number value)
          Converts an integer into a boolean value.
static boolean objectEquals(Object o1, Object o2)
          Check two objects for equality regarding that the references could be null.
static int objectHashCode(Object obj)
          Return the hash code of an object regarding that the reference could be null.
static void printHexDump(PrintWriter pw, byte[] data)
          Prints the given data in a pretty-printed hex dump format.
static void printHexDump(PrintWriter pw, byte[] data, int offset, int length)
          Prints the given data in a pretty-printed hex dump format.
static void reverse(byte[] array)
          Reverses the given byte array and stores the result in the same array. object.
static String stringJoin(Collection<? extends Object> items, String separator)
          Joins the String.valueOf() representation of the items in the collections separated by the given separator into a new string.
static String trim(String original)
          Returns a copy of the string, with leading and trailing whitespace omitted.
static URI urlToURI(URL url)
          Returns a URI equivalent to the given URL.
static boolean waitOnLatch(CountDownLatch latch, long timeout, TimeUnit unit)
          Waits on a latch for a specified time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EPSILON

public static final double EPSILON
Epsilon for equality check in doubles.

See Also:
Constant Field Values

USERTASK_PACKAGENAME

public static final String USERTASK_PACKAGENAME
Name of the user task package.

See Also:
Constant Field Values

LOGSERVICE_SURFIX

public static final String LOGSERVICE_SURFIX
Surfix of the log service.

See Also:
Constant Field Values
Method Detail

areNearlyEqual

public static boolean areNearlyEqual(double value1,
                                     double value2,
                                     double epsilon)
Checks if the two double values are nearly equal.

A small epsilon check is performed. Epsilon is 10^-5.

Parameters:
value1 - The first value.
value2 - The second value.
epsilon - The epsilon.
Returns:
True if the two value are nearly equal.

areNearlyEqual

public static boolean areNearlyEqual(double value1,
                                     double value2)
Parameters:
value1 - The first value.
value2 - The second value.
Returns:
True if the two values are nearly equal.
See Also:
areNearlyEqual(double, double, double)

areNearlyEqual

public static boolean areNearlyEqual(double[] value1,
                                     double[] value2,
                                     double epsilon)
Checks if the two double arrays are nearly equal.

A small epsilon check is performed.

Parameters:
value1 - The first value. May not be null.
value2 - The second value. May not be null.
epsilon - The epsilon.
Returns:
True if the two value are nearly equal.

areNearlyEqual

public static boolean areNearlyEqual(double[] value1,
                                     double[] value2)
Parameters:
value1 - The first value. May not be null.
value2 - The second value. May not be null.
Returns:
True if the two value are nearly equal.
See Also:
areNearlyEqual(double[], double[], double)

areNearlyEqual

public static boolean areNearlyEqual(long value1,
                                     long value2,
                                     long epsilon)
Checks if the two long values are nearly equal.

A small epsilon check is performed.

Parameters:
value1 - The first value.
value2 - The second value.
epsilon - The epsilon.
Returns:
True if the two values are nearly equal.

objectEquals

public static boolean objectEquals(Object o1,
                                   Object o2)
Check two objects for equality regarding that the references could be null.

Parameters:
o1 - the first object
o2 - the second object
Returns:
true if both are null, false if one is null but not the other, o1.equals(o2) if both are != null

objectHashCode

public static int objectHashCode(Object obj)
Return the hash code of an object regarding that the reference could be null.

Parameters:
obj - The object to retrieve the hash code
Returns:
obj.hashCode() if obj is != null, otherwise 0

milliSleep

@Deprecated
public static void milliSleep(long millis)
Deprecated. use ThreadUtil.milliSleep(long) instead

Helper method for sleeping some time.

Catches the interrupted exception, 'reinterrupts' the thread so that it's isInterrupted method returns true and throws a ThreadInterruptedException.

Parameters:
millis - The milliseconds to sleep. If negative or 0, the method returns immediately.
Throws:
ThreadInterruptedException - if the sleep was interrupted.

numberToBoolean

public static boolean numberToBoolean(Number value)
Converts an integer into a boolean value.

Parameters:
value - the integer value
Returns:
the boolean value

booleanToInt

public static int booleanToInt(boolean value)
Converts a boolean value into an integer value.

Parameters:
value - the boolean value
Returns:
the integer value

close

public static boolean close(Closeable closeable)
Closes the given Closeable. Ignores any Throwable.

Parameters:
closeable - The item to close. Can be null.
Returns:
true if the Closeable was closed successful.

close

public static void close(MulticastSocket socket)
Closes the given socket. Avoids any exceptions.

Parameters:
socket - The socket. Can be null.

close

public static void close(Socket socket)
Closes the given socket. Avoids any exceptions.

Parameters:
socket - The socket. Can be null.

close

public static void close(ServerSocket socket)
Closes the given socket. Avoids any exceptions.

Parameters:
socket - The socket. Can be null.

isNullOrEmpty

public static boolean isNullOrEmpty(String str)
Checks if the given string is null or empty.

Parameters:
str - The string.
Returns:
True if the given string is null or empty.

getConsoleLogger

@Deprecated
public static Logger getConsoleLogger(Class<?> clazz)
Deprecated. 

Parameters:
clazz - The given class
Returns:
the application logger of the given class

getConsoleLogger

@Deprecated
public static Logger getConsoleLogger(String className)
Deprecated. 

Parameters:
className - The given class name
Returns:
the application logger of the given class

waitOnLatch

public static boolean waitOnLatch(CountDownLatch latch,
                                  long timeout,
                                  TimeUnit unit)
Waits on a latch for a specified time.

Parameters:
latch - The latch on which waiting shoud occur.
timeout - The timeout value.
unit - The timeout unit.
Returns:
True if the latch went to 0 before timeout of false if not or an interrupted exception occured.

hasSameData

public static boolean hasSameData(Collection<? extends IDataEquatable> one,
                                  Collection<? extends IDataEquatable> other)
Compares all elements in the committed containers.

Parameters:
one - The first container that should be compared
other - The second container that should be compared
Returns:
True if the containers are equal

getLogger

@Deprecated
public static Logger getLogger(Class<?> loggerClass)
Deprecated. Please use 'LoggerFactory.getLogger(Class loggerClass)'

Returns a logger with the classname as identifier.

Parameters:
loggerClass - The class that uses the logger
Returns:
the Logger to be used

getLogger

@Deprecated
public static Logger getLogger(String loggerName)
Deprecated. Please use 'LoggerFactory.getLogger(String loggerName)'

Returns a logger with the classname as identifier.

Parameters:
loggerName - The class that uses the logger
Returns:
the Logger to be used

stringJoin

public static String stringJoin(Collection<? extends Object> items,
                                String separator)
Joins the String.valueOf() representation of the items in the collections separated by the given separator into a new string.

Parameters:
items - The collection of items. Can be empty or contain null values.
separator - separator to put in between the items
Returns:
the joined string representations

urlToURI

public static URI urlToURI(URL url)
                    throws URISyntaxException
Returns a URI equivalent to the given URL. This method can handle special characters like spaces where URL.toURI() would throw an URISyntaxException.

Parameters:
url - the URL to be converted.
Returns:
an URI instance equivalent to the URL argument.
Throws:
URISyntaxException - if this URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI.

appendToURI

public static URI appendToURI(URI uri,
                              String pathSuffix)
                       throws URISyntaxException
Returns a new URI with the given path suffix appended. This method can handle special characters like spaces where URI(String) would throw an URISyntaxException.

Parameters:
uri - the base URI.
pathSuffix - the partial path to be appended to the scheme specific path of the base URI
Returns:
a new URI instance with the extended path.
Throws:
URISyntaxException - if this URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI.

convertSunriseTime

public static Date convertSunriseTime(int[] time)
Converts the timestamp received from Sunrise Framework to a Date object. The returned time is in GMT.

Parameters:
time - Sunrise Framework time. Array size must be two. The first integer contains the number of the seconds since 1970. The second integer contains the number of nanoseconds.
Returns:
Date object representing the time of the sunrise framework.

trim

public static String trim(String original)
Returns a copy of the string, with leading and trailing whitespace omitted. Furthermore all multiple whitespaces and new-line elements within the string are omitted.

Parameters:
original - the original string
Returns:
the trimmed string.

reverse

public static void reverse(byte[] array)
Reverses the given byte array and stores the result in the same array. object.

Parameters:
array - the array to reverse.

printHexDump

public static void printHexDump(PrintWriter pw,
                                byte[] data)
Prints the given data in a pretty-printed hex dump format. The function call printHexDump(pw,data)is equivalent to printHexDump(pw, data, 0, data.length).

Parameters:
pw - a PrintWriter to which the hex dump will be printed
data - data bytes to be dumped
See Also:
printHexDump(PrintWriter, byte[], int, int)

printHexDump

public static void printHexDump(PrintWriter pw,
                                byte[] data,
                                int offset,
                                int length)
Prints the given data in a pretty-printed hex dump format. Example usage:
 PrintWriter pw = new PrintWriter(System.out);
 CommonUtil.printHexDump(pw, data, offset, length);
 
The hex dump of "Hello World, nice to meet you!\n".getBytes() will look like this:
 0000  48 65 6c 6c 6f 20 57 6f  72 6c 64 2c 20 6e 69 63   Hello Wo rld, nic
 0010  65 20 74 6f 20 6d 65 65  74 20 79 6f 75 21 0a      e to mee t you!.
 

Parameters:
pw - a PrintWriter to which the hex dump will be printed
data - data bytes to be dumped
offset - the offset in the array at which to start the dump
length - total number of bytes to dump. The dump will stop with the last byte in the array if the given offset and length exceed the array bounds.
Throws:
ArrayIndexOutOfBoundsException - if offset is negative, or greater or equal data.length

capitalize

public static String capitalize(String string)
Capitalizes the given string.

If the argument is null, the method will return null.

Parameters:
string -
Returns:
capitalized string

decapitalize

public static String decapitalize(String string)
Decapitalizes the given string.

If the argument is null, the method will return null.

Parameters:
string -
Returns:
decapitalized string


Copyright © 2019. All rights reserved.