sk.baka.ambient.commons
Class MiscUtils

java.lang.Object
  extended by sk.baka.ambient.commons.MiscUtils

public final class MiscUtils
extends Object

Miscellaneous utility methods.

Author:
Martin Vysny

Field Summary
static Object NULL
          May be used in maps and/or sets which does not support null.
 
Method Summary
static void closeQuietly(Closeable c)
          Closes the closeable object.
static void closeQuietly(ServerSocket socket)
          Closes a server socket quietly.
static void closeQuietly(Socket socket)
          Closes a socket quietly.
static Serializable deserialize(byte[] serialized)
          Deserializes an object from a byte array.
static Serializable deserialize(InputStream stream)
          Deserializes an object from an input stream.
static Serializable deserializeFromFile(File file)
          Deserializes an object from contents of given file.
static String emptyIfNull(String str)
          Returns given string.
static String fixArtistAlbumName(String artistAlbum)
          Moves the leading "the" word to the end of given name.
static String format(int resId, Object... args)
          Formats given string.
static String formatByteLength(long length)
          Formats the length and returns it.
static byte[] fromHexa(String hexaArray)
          Converts hexadecimal string returned by toHexa(byte[]) back to the original array.
static
<T> Ref<T>
getIdentity(T object)
          Wraps given object to a reference.
static List<Class<?>> getInterfaces(Class<?> clazz)
          Returns all interfaces that given class and all superclasses implements.
static String getRFC2822Date(Date date)
          Formats given date as a RFC 2822 date.
static String getStackTrace(Throwable throwable)
          Returns given exception (including its stack trace) as a string.
static int hashCode(Object o)
          Returns object hash code.
static boolean isEmpty(String str)
          Checks if given string is empty (null or zero-sized).
static boolean isEmptyOrWhitespace(String str)
          Checks if given string is empty or consists of whitespace characters only.
static
<T> List<T>
iterableToList(Iterable<? extends T> i)
          Iterates over given iterable and returns its items as a list.
static
<T extends Enum<T>>
T
nextOrThis(T constant, boolean next)
          Returns next enum constant.
static
<T extends Comparable<? super T>>
int
nullCompare(T o1, T o2, boolean nullFirst)
          Compares two objects.
static
<T> boolean
nullEquals(T o1, T o2)
          Similar to nullCompare(Comparable, Comparable, boolean), but uses Object.equals(Object) instead.
static String nullIfEmptyOrWhitespace(String str)
          If given string is empty, contains spaces only or is null then null is returned.
static Date parseRFC2822Date(String date)
          Parses RFC 2822 date.
static void serialize(Serializable object, OutputStream out)
          Serializes given object to the output stream.
static byte[] serializeToBytes(Serializable object)
          Serializes given object to the output stream.
static void sysout(String string)
          Sysout for the fucking Android.
static String toHexa(byte[] array)
          Formats each byte in the array in its hexadecimal form and returns it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL

public static final Object NULL
May be used in maps and/or sets which does not support null.

Method Detail

nullCompare

public static <T extends Comparable<? super T>> int nullCompare(T o1,
                                                                T o2,
                                                                boolean nullFirst)
Compares two objects. Handles nulls correctly. For return value please see the Comparable.compareTo(Object) contract.

Type Parameters:
T - the object type
Parameters:
o1 - first object
o2 - second object
nullFirst - if true then null is less than any non-null object. If false then null is greater than any non-null object.
Returns:
see Comparable.compareTo(Object) for details.

nextOrThis

public static <T extends Enum<T>> T nextOrThis(T constant,
                                               boolean next)
Returns next enum constant. If there is no next constant then returns the first one.

Type Parameters:
T - the enum type
Parameters:
constant - the constant, must not be null.
next - if true then next constant is returned, if false then simply given constant is returned.
Returns:
the next constant.

getInterfaces

public static List<Class<?>> getInterfaces(Class<?> clazz)
Returns all interfaces that given class and all superclasses implements.

Parameters:
clazz - the class to analyze
Returns:
interface list that the class is castable to.

nullIfEmptyOrWhitespace

public static String nullIfEmptyOrWhitespace(String str)
If given string is empty, contains spaces only or is null then null is returned.

Parameters:
str - the string.
Returns:
string or null if one of the above applies.

emptyIfNull

public static String emptyIfNull(String str)
Returns given string. If given string is null then an empty string is returned.

Parameters:
str - the string.
Returns:
non-null string.

isEmptyOrWhitespace

public static boolean isEmptyOrWhitespace(String str)
Checks if given string is empty or consists of whitespace characters only.

Parameters:
str - the string to check.
Returns:
true if the string is empty, false otherwise.

isEmpty

public static boolean isEmpty(String str)
Checks if given string is empty (null or zero-sized).

Parameters:
str - the string to check.
Returns:
true if the string is empty, false otherwise.

format

public static String format(int resId,
                            Object... args)
Formats given string. Uses Formatter.

Parameters:
resId - formatting string id
args - parameters
Returns:
formatted string

nullEquals

public static <T> boolean nullEquals(T o1,
                                     T o2)
Similar to nullCompare(Comparable, Comparable, boolean), but uses Object.equals(Object) instead.

Type Parameters:
T - the object type
Parameters:
o1 - first object
o2 - second object
Returns:
true if either o1 is equal to o2, or both are nulls; false otherwise.

deserializeFromFile

public static Serializable deserializeFromFile(File file)
                                        throws IOException
Deserializes an object from contents of given file.

Parameters:
file - the file to read
Returns:
deserialized object.
Throws:
IOException

deserialize

public static Serializable deserialize(InputStream stream)
                                throws IOException
Deserializes an object from an input stream. The stream is closed.

Parameters:
stream - the stream to read
Returns:
deserialized object.
Throws:
IOException

serialize

public static void serialize(Serializable object,
                             OutputStream out)
                      throws IOException
Serializes given object to the output stream. The stream is closed.

Parameters:
object - the object to serialize
out - serialize here
Throws:
IOException

serializeToBytes

public static byte[] serializeToBytes(Serializable object)
Serializes given object to the output stream. The stream is closed.

Parameters:
object - the object to serialize
Returns:
serialized form

deserialize

public static Serializable deserialize(byte[] serialized)
Deserializes an object from a byte array.

Parameters:
serialized - the serialized object form
Returns:
deserialized object.

closeQuietly

public static void closeQuietly(Closeable c)
Closes the closeable object. Catches any exception.

Parameters:
c - close this closeable. Does nothing if the stream is null.

closeQuietly

public static void closeQuietly(Socket socket)
Closes a socket quietly.

Parameters:
socket - the socket to close.

closeQuietly

public static void closeQuietly(ServerSocket socket)
Closes a server socket quietly.

Parameters:
socket - the socket to close.

iterableToList

public static <T> List<T> iterableToList(Iterable<? extends T> i)
Iterates over given iterable and returns its items as a list.

Type Parameters:
T - the item type
Parameters:
i - the iterable
Returns:
list, never null.

formatByteLength

public static String formatByteLength(long length)
Formats the length and returns it.

Parameters:
length - in bytes
Returns:
xxKb/xxMb/etc

fixArtistAlbumName

public static String fixArtistAlbumName(String artistAlbum)
Moves the leading "the" word to the end of given name.

Parameters:
artistAlbum - an artist/album name
Returns:
fixed name

getIdentity

public static <T> Ref<T> getIdentity(T object)
Wraps given object to a reference. The purpose of this reference is to enforce reference-equality in place of object-equality. In other words, two references returned by this method are considered equal if and only if r1.get() == r2.get().

Type Parameters:
T - the object type.
Parameters:
object - the object instance. nulls are handled correctly, i.e. null is only equal to null.
Returns:
a reference to given object.

hashCode

public static int hashCode(Object o)
Returns object hash code. Returns 0 if null was given.

Parameters:
o - the object to compute hash code for.
Returns:
the hash code.

parseRFC2822Date

public static Date parseRFC2822Date(String date)
                             throws ParseException
Parses RFC 2822 date.

Parameters:
date - the date to parse.
Returns:
parsed date.
Throws:
ParseException - if parse fails.

getRFC2822Date

public static String getRFC2822Date(Date date)
Formats given date as a RFC 2822 date.

Parameters:
date - the date to format.
Returns:
formatted date.

toHexa

public static String toHexa(byte[] array)
Formats each byte in the array in its hexadecimal form and returns it.

Parameters:
array - the array to print
Returns:
hexadecimal format

fromHexa

public static byte[] fromHexa(String hexaArray)
Converts hexadecimal string returned by toHexa(byte[]) back to the original array.

Parameters:
hexaArray - the array returned by toHexa(byte[]).
Returns:
the array.

getStackTrace

public static String getStackTrace(Throwable throwable)
Returns given exception (including its stack trace) as a string.

Parameters:
throwable - the throwable.
Returns:
string representation of the throwable.

sysout

public static void sysout(String string)
Sysout for the fucking Android.

Parameters:
string - the fucking string.


Copyright © 2007-2008. All Rights Reserved.