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 SummaryConstructors Constructor Description Util()
- 
Method SummaryModifier 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.
- 
Constructor Details- 
Utilpublic Util()
 
- 
- 
Method Details- 
toFileConverts URL to file. ThrowsIllegalArgumentExceptionif the URL is not a "file://" URL.- Throws:
- IllegalArgumentException
 
- 
stringFromFileReads file contents, returning it as a String, using System default line separator.- Throws:
- IOException
 
- 
stringFromFileReads file contents, returning it as a String, joining lines with provided separator.- Throws:
- IOException
 
- 
join- 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.
 
- 
substBackslashesReplaces all backslashes "\" with forward slashes "/". Convenience method to convert path Strings to URI format.
- 
unwindExceptionLooks 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.
- 
nullSafeEqualsCompares 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.
- 
nullSafeCompareCompares 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
 
- 
isEmptyStringReturns true, if the String is null or an empty string.
- 
isBlankReturns true, if string not empty and contains non-whitespace characters.- Since:
- 4.1
 
- 
isNumeric- Since:
- 4.1
 
- 
countMatchesReturns how many times the sub appears in the str.- Since:
- 4.1
 
- 
capitalizedCapitalizes the first letter of the property name.- Since:
- 4.1
 
- 
uncapitalizedReturns string with lowercased first letter- Since:
- 4.2
 
- 
cloneViaSerializationCreates Serializable object copy using serialization/deserialization.- Throws:
- Exception
 
- 
createXmlReaderCreates 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- Parameters:
- url- to read
- Returns:
- org.w3c.dom.Document from the given URL
- Since:
- 4.1
 
- 
getPackagePathReturns 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".
- 
stripPackageNameReturns an unqualified class name for the fully qualified name.- Since:
- 3.0
 
- 
toMapCreates a mutable map out of two arrays with keys and values.- Since:
- 1.2
 
- 
extractFileExtensionExtracts extension from the file name. Dot is not included in the returned string.
- 
stripFileExtensionStrips extension from the file name.
- 
stripLineBreaksStrips "\n", "\r\n", "\r" from the argument string, replacing them with a provided character.- Since:
- 3.1
 
- 
encodeXmlAttributeEncodes 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.
- 
prettyTrimTrims 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
 
- 
sortedIteratorReturns 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.
- 
hashCodeBuilds a hashCode of Collection.
- 
sqlPatternToPattern- Since:
- 1.2
 
- 
isAccessibleReturns true if a Member is accessible via reflection under normal Java access controls.- Since:
- 1.2
 
- 
getJavaClassDeprecated.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
 
- 
underscoredToJavaConverts 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
 
- 
specialCharsToJavaReplaces special chars with human-readable and Java-id-compatible symbols.- Since:
- 4.0
 
 
-