org.apache.cayenne.util
Class Util

java.lang.Object
  extended by org.apache.cayenne.util.Util

public class Util
extends Object

Contains various unorganized static utility methods used across Cayenne.


Constructor Summary
Util()
           
 
Method Summary
static Object cloneViaSerialization(Serializable obj)
          Creates Serializable object copy using serialization/deserialization.
static boolean copy(File source, File destination)
          Copies file contents from source to destination.
static boolean copy(URL from, File to)
          Save URL contents to a file.
static void copyPipe(InputStream in, OutputStream out, int bufSizeHint)
          Reads data from the input and writes it to the output, until the end of the input stream.
static XMLReader createXmlReader()
          Creates an XMLReader with default feature set.
static boolean delete(String filePath, boolean recursive)
          Deletes a file or directory, allowing recursive directory deletion.
static String encodeXmlAttribute(String str)
          Encodes a string so that it can be used as an attribute value in an XML document.
static String extractFileExtension(String fileName)
          Extracts extension from the file name.
static Class<?> getJavaClass(String className)
          Creates a Java class, handling regular class names as well as single-dimensional arrays and primitive types.
static String getPackagePath(String className)
          Returns package name for the Java class as a path separated with forward slash ("/").
static int hashCode(Collection<?> c)
          Builds a hashCode of Collection.
static boolean isAccessible(Member member)
          Returns true if a Member is accessible via reflection under normal Java access controls.
static boolean isEmptyString(String string)
          Returns true, if the String is null or an empty string.
static
<T> int
nullSafeCompare(boolean nullsFirst, Comparable<T> o1, T o2)
          Compares two objects similar to "Comparable.compareTo(Object)".
static boolean nullSafeEquals(Object o1, Object o2)
          Compares two objects similar to "Object.equals(Object)".
static String prettyTrim(String str, int maxLength)
          Trims long strings substituting middle part with "...".
static
<T> Iterator<T>
sortedIterator(Iterator<T> it, Comparator<T> comparator)
          Returns a sorted iterator from an unsorted one.
static Pattern sqlPatternToPattern(String pattern, boolean ignoreCase)
           
static String stringFromFile(File file)
          Reads file contents, returning it as a String, using System default line separator.
static String stringFromFile(File file, String joinWith)
          Reads file contents, returning it as a String, joining lines with provided separator.
static String stripFileExtension(String fileName)
          Strips extension from the file name.
static String stripLineBreaks(String string, String replaceWith)
          Strips "\n", "\r\n", "\r" from the argument string.
static String stripPackageName(String className)
          Returns an unqualified class name for the fully qualified name.
static String substBackslashes(String string)
          Replaces all backslashes "\" with forward slashes "/".
static
<K,V> Map<K,V>
toMap(K[] keys, V[] values)
          Creates a mutable map out of two arrays with keys and values.
static Throwable unwindException(Throwable th)
          Looks up and returns the root cause of an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

stringFromFile

public static String stringFromFile(File file)
                             throws IOException
Reads file contents, returning it as a String, using System default line separator.

Throws:
IOException

stringFromFile

public static String stringFromFile(File file,
                                    String joinWith)
                             throws IOException
Reads file contents, returning it as a String, joining lines with provided separator.

Throws:
IOException

copy

public static boolean copy(File source,
                           File destination)
Copies file contents from source to destination. Makes up for the lack of file copying utilities in Java


copy

public static boolean copy(URL from,
                           File to)
Save URL contents to a file.


copyPipe

public static void copyPipe(InputStream in,
                            OutputStream out,
                            int bufSizeHint)
                     throws IOException
Reads data from the input and writes it to the output, until the end of the input stream.

Parameters:
in -
out -
bufSizeHint -
Throws:
IOException

delete

public static boolean delete(String filePath,
                             boolean recursive)
Deletes a file or directory, allowing recursive directory deletion. This is an improved version of File.delete() method.


substBackslashes

public static String substBackslashes(String string)
Replaces all backslashes "\" with forward slashes "/". Convenience method to convert path Strings to URI format.


unwindException

public static Throwable unwindException(Throwable th)
Looks up and returns the root cause of an exception. If none is found, returns supplied Throwable object unchanged. If root is found, recursively "unwraps" it, and returns the result to the user.


nullSafeEquals

public static boolean nullSafeEquals(Object o1,
                                     Object o2)
Compares two objects similar to "Object.equals(Object)". Unlike Object.equals(..), this method doesn't throw an exception if any of the two objects is null.


nullSafeCompare

public static <T> int nullSafeCompare(boolean nullsFirst,
                                      Comparable<T> o1,
                                      T o2)
Compares two objects similar to "Comparable.compareTo(Object)". Unlike Comparable.compareTo(..), this method doesn't throw an exception if any of the two objects is null.

Since:
1.1

isEmptyString

public static boolean isEmptyString(String string)
Returns true, if the String is null or an empty string.


cloneViaSerialization

public static Object cloneViaSerialization(Serializable obj)
                                    throws Exception
Creates Serializable object copy using serialization/deserialization.

Throws:
Exception

createXmlReader

public static XMLReader createXmlReader()
                                 throws SAXException,
                                        ParserConfigurationException
Creates an XMLReader with default feature set. Note that all Cayenne internal XML parsers should probably use XMLReader obtained via this method for consistency sake, and can customize feature sets as needed.

Throws:
SAXException
ParserConfigurationException

getPackagePath

public static String getPackagePath(String className)
Returns package name for the Java class as a path separated with forward slash ("/"). Method is used to lookup resources that are located in package subdirectories. For example, a String "a/b/c" will be returned for class name "a.b.c.ClassName".


stripPackageName

public static String stripPackageName(String className)
Returns an unqualified class name for the fully qualified name.

Since:
3.0

toMap

public static <K,V> Map<K,V> toMap(K[] keys,
                                   V[] values)
Creates a mutable map out of two arrays with keys and values.

Since:
1.2

extractFileExtension

public static String extractFileExtension(String fileName)
Extracts extension from the file name. Dot is not included in the returned string.


stripFileExtension

public static String stripFileExtension(String fileName)
Strips extension from the file name.


stripLineBreaks

public static String stripLineBreaks(String string,
                                     String replaceWith)
Strips "\n", "\r\n", "\r" from the argument string.

Since:
1.2

encodeXmlAttribute

public static String encodeXmlAttribute(String str)
Encodes a string so that it can be used as an attribute value in an XML document. Will do conversion of the greater/less signs, quotes and ampersands.


prettyTrim

public static String prettyTrim(String str,
                                int maxLength)
Trims long strings substituting middle part with "...".

Parameters:
str - String to trim.
maxLength - maximum allowable length. Must be at least 5, or an IllegalArgumentException is thrown.
Returns:
String

sortedIterator

public static <T> Iterator<T> sortedIterator(Iterator<T> it,
                                             Comparator<T> comparator)
Returns a sorted iterator from an unsorted one. Use this method as a last resort, since it is much less efficient then just sorting a collection that backs the original iterator.


hashCode

public static int hashCode(Collection<?> c)
Builds a hashCode of Collection.


sqlPatternToPattern

public static Pattern sqlPatternToPattern(String pattern,
                                          boolean ignoreCase)
Since:
1.2

isAccessible

public static boolean isAccessible(Member member)
Returns true if a Member is accessible via reflection under normal Java access controls.

Since:
1.2

getJavaClass

public static Class<?> getJavaClass(String className)
                             throws ClassNotFoundException
Creates a Java class, handling regular class names as well as single-dimensional arrays and primitive types.

Throws:
ClassNotFoundException
Since:
1.2


Copyright © 2001-2011 Apache Cayenne. All Rights Reserved.