Class Util


  • public class Util
    extends Object
    Contains various unorganized static utility methods used across Cayenne.
    • 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
      • 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
      • 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. 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

        public static String underscoredToJava​(String name,
                                               boolean capitalize)
        Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123". If capitalize is true, returned name is capitalized (for instance if this is a class name).
        Since:
        4.0
      • specialCharsToJava

        public static String specialCharsToJava​(String string)
        Replaces special chars with human-readable and Java-id-compatible symbols.
        Since:
        4.0