Class SParser

  • All Implemented Interfaces:

    
    public class SParser
    
                        

    A common string parser

    Author:

    vincent

    • Constructor Detail

      • SParser

        SParser()
        Constructor for SParser.
    • Method Detail

      • setClassLoader

         void setClassLoader(ClassLoader classLoader)

        Set the class loader that will be used to load the various objects.

        Parameters:
        classLoader - the new class loader.
      • get

         String get()

        Get the value of property, null if no such property exists.

        Values are processed for variable expansion before being returned.

        Returns:

        the value of or its replacing property, or null if no such property exists.

      • getTrimmed

         String getTrimmed()

        Get the value of property as a trimmed String, null if no such property exists.

        Values are processed for variable expansion before being returned.

        Returns:

        the value of or its replacing property, or null if no such property exists.

      • getTrimmed

         String getTrimmed(String defaultValue)

        Get the value of property as a trimmed String, defaultValue if no such property exists. See @{Configuration#getTrimmed} for more details.

        Parameters:
        defaultValue - the property default value.
        Returns:

        the value of or defaultValue if it is not set.

      • getInt

         int getInt(int defaultValue)

        Get an integer value, if the configured value is invalid or not set, return the default value.

      • getInts

         Array<int> getInts()

        getInts.

        Returns:

        an array of objects.

      • setInt

         void setInt(int value)

        setInt.

        Parameters:
        value - a int.
      • getLong

         long getLong(long defaultValue)

        Get the value of property as a long. If no such property exists, the provided default value is returned, or if the specified value is not a valid long, then an error is thrown.

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a long, or defaultValue.

      • getLongBytes

         long getLongBytes(long defaultValue)

        Get the value of property as a long or human readable format. If no such property exists, the provided default value is returned, or if the specified value is not a valid long or human readable format, then an error is thrown. You can use the following suffix (case insensitive): k(kilo), m(mega), g(giga), t(tera), p(peta), e(exa)

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a long, or defaultValue.

      • setLong

         void setLong(long value)

        Set the value of property to a long.

        Parameters:
        value - long value of the property.
      • getFloat

         float getFloat(float defaultValue)

        Get the value of property as a float. If no such property exists, the provided default value is returned, or if the specified value is not a valid float, then an error is thrown.

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a float, or defaultValue.

      • setFloat

         void setFloat(float value)

        Set the value of property to a float.

        Parameters:
        value - property value.
      • getDouble

         double getDouble(double defaultValue)

        Get the value of property as a double. If no such property exists, the provided default value is returned, or if the specified value is not a valid double, then an error is thrown.

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a double, or defaultValue.

      • setDouble

         void setDouble(double value)

        Set the value of property to a double.

        Parameters:
        value - property value.
      • getBoolean

         boolean getBoolean(boolean defaultValue)

        Get the value of property as a boolean. If no such property is specified, or if the specified value is not a valid boolean, then defaultValue is returned.

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a boolean, or defaultValue.

      • setBoolean

         void setBoolean(boolean value)

        Set the value of property to a boolean.

        Parameters:
        value - boolean value of the property.
      • setBooleanIfUnset

         void setBooleanIfUnset(boolean value)

        Set the given property, if it is currently unset.

        Parameters:
        value - new value
      • setEnum

         <T extends Enum<T>> void setEnum(T value)

        Set the value of property to the given type. This is equivalent to set(<name>, value.toString()).

        Parameters:
        value - new value
      • getEnum

         <T extends Enum<T>> T getEnum(T defaultValue)

        Return value matching this enumerated type.

        Parameters:
        defaultValue - Value returned if no mapping exists
        Returns:

        a T object.

      • setTimeDuration

         void setTimeDuration(long value, TimeUnit unit)

        Set the value to the given time duration

        Parameters:
        value - Time duration
        unit - Unit of time
      • getTimeDuration

         long getTimeDuration(long defaultValue, TimeUnit unit)

        Return time duration in the given time unit. Valid units are encoded in properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds (ms), seconds (s), minutes (m), hours (h), and days (d).

        Parameters:
        defaultValue - Value returned if no mapping exists.
        unit - Unit to convert the stored property, if it exists.
        Returns:

        The time duration

      • getPattern

         Pattern getPattern(Pattern defaultValue)

        Get the value of property as a Pattern. If no such property is specified, or if the specified value is not a valid Pattern, then DefaultValue is returned.

        Parameters:
        defaultValue - default value
        Returns:

        property value as a compiled Pattern, or defaultValue

      • setPattern

         void setPattern(Pattern pattern)

        Set the given property to Pattern. If the pattern is passed as null, sets the empty pattern which results in further calls to getPattern(...) returning the default value.

        Parameters:
        pattern - new value
      • getRange

         SParser.IntegerRanges getRange(String defaultValue)

        Parse the given attribute as a set of integer ranges

        Parameters:
        defaultValue - the default value if it is not set
        Returns:

        a new set of ranges from the configured value

      • getStringCollection

         Collection<String> getStringCollection()

        Get the comma delimited values of property as a collection of Strings. If no such property is specified then empty collection is returned.

        Returns:

        property value as a collection of Strings.

      • getPair

         Pair<String, String> getPair(Pair<String, String> defaultValue)

        getPair.

        Parameters:
        defaultValue - a org.apache.commons.lang3.tuple.Pair object.
        Returns:

        a org.apache.commons.lang3.tuple.Pair object.

      • getStrings

         Array<String> getStrings()

        Get the comma delimited values of property as an array of Strings. If no such property is specified then null is returned.

        Returns:

        property value as an array of Strings, or null.

      • setStrings

         void setStrings(Array<String> values)

        Set the array of string values for property as as comma delimited values.

        Parameters:
        values - The values
      • getStrings

         Array<String> getStrings(Array<String> defaultValue)

        Get the comma delimited values of property as an array of Strings. If no such property is specified then default value is returned.

        Parameters:
        defaultValue - The default value
        Returns:

        property value as an array of Strings, or default value.

      • getTrimmedStringCollection

         Collection<String> getTrimmedStringCollection()

        Get the comma delimited values of property as a collection of Strings, trimmed of the leading and trailing whitespace. If no such property is specified then empty Collection is returned.

        Returns:

        property value as a collection of Strings, or empty Collection

      • getTrimmedStrings

         Array<String> getTrimmedStrings()

        Get the comma delimited values of property as an array of Strings, trimmed of the leading and trailing whitespace. If no such property is specified then an empty array is returned.

        Returns:

        property value as an array of trimmed Strings, or empty array.

      • getTrimmedStrings

         Array<String> getTrimmedStrings(Array<String> defaultValue)

        Get the comma delimited values of property as an array of Strings, trimmed of the leading and trailing whitespace. If no such property is specified then default value is returned.

        Parameters:
        defaultValue - The default value
        Returns:

        property value as an array of trimmed Strings, or default value.

      • getUint

         Integer getUint(int defaultValue)

        Get a unsigned integer, if the configured value is negative or not set, return the default value

        Parameters:
        defaultValue - The default value return if the configured value is negative
        Returns:

        a positive integer

      • getUlong

         Long getUlong(long defaultValue)

        Get a unsigned long integer, if the configured value is negative, return the default value

        Parameters:
        defaultValue - The default value return if the configured value is negative
        Returns:

        a positive long integer

      • getDuration

         Duration getDuration(Duration defaultValue)

        Support both ISO-8601 standard and hadoop time duration format ISO-8601 standard : PnDTnHnMn.nS Hadoop time duration format : Valid units are : ns, us, ms, s, m, h, d.

        Parameters:
        defaultValue - a java.time.Duration object.
        Returns:

        a java.time.Duration object.

      • getDuration

         Duration getDuration()

        Retrieves the duration.

        This method obtains the duration by invoking another overloaded getDuration method. It uses a constant named INVALID_DURATION as a default value, indicating that if a valid duration cannot be obtained, an invalid Duration object will be returned.

        Support both ISO-8601 standard and hadoop time duration format

        * ISO-8601 standard : PnDTnHnMn.nS * Hadoop time duration format : Valid units are : ns, us, ms, s, m, h, d.

        Note: for hadoop time duration format, the unit is always lowercase, and only single unit is allowed.

        Returns:

        A Duration object representing the duration of a process. If a valid duration cannot be obtained, it returns an object representing an invalid duration.

      • getInstant

         Instant getInstant()

        Try to detect a date time from the text and convert it to be a instant If no date time detected, return Instant.EPOCH

      • getInstant

         Instant getInstant(Instant defaultValue)

        Try to detect a date time from the text and convert it to be a instant If no date time detected, return defaultValue Accept the following format: 1. yyyy-MM-dd[ HH[:mm[:ss]]] 2. ISO_INSTANT, or yyyy-MM-ddTHH:mm:ssZ

      • getClassByNameOrNull

         Class<out Object> getClassByNameOrNull(String name)

        Load a class by name, returning null rather than throwing an exception if it couldn't be loaded. This is to avoid the overhead of creating an exception.

        Parameters:
        name - the class name
        Returns:

        the class object, or null if it could not be found.

      • getClasses

         Array<Class<out Object>> getClasses(Array<Class<out Object>> defaultValue)

        Get the value of property as an array of Class. The value of the property specifies a list of comma separated class names. If no such property is specified, then defaultValue is returned.

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a Class[], or defaultValue.

      • getClass

         Class<out Object> getClass(Class<out Object> defaultValue)

        Get the value as a Class. If no such property is specified, then defaultValue is returned.

        Parameters:
        defaultValue - default value.
        Returns:

        property value as a Class, or defaultValue.

      • getClass

         <U> Class<out U> getClass(Class<out U> defaultValue, Class<U> xface)

        Set the value to theClass implementing the given interface xface.

        If no such property is specified, then defaultValue is returned.

        An exception is thrown if the returned class does not implement the named interface.

        Parameters:
        defaultValue - default value.
        xface - the interface implemented by the named class.
        Returns:

        property value as a Class, or defaultValue.

      • setClass

         void setClass(Class<out Object> theClass, Class<out Object> xface)

        Set the value to theClass implementing the given interface xface.

        An exception is thrown if theClass does not implement the interface xface.

        Parameters:
        theClass - property value.
        xface - the interface implemented by the named class.
      • getFile

         File getFile(String dirsProp, String path)

        Get a local file name under a directory named in dirsProp with the given path. If dirsProp contains multiple directories, then one is chosen based on path's hash code. If the selected directory does not exist, an attempt is made to create it.

        Parameters:
        dirsProp - directory in which to locate the file.
        path - file-path.
        Returns:

        local file under the directory with the given path.