sk.baka.tools
Class JavaUtils

java.lang.Object
  extended by sk.baka.tools.JavaUtils

public final class JavaUtils
extends Object

Contains several utilities for classic java classes.

Author:
Martin Vysny

Nested Class Summary
static class JavaUtils.BeanProperty
          Contains accessors to a bean property.
static interface JavaUtils.IEqualsComparator
          Compares two objects.
 
Field Summary
static JavaUtils.IEqualsComparator COMPARATOR_EQUALS
          Compares two objects using equals(java.lang.Object, java.lang.Object).
 
Method Summary
static Date clone(Date date)
          Clones given date object.
static boolean equals(Object o1, Object o2)
          Checks that two objects are equal.
static
<T> JavaUtils.BeanProperty
equalsBean(T bean1, T bean2)
          Compares two beans.
static
<T> JavaUtils.BeanProperty
equalsBean(T bean1, T bean2, JavaUtils.IEqualsComparator comparator)
          Compares two beans.
static List<JavaUtils.BeanProperty> getBeanProperties(Class<?> beanClass, boolean includeSuperclasses)
          Returns all property accessors from given class, and optionally from its superclasses.
static Method getGetter(Field field)
          Returns a getter method for given field, if such a method exists in the same class.
static InputStream getResource(String resource)
          Tries to obtain an input stream for given resource.
static Method getSetter(Field field)
          Returns a setter method for given field, if such a method exists in the same class.
static String getStacktrace(Throwable t)
          Returns a stacktrace of given throwable
static boolean isBlank(Object array)
          Deprecated. use isBlankArray(java.lang.Object)
static boolean isBlankArray(Object array)
          Returns true if given array is null, zero-sized or contains only nulls.
static boolean isEmpty(Object array)
          Deprecated. use #isEmptyList(java.lang.Object)
static boolean isEmptyArray(Object array)
          Returns true if given array is null or zero-sized.
static boolean isEnum(Class<?> clazz)
          A workaround around broken Class.isEnum() which returns false for more complex enums (enums which define abstract methods and let each constant override the method).
static boolean isPrimitiveArray(Object array)
          Checks whether given array is an array of primitives.
static Properties loadProperties(InputStream in)
          Loads properties from given input stream.
static Properties loadProperties(String filename)
          Loads properties from given file.
static List<Object> toList(Object array)
          Creates a new list and copies given array to it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPARATOR_EQUALS

public static final JavaUtils.IEqualsComparator COMPARATOR_EQUALS
Compares two objects using equals(java.lang.Object, java.lang.Object).

Method Detail

clone

public static Date clone(Date date)
Clones given date object.

Parameters:
date - the date to clone, may be null
Returns:
cloned date object or null

loadProperties

public static Properties loadProperties(String filename)
                                 throws IOException
Loads properties from given file.

Parameters:
filename - the file to load the properties from.
Returns:
the properties instance, never null
Throws:
IOException - if i/o error occurs or the file does not exist.

loadProperties

public static Properties loadProperties(InputStream in)
                                 throws IOException
Loads properties from given input stream. The stream is always closed.

Parameters:
in - the input stream, always closed
Returns:
properties instance, never null
Throws:
IOException - on i/o error

getResource

public static InputStream getResource(String resource)
                               throws IOException
Tries to obtain an input stream for given resource. Fails if the resource does not exist. Always uses context class loader.

Parameters:
resource - the resource to read
Returns:
input stream, never null
Throws:
IOException - if the resource is not found

isEmpty

@Deprecated
public static boolean isEmpty(Object array)
Deprecated. use #isEmptyList(java.lang.Object)

Returns true if given array is null or zero-sized. Accepts:

Parameters:
array - the array to check. null array is empty by definition.
Returns:
true if null or zero-sized, false otherwise.

isEmptyArray

public static boolean isEmptyArray(Object array)
Returns true if given array is null or zero-sized. Accepts:

Parameters:
array - the array to check. null array is empty by definition.
Returns:
true if null or zero-sized, false otherwise.

isBlank

@Deprecated
public static boolean isBlank(Object array)
Deprecated. use isBlankArray(java.lang.Object)

Returns true if given array is null, zero-sized or contains only nulls. Accepts: Warning: the operation is destructive on Enumerations and Iterators as items are actually read from them.

Parameters:
array - the array to check, may be both primitive, Object array and a Collection.
Returns:
true if null or zero-sized, false otherwise.

isBlankArray

public static boolean isBlankArray(Object array)
Returns true if given array is null, zero-sized or contains only nulls. Accepts: Warning: the operation is destructive on Enumerations and Iterators as items are actually read from them.

Parameters:
array - the array to check, may be both primitive, Object array and a Collection.
Returns:
true if null or zero-sized, false otherwise.

isPrimitiveArray

public static boolean isPrimitiveArray(Object array)
Checks whether given array is an array of primitives.

Parameters:
array - an array.
Returns:
true if the array is an array of primitives, false if it is a Object[], String[] etc.

getStacktrace

public static String getStacktrace(Throwable t)
Returns a stacktrace of given throwable

Parameters:
t - the throwable to return
Returns:
stacktrace, never null.

equals

public static boolean equals(Object o1,
                             Object o2)
Checks that two objects are equal. Handles nulls.

Parameters:
o1 - first object, may be null
o2 - first object, may be null
Returns:
true if both objects are null or they are equal, false otherwise.

toList

public static List<Object> toList(Object array)
Creates a new list and copies given array to it. Accepts:

Parameters:
array - the array to convert, may be null.
Returns:
a list of objects. An empty list if null was provided.

getGetter

public static Method getGetter(Field field)
Returns a getter method for given field, if such a method exists in the same class.

Parameters:
field - a field reference.
Returns:
getter method or null if no such method exists.

getSetter

public static Method getSetter(Field field)
Returns a setter method for given field, if such a method exists in the same class.

Parameters:
field - a field reference.
Returns:
setter method or null if no such method exists.

getBeanProperties

public static List<JavaUtils.BeanProperty> getBeanProperties(Class<?> beanClass,
                                                             boolean includeSuperclasses)
Returns all property accessors from given class, and optionally from its superclasses.

Parameters:
beanClass - the bean class
includeSuperclasses - if true then class superclasses are considered as well.
Returns:
list of fields and their getters

equalsBean

public static <T> JavaUtils.BeanProperty equalsBean(T bean1,
                                                    T bean2)
Compares two beans. Fails if some of their properties are not equal. Uses regular Object.equals(java.lang.Object) on field values (does not perform recursive comparison).

Type Parameters:
T - the bean type
Parameters:
bean1 - first bean, must not be null
bean2 - second bean, must not be null
Returns:
null if beans equals, a mismatched property reference otherwise.
Throws:
IllegalArgumentException - if beans do not have same class.
NullPointerException - if at least one of the parameter is null

equalsBean

public static <T> JavaUtils.BeanProperty equalsBean(T bean1,
                                                    T bean2,
                                                    JavaUtils.IEqualsComparator comparator)
Compares two beans. Fails if some of their properties are not equal. Uses given comparator to compare field values.

Type Parameters:
T - the bean type
Parameters:
bean1 - first bean, must not be null
bean2 - second bean, must not be null
comparator - use this comparator when comparing bean property values.
Returns:
null if beans equals, a mismatched property reference otherwise.
Throws:
IllegalArgumentException - if beans do not have same class.
NullPointerException - if at least one of the parameter is null

isEnum

public static boolean isEnum(Class<?> clazz)
A workaround around broken Class.isEnum() which returns false for more complex enums (enums which define abstract methods and let each constant override the method).

Parameters:
clazz -
Returns:


Copyright © 2009-2010. All Rights Reserved.