Package org.apache.cayenne.util
Class Util
java.lang.Object
org.apache.cayenne.util.Util
Contains various unorganized static utility methods used across Cayenne.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
capitalized
(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 int
countMatches
(String str, String sub) Returns how many times the sub appears in the str.static XMLReader
Creates an XMLReader with default feature set.static String
encodeXmlAttribute
(String string) 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) Deprecated.since 4.0 this method based on statically defined class loading algorithm is not going to work in environments like OSGi.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
isBlank
(CharSequence cs) Returns true, if string not empty and contains non-whitespace characters.static boolean
isEmptyString
(CharSequence string) Returns true, if the String is null or an empty string.static boolean
static 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 Document
readDocument
(URL url) static <T> Iterator
<T> sortedIterator
(Iterator<T> it, Comparator<T> comparator) Returns a sorted iterator from an unsorted one.static String
specialCharsToJava
(String string) Replaces special chars with human-readable and Java-id-compatible symbols.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, char replaceWith) Strips "\n", "\r\n", "\r" from the argument string, replacing them with a provided character.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 File
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 String
uncapitalized
(String aString) Returns string with lowercased first letterstatic String
underscoredToJava
(String name, boolean capitalize) Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123".static Throwable
Looks up and returns the root cause of an exception.
-
Constructor Details
-
Util
public Util()
-
-
Method Details
-
toFile
Converts URL to file. ThrowsIllegalArgumentException
if the URL is not a "file://" URL.- Throws:
IllegalArgumentException
-
stringFromFile
Reads file contents, returning it as a String, using System default line separator.- Throws:
IOException
-
stringFromFile
Reads 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.
-
substBackslashes
Replaces all backslashes "\" with forward slashes "/". Convenience method to convert path Strings to URI format. -
unwindException
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
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
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
Returns true, if the String is null or an empty string. -
isBlank
Returns true, if string not empty and contains non-whitespace characters.- Since:
- 4.1
-
isNumeric
- Since:
- 4.1
-
countMatches
Returns how many times the sub appears in the str.- Since:
- 4.1
-
capitalized
Capitalizes the first letter of the property name.- Since:
- 4.1
-
uncapitalized
Returns string with lowercased first letter- Since:
- 4.2
-
cloneViaSerialization
Creates Serializable object copy using serialization/deserialization.- Throws:
Exception
-
createXmlReader
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
- Parameters:
url
- to read- Returns:
- org.w3c.dom.Document from the given URL
- Since:
- 4.1
-
getPackagePath
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
Returns an unqualified class name for the fully qualified name.- Since:
- 3.0
-
toMap
Creates a mutable map out of two arrays with keys and values.- Since:
- 1.2
-
extractFileExtension
Extracts extension from the file name. Dot is not included in the returned string. -
stripFileExtension
Strips extension from the file name. -
stripLineBreaks
Strips "\n", "\r\n", "\r" from the argument string, replacing them with a provided character.- Since:
- 3.1
-
encodeXmlAttribute
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
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
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
Builds a hashCode of Collection. -
sqlPatternToPattern
- Since:
- 1.2
-
isAccessible
Returns true if a Member is accessible via reflection under normal Java access controls.- Since:
- 1.2
-
getJavaClass
Deprecated.since 4.0 this method based on statically defined class loading algorithm is not going to work in environments like OSGi.AdhocObjectFactory
should 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
Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123". Ifcapitalize
is true, returned name is capitalized (for instance if this is a class name).- Since:
- 4.0
-
specialCharsToJava
Replaces special chars with human-readable and Java-id-compatible symbols.- Since:
- 4.0
-