Package org.apache.cayenne.util
Class Util
- java.lang.Object
-
- org.apache.cayenne.util.Util
-
public class Util extends Object
Contains various unorganized static utility methods used across Cayenne.
-
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Stringcapitalized(String name)Capitalizes the first letter of the property name.static <T extends Serializable>
TcloneViaSerialization(T object)Creates Serializable object copy using serialization/deserialization.static intcountMatches(String str, String sub)Returns how many times the sub appears in the str.static XMLReadercreateXmlReader()Creates an XMLReader with default feature set.static StringencodeXmlAttribute(String string)Encodes a string so that it can be used as an attribute value in an XML document.static StringextractFileExtension(String fileName)Extracts extension from the file name.static Class<?>getJavaClass(String className)Deprecated.since 4.0 this method based on statically defined class loading algorithm is not going to work in environments like OSGi.static StringgetPackagePath(String className)Returns package name for the Java class as a path separated with forward slash ("/").static inthashCode(Collection<?> c)Builds a hashCode of Collection.static booleanisAccessible(Member member)Returns true if a Member is accessible via reflection under normal Java access controls.static booleanisBlank(CharSequence cs)Returns true, if string not empty and contains non-whitespace characters.static booleanisEmptyString(CharSequence string)Returns true, if the String is null or an empty string.static booleanisNumeric(CharSequence cs)static Stringjoin(Iterable<?> objects, String separator)static <T> intnullSafeCompare(boolean nullsFirst, Comparable<T> o1, T o2)Compares two objects similar to "Comparable.compareTo(Object)".static booleannullSafeEquals(Object o1, Object o2)Compares two objects similar to "Object.equals(Object)".static StringprettyTrim(String str, int maxLength)Trims long strings substituting middle part with "...".static DocumentreadDocument(URL url)static <T> Iterator<T>sortedIterator(Iterator<T> it, Comparator<T> comparator)Returns a sorted iterator from an unsorted one.static StringspecialCharsToJava(String string)Replaces special chars with human-readable and Java-id-compatible symbols.static PatternsqlPatternToPattern(String pattern, boolean ignoreCase)static StringstringFromFile(File file)Reads file contents, returning it as a String, using System default line separator.static StringstringFromFile(File file, String joinWith)Reads file contents, returning it as a String, joining lines with provided separator.static StringstripFileExtension(String fileName)Strips extension from the file name.static StringstripLineBreaks(String string, char replaceWith)Strips "\n", "\r\n", "\r" from the argument string, replacing them with a provided character.static StringstripPackageName(String className)Returns an unqualified class name for the fully qualified name.static StringsubstBackslashes(String string)Replaces all backslashes "\" with forward slashes "/".static FiletoFile(URL url)Converts URL to file.static <K,V>
Map<K,V>toMap(K[] keys, V[] values)Creates a mutable map out of two arrays with keys and values.static Stringuncapitalized(String aString)Returns string with lowercased first letterstatic StringunderscoredToJava(String name, boolean capitalize)Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123".static ThrowableunwindException(Throwable th)Looks up and returns the root cause of an exception.
-
-
-
Method Detail
-
toFile
public static File toFile(URL url) throws IllegalArgumentException
Converts URL to file. ThrowsIllegalArgumentExceptionif the URL is not a "file://" URL.- Throws:
IllegalArgumentException
-
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
-
join
public static String join(Iterable<?> objects, String separator)
- Parameters:
objects- An Iterable of objects that will be converted to Strings and joined together.separator- The separator between the strings.- Returns:
- A single string of all the input strings separated by the separator.
-
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(CharSequence string)
Returns true, if the String is null or an empty string.
-
isBlank
public static boolean isBlank(CharSequence cs)
Returns true, if string not empty and contains non-whitespace characters.- Since:
- 4.1
-
isNumeric
public static boolean isNumeric(CharSequence cs)
- Since:
- 4.1
-
countMatches
public static int countMatches(String str, String sub)
Returns how many times the sub appears in the str.- Since:
- 4.1
-
capitalized
public static String capitalized(String name)
Capitalizes the first letter of the property name.- Since:
- 4.1
-
uncapitalized
public static String uncapitalized(String aString)
Returns string with lowercased first letter- Since:
- 4.2
-
cloneViaSerialization
public static <T extends Serializable> T cloneViaSerialization(T object) 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.
-
readDocument
public static Document readDocument(URL url)
- Parameters:
url- to read- Returns:
- org.w3c.dom.Document from the given URL
- Since:
- 4.1
-
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, char replaceWith)
Strips "\n", "\r\n", "\r" from the argument string, replacing them with a provided character.- Since:
- 3.1
-
encodeXmlAttribute
public static String encodeXmlAttribute(String string)
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
@Deprecated public static Class<?> getJavaClass(String className) throws ClassNotFoundException
Deprecated.since 4.0 this method based on statically defined class loading algorithm is not going to work in environments like OSGi.AdhocObjectFactoryshould be used as it can provide the environment-specific class loading policy.Creates a Java class, handling regular class names as well as single-dimensional arrays and primitive types.- Throws:
ClassNotFoundException- Since:
- 1.2
-
underscoredToJava
public static String underscoredToJava(String name, boolean capitalize)
Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123". Ifcapitalizeis true, returned name is capitalized (for instance if this is a class name).- Since:
- 4.0
-
-