# 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

  Modifier and Type
  Method
  Description
  `static String`
  `capitalized(String name)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static <T extends Serializable>
  T`
  `cloneViaSerialization(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`
  `createXmlReader()`
  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.
  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)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `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`
  `isNumeric(CharSequence cs)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  use `cs.chars().allMatch(Character::isDigit)` or equivalent
  `static String`
  `join(Iterable<?> objects, String separator)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  use `String.join(CharSequence, CharSequence...)` or `Collectors.joining()`
  `static <T> int`
  `nullSafeCompare(boolean nullsFirst, Comparable<T> o1, T o2)`
  Deprecated.
  since 5.0 in favor of the JDK's `Comparator.nullsFirst(java.util.Comparator)` / `Comparator.nullsLast(java.util.Comparator)` combined with `Comparator.naturalOrder()`.
  `static boolean`
  `nullSafeEquals(Object o1, Object o2)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Use `Objects.equals(Object, Object)` for non-array types, or `Objects.deepEquals(Object, Object)` when the values may be arrays.
  `static String`
  `prettyTrim(String str, int maxLength)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static Document`
  `readDocument(URL url)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static <T> Iterator<T>`
  `sortedIterator(Iterator<T> it, Comparator<T> comparator)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static String`
  `specialCharsToJava(String string)`
  Replaces special chars with human-readable and Java-id-compatible symbols.
  `static Pattern`
  `sqlPatternToPattern(String pattern, boolean ignoreCase)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static String`
  `stringFromFile(File file)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static String`
  `stringFromFile(File file, String joinWith)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `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`
  `toFile(URL url)`
  Converts URL to file.
  `static <K,V> Map<K,V>`
  `toMap(K[] keys, V[] values)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  use `IntStream.range(int, int)` with `Collectors.toMap(Function, Function)` to build a map from parallel arrays
  `static String`
  `uncapitalized(String aString)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  unused
  `static String`
  `underscoredToJava(String name, boolean capitalize)`
  Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123".
  `static Throwable`
  `unwindException(Throwable th)`
  Looks up and returns the root cause of an exception.
  `static Throwable`
  `unwindException(Throwable th, Class<? extends Throwable> upTo)`
  Unwinds an exception chain like `unwindException(Throwable)`, but never unwinds past the innermost exception that is an instance of `upTo`.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Util

    public Util()

- ## Method Details

  - ### toFile

    public static File toFile(URL url) throws IllegalArgumentException
    Converts URL to file. Throws `IllegalArgumentException` if the URL is not a "file://" URL.
    Throws:
      `IllegalArgumentException`

  - ### stringFromFile

    @Deprecated(since="5.0", forRemoval=true) public static String stringFromFile(File file) throws IOException
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    Reads file contents, returning it as a String, using System default line separator.
    Throws:
      `IOException`

  - ### stringFromFile

    @Deprecated(since="5.0", forRemoval=true) public static String stringFromFile(File file, String joinWith) throws IOException
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    Reads file contents, returning it as a String, joining lines with provided separator.
    Throws:
      `IOException`

  - ### join

    @Deprecated(since="5.0", forRemoval=true) public static String join(Iterable<?> objects, String separator)
    Deprecated, for removal: This API element is subject to removal in a future version.
    use `String.join(CharSequence, CharSequence...)` or `Collectors.joining()`
    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.

  - ### unwindException

    public static Throwable unwindException(Throwable th, Class<? extends Throwable> upTo)
    Unwinds an exception chain like `unwindException(Throwable)`, but never unwinds past the innermost exception that is an instance of `upTo`. If the chain contains such an exception, the innermost matching one is returned; otherwise the fully unwound root cause is returned. This lets callers strip generic wrappers while preserving a meaningful exception type that itself wraps a lower-level cause.
    Since:
      5.0

  - ### nullSafeEquals

    @Deprecated(since="5.0", forRemoval=true) public static boolean nullSafeEquals(Object o1, Object o2)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use `Objects.equals(Object, Object)` for non-array types, or `Objects.deepEquals(Object, Object)` when the values may be arrays.
    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

    @Deprecated(since="5.0") public static <T> int nullSafeCompare(boolean nullsFirst, Comparable<T> o1, T o2)
    Deprecated.
    since 5.0 in favor of the JDK's `Comparator.nullsFirst(java.util.Comparator)` / `Comparator.nullsLast(java.util.Comparator)` combined with `Comparator.naturalOrder()`.
    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

    @Deprecated(since="5.0", forRemoval=true) public static boolean isNumeric(CharSequence cs)
    Deprecated, for removal: This API element is subject to removal in a future version.
    use `cs.chars().allMatch(Character::isDigit)` or equivalent
    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

    @Deprecated(since="5.0", forRemoval=true) public static String capitalized(String name)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    Capitalizes the first letter of the property name.
    Since:
      4.1

  - ### uncapitalized

    @Deprecated(since="5.0", forRemoval=true) public static String uncapitalized(String aString)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    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.
    Throws:
      `SAXException`
      `ParserConfigurationException`

  - ### readDocument

    @Deprecated(since="5.0", forRemoval=true) public static Document readDocument(URL url)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    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

    @Deprecated(since="5.0", forRemoval=true) public static <K,V> Map<K,V> toMap(K[] keys, V[] values)
    Deprecated, for removal: This API element is subject to removal in a future version.
    use `IntStream.range(int, int)` with `Collectors.toMap(Function, Function)` to build a map from parallel arrays
    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

    @Deprecated(since="5.0", forRemoval=true) public static String prettyTrim(String str, int maxLength)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    Trims long strings substituting middle part with "...".

  - ### sortedIterator

    @Deprecated(since="5.0", forRemoval=true) public static <T> Iterator<T> sortedIterator(Iterator<T> it, Comparator<T> comparator)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    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

    @Deprecated(since="5.0", forRemoval=true) public static int hashCode(Collection<?> c)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    Builds a hashCode of Collection.

  - ### sqlPatternToPattern

    @Deprecated(since="5.0", forRemoval=true) public static Pattern sqlPatternToPattern(String pattern, boolean ignoreCase)
    Deprecated, for removal: This API element is subject to removal in a future version.
    unused
    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.
    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