Class ConfigPropertiesCascadeCommonUtils

  • Direct Known Subclasses:
    ConfigPropertiesCascadeUtils

    public class ConfigPropertiesCascadeCommonUtils
    extends Object
    utility methods for grouper. This code is copied from the Grouper project
    Author:
    mchyzer
    • Field Detail

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        The name says it all.
        See Also:
        Constant Field Values
      • EMPTY_STRING_ARRAY

        public static final String[] EMPTY_STRING_ARRAY
        An empty immutable String array.
    • Constructor Detail

      • ConfigPropertiesCascadeCommonUtils

        public ConfigPropertiesCascadeCommonUtils()
    • Method Detail

      • fileCanonicalPath

        public static String fileCanonicalPath​(File file)
        get canonical path of file
        Parameters:
        file - The file from which the canonical path will be extracted
        Returns:
        the canonical path
        See Also:
        File.getCanonicalPath()
      • computeUrl

        public static URL computeUrl​(String resourceName,
                                     boolean canBeNull)
        compute a url of a resource
        Parameters:
        resourceName - The resource name for which a URL will be built
        canBeNull - if can't be null, throw runtime
        Returns:
        the URL for the resource name
        See Also:
        ClassLoader.getResource(String)
      • classLoader

        public static ClassLoader classLoader()
        fast class loader
        Returns:
        the class loader
      • prefixOrSuffix

        public static String prefixOrSuffix​(String startString,
                                            String separator,
                                            boolean isPrefix)
        get the prefix or suffix of a string based on a separator
        Parameters:
        startString - is the string to start with
        separator - is the separator to split on
        isPrefix - if thre prefix or suffix should be returned
        Returns:
        the prefix or suffix, if the separator isnt there, return the original string
      • newInstance

        public static <T> T newInstance​(Class<T> theClass)
        Construct a class
        Type Parameters:
        T - template type
        Parameters:
        theClass - the class
        Returns:
        the instance
      • newInstance

        public static <T> T newInstance​(Class<T> theClass,
                                        boolean allowPrivateConstructor)
        Construct a class
        Type Parameters:
        T - template type
        Parameters:
        theClass - the class
        allowPrivateConstructor - true if should allow private constructors
        Returns:
        the instance
      • MapToString

        @Deprecated
        public static String MapToString​(Map map)
        Deprecated.
        use mapToString(map)
        convert a set to a string (comma separate)
        Parameters:
        map - the map to convert into a human-readable string
        Returns:
        the String
      • mapToString

        public static String mapToString​(Map map)
        convert a set to a string (comma separate)
        Parameters:
        map - the map to convert into a human-readable string
        Returns:
        the String
      • splitTrim

        public static String[] splitTrim​(String input,
                                         String separator)
        split a string based on a separator into an array, and trim each entry (see the Commons Util trim() for more details)
        Parameters:
        input - is the delimited input to split and trim
        separator - is what to split on
        Returns:
        the array of items after split and trimmed, or null if input is null. will be trimmed to empty
      • splitTrimToList

        public static List<String> splitTrimToList​(String input,
                                                   String separator)
        split a string based on a separator into an array, and trim each entry (see the Commons Util trim() for more details)
        Parameters:
        input - is the delimited input to split and trim
        separator - is what to split on
        Returns:
        the list of items after split and trimmed, or null if input is null. will be trimmed to empty
      • splitTrim

        public static String[] splitTrim​(String input,
                                         String separator,
                                         boolean treatAdjacentSeparatorsAsOne)
        split a string based on a separator into an array, and trim each entry (see the Commons Util trim() for more details)
        Parameters:
        input - is the delimited input to split and trim
        separator - is what to split on
        treatAdjacentSeparatorsAsOne - when true, adjacent separators are treaded as one
        Returns:
        the array of items after split and trimmed, or null if input is null. will be trimmed to empty
      • toList

        public static <T> List<T> toList​(T... objects)
        return a list of objects from varargs. Though if there is one object, and it is a list, return it.
        Type Parameters:
        T - template type of the objects
        Parameters:
        objects - The arguments to be returned as a List
        Returns:
        the list or null if objects is null
      • className

        public static String className​(Object object)
        null safe classname method, gets the unenhanced name
        Parameters:
        object - The object whose class name is desired
        Returns:
        the classname, or null if the object was null
      • intValue

        public static int intValue​(Object input)
        convert an object to a int
        Parameters:
        input - the object (String or Number) to parse or convert to an int
        Returns:
        the number
      • closeQuietly

        public static void closeQuietly​(InputStream input)
        Unconditionally close an InputStream. Equivalent to InputStream.close(), except any exceptions will be ignored.
        Parameters:
        input - A (possibly null) InputStream
      • toString

        public static String toString​(InputStream input,
                                      String encoding)
                               throws IOException
        Get the contents of an InputStream as a String.
        Parameters:
        input - the InputStream to read from
        encoding - The name of a supported character encoding. See the IANA Charset Registry for a list of valid encoding types.
        Returns:
        the requested String
        Throws:
        IOException - In case of an I/O problem
      • copy

        public static void copy​(InputStream input,
                                Writer output,
                                String encoding)
                         throws IOException
        Copy and convert bytes from an InputStream to chars on a Writer, using the specified encoding.
        Parameters:
        input - the InputStream to read from
        output - the Writer to write to
        encoding - The name of a supported character encoding. See the IANA Charset Registry for a list of valid encoding types.
        Throws:
        IOException - In case of an I/O problem
      • copy

        public static int copy​(Reader input,
                               Writer output)
                        throws IOException
        Copy chars from a Reader to a Writer.
        Parameters:
        input - the Reader to read from
        output - the Writer to write to
        Returns:
        the number of characters copied
        Throws:
        IOException - In case of an I/O problem
      • enumValueOfIgnoreCase

        public static <E extends Enum<?>> E enumValueOfIgnoreCase​(Class<E> theEnumClass,
                                                                  String string,
                                                                  boolean exceptionOnNotFound)
                                                           throws RuntimeException
        do a case-insensitive matching
        Type Parameters:
        E - generic type
        Parameters:
        theEnumClass - class of the enum
        string - The name of an enum constant
        exceptionOnNotFound - true if exception should be thrown on not found
        Returns:
        the enum or null or exception if not found
        Throws:
        RuntimeException - if there is a problem
      • equals

        public static boolean equals​(String first,
                                     String second)
        null safe string compare
        Parameters:
        first - first string, or null
        second - second string, or null
        Returns:
        true if equal
      • isBlank

        public static boolean isBlank​(String str)

        Checks if a String is whitespace, empty ("") or null.

         isBlank(null)      = true
         isBlank("")        = true
         isBlank(" ")       = true
         isBlank("bob")     = false
         isBlank("  bob  ") = false
         
        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is null, empty or whitespace
        Since:
        2.0
      • trim

        public static String trim​(String str)
        trim whitespace from string
        Parameters:
        str - string to trim
        Returns:
        trimmed string
      • equalsIgnoreCase

        public static boolean equalsIgnoreCase​(String str1,
                                               String str2)
        null-safe equalsignorecase
        Parameters:
        str1 - first string
        str2 - second string
        Returns:
        true if the strings are equal ignore case
      • split

        public static String[] split​(String str,
                                     String separatorChars)

        Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.

        The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

        A null input String returns null. A null separatorChars splits on whitespace.

         StringUtils.split(null, *)         = null
         StringUtils.split("", *)           = []
         StringUtils.split("abc def", null) = ["abc", "def"]
         StringUtils.split("abc def", " ")  = ["abc", "def"]
         StringUtils.split("abc  def", " ") = ["abc", "def"]
         StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
         
        Parameters:
        str - the String to parse, may be null
        separatorChars - the characters used as the delimiters, null splits on whitespace
        Returns:
        an array of parsed Strings, null if null String input
      • splitPreserveAllTokens

        public static String[] splitPreserveAllTokens​(String str,
                                                      String separatorChars)

        Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.

        The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.

        A null input String returns null. A null separatorChars splits on whitespace.

         StringUtils.splitPreserveAllTokens(null, *)           = null
         StringUtils.splitPreserveAllTokens("", *)             = []
         StringUtils.splitPreserveAllTokens("abc def", null)   = ["abc", "def"]
         StringUtils.splitPreserveAllTokens("abc def", " ")    = ["abc", "def"]
         StringUtils.splitPreserveAllTokens("abc  def", " ")   = ["abc", "", def"]
         StringUtils.splitPreserveAllTokens("ab:cd:ef", ":")   = ["ab", "cd", "ef"]
         StringUtils.splitPreserveAllTokens("ab:cd:ef:", ":")  = ["ab", "cd", "ef", ""]
         StringUtils.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""]
         StringUtils.splitPreserveAllTokens("ab::cd:ef", ":")  = ["ab", "", cd", "ef"]
         StringUtils.splitPreserveAllTokens(":cd:ef", ":")     = ["", cd", "ef"]
         StringUtils.splitPreserveAllTokens("::cd:ef", ":")    = ["", "", cd", "ef"]
         StringUtils.splitPreserveAllTokens(":cd:ef:", ":")    = ["", cd", "ef", ""]
         
        Parameters:
        str - the String to parse, may be null
        separatorChars - the characters used as the delimiters, null splits on whitespace
        Returns:
        an array of parsed Strings, null if null String input
        Since:
        2.1
      • defaultString

        public static String defaultString​(String str)

        Returns either the passed in String, or if the String is null, an empty String ("").

         StringUtils.defaultString(null)  = ""
         StringUtils.defaultString("")    = ""
         StringUtils.defaultString("bat") = "bat"
         
        Parameters:
        str - the String to check, may be null
        Returns:
        the passed in String, or the empty String if it was null
        See Also:
        String.valueOf(Object)
      • jarFile

        public static File jarFile​(Class sampleClass)
        get a jar file from a sample class
        Parameters:
        sampleClass - the class for which the jar is looked up
        Returns:
        the jar file
      • stripLastSlashIfExists

        public static String stripLastSlashIfExists​(String input)
        strip the last slash (/ or \) from a string if it exists
        Parameters:
        input - A string potentially ending in '\' or '/'
        Returns:
        input without the last / or \