Class StringValue

  • All Implemented Interfaces:
    java.io.Serializable, IClusterable
    Direct Known Subclasses:
    EnumeratedType

    public class StringValue
    extends java.lang.Object
    implements IClusterable
    Holds an immutable String value and optionally a Locale, with methods to convert to various types. Also provides some handy parsing methods and a variety of static factory methods.

    Objects can be constructed directly from Strings or by using the valueOf() static factory methods. The repeat() static factory methods provide a way of generating a String value that repeats a given char or String a number of times.

    Conversions to a wide variety of types can be found in the to*() methods. A generic conversion can be achieved with to(Class).

    The beforeFirst(), afterFirst(), beforeLast() and afterLast() methods are handy for parsing things like paths and filenames.

    Author:
    Jonathan Locke
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected StringValue​(java.lang.String text)
      Private constructor to force use of static factory methods.
      protected StringValue​(java.lang.String text, java.util.Locale locale)
      Private constructor to force use of static factory methods.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String afterFirst​(char c)
      Gets the substring after the first occurrence given char.
      java.lang.String afterLast​(char c)
      Gets the substring after the last occurrence given char.
      java.lang.String beforeFirst​(char c)
      Gets the substring before the first occurrence given char.
      java.lang.String beforeLast​(char c)
      Gets the substring before the last occurrence given char.
      boolean equals​(java.lang.Object obj)
      int hashCode()
      boolean isEmpty()
      Returns whether the text is null or empty
      boolean isNull()
      Returns whether the text is null.
      static StringValue repeat​(int times, char c)  
      static StringValue repeat​(int times, java.lang.String s)  
      java.lang.CharSequence replaceAll​(java.lang.CharSequence searchFor, java.lang.CharSequence replaceWith)
      Replaces on this text.
      <T> T to​(java.lang.Class<T> type)
      Converts this StringValue to a given type.
      boolean toBoolean()
      Convert this text to a boolean.
      boolean toBoolean​(boolean defaultValue)
      Convert to boolean, returning default value if text is inconvertible.
      java.lang.Boolean toBooleanObject()
      Convert this text to a boolean.
      char toChar()
      Convert this text to a char.
      char toChar​(char defaultValue)
      Convert to character, returning default value if text is inconvertible.
      java.lang.Character toCharacter()
      Convert this text to a Character.
      double toDouble()
      Convert this text to a double.
      double toDouble​(double defaultValue)
      Convert to double, returning default value if text is inconvertible.
      java.lang.Double toDoubleObject()
      Convert this text to a Double.
      java.time.Duration toDuration()
      Convert this text to a Duration instance.
      java.time.Duration toDuration​(java.time.Duration defaultValue)
      Convert to duration, returning default value if text is inconvertible.
      <T extends java.lang.Enum<T>>
      T
      toEnum​(java.lang.Class<T> eClass)
      Convert this text to an enum.
      <T extends java.lang.Enum<T>>
      T
      toEnum​(java.lang.Class<T> eClass, T defaultValue)
      Convert this text to an enum.
      <T extends java.lang.Enum<T>>
      T
      toEnum​(T defaultValue)
      Convert this text to an enum.
      java.time.Instant toInstant()
      Convert this text to an Instant instance.
      java.time.Instant toInstant​(java.time.Instant defaultValue)
      Convert to Instant, returning default value if text is inconvertible.
      int toInt()
      Convert this text to an int.
      int toInt​(int defaultValue)
      Convert to integer, returning default value if text is inconvertible.
      java.lang.Integer toInteger()
      Convert this text to an Integer.
      long toLong()
      Convert this text to a long.
      long toLong​(long defaultValue)
      Convert to long integer, returning default value if text is inconvertible.
      java.lang.Long toLongObject()
      Convert this text to a Long.
      <T> T toOptional​(java.lang.Class<T> type)
      Converts this StringValue to a given type or null if the value is empty.
      java.lang.Boolean toOptionalBoolean()
      Convert to object types, returning null if text is null or empty.
      java.lang.Character toOptionalCharacter()
      Convert to object types, returning null if text is null or empty.
      java.lang.Double toOptionalDouble()
      Convert to object types, returning null if text is null or empty.
      java.time.Duration toOptionalDuration()
      Convert to object types, returning null if text is null or empty.
      <T extends java.lang.Enum<T>>
      T
      toOptionalEnum​(java.lang.Class<T> eClass)
      Convert to enum, returning null if text is null or empty.
      java.time.Instant toOptionalInstant()
      Convert to object types, returning null if text is null or empty.
      java.lang.Integer toOptionalInteger()
      Convert to object types, returning null if text is null or empty.
      java.lang.Long toOptionalLong()
      Convert to object types, returning null if text is null or empty.
      java.lang.String toOptionalString()
      Convert to object types, returning null if text is null.
      java.lang.String toString()  
      java.lang.String toString​(java.lang.String defaultValue)
      Convert to primitive types, returning default value if text is null.
      static StringValue valueOf​(double value)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(double value, int places, java.util.Locale locale)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(double value, java.util.Locale locale)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(java.lang.Object object)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(java.lang.Object object, java.util.Locale locale)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(java.lang.String string)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(java.lang.String string, java.util.Locale locale)
      Converts the given input to an instance of StringValue.
      static StringValue valueOf​(AppendingStringBuffer buffer)
      Converts the given input to an instance of StringValue.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • StringValue

        protected StringValue​(java.lang.String text)
        Private constructor to force use of static factory methods.
        Parameters:
        text - The text for this string value
      • StringValue

        protected StringValue​(java.lang.String text,
                              java.util.Locale locale)
        Private constructor to force use of static factory methods.
        Parameters:
        text - The text for this string value
        locale - the locale for formatting and parsing
    • Method Detail

      • repeat

        public static StringValue repeat​(int times,
                                         char c)
        Parameters:
        times - Number of times to repeat character
        c - Character to repeat
        Returns:
        Repeated character string
      • repeat

        public static StringValue repeat​(int times,
                                         java.lang.String s)
        Parameters:
        times - Number of times to repeat string
        s - String to repeat
        Returns:
        Repeated character string
      • valueOf

        public static StringValue valueOf​(double value)
        Converts the given input to an instance of StringValue.
        Parameters:
        value - Double precision value
        Returns:
        String value formatted with one place after decimal
      • valueOf

        public static StringValue valueOf​(double value,
                                          int places,
                                          java.util.Locale locale)
        Converts the given input to an instance of StringValue.
        Parameters:
        value - Double precision value
        places - Number of places after decimal
        locale - Locale to be used for formatting
        Returns:
        String value formatted with the given number of places after decimal
      • valueOf

        public static StringValue valueOf​(double value,
                                          java.util.Locale locale)
        Converts the given input to an instance of StringValue.
        Parameters:
        value - Double precision value
        locale - Locale to be used for formatting
        Returns:
        String value formatted with one place after decimal
      • valueOf

        public static StringValue valueOf​(java.lang.Object object)
        Converts the given input to an instance of StringValue.
        Parameters:
        object - An object
        Returns:
        String value for object
      • valueOf

        public static StringValue valueOf​(java.lang.Object object,
                                          java.util.Locale locale)
        Converts the given input to an instance of StringValue.
        Parameters:
        object - An object
        locale - Locale to be used for formatting
        Returns:
        String value for object
      • valueOf

        public static StringValue valueOf​(java.lang.String string)
        Converts the given input to an instance of StringValue.
        Parameters:
        string - A string
        Returns:
        String value for string
      • valueOf

        public static StringValue valueOf​(java.lang.String string,
                                          java.util.Locale locale)
        Converts the given input to an instance of StringValue.
        Parameters:
        string - A string
        locale - Locale to be used for formatting
        Returns:
        String value for string
      • valueOf

        public static StringValue valueOf​(AppendingStringBuffer buffer)
        Converts the given input to an instance of StringValue.
        Parameters:
        buffer - A string buffer
        Returns:
        String value
      • afterFirst

        public final java.lang.String afterFirst​(char c)
        Gets the substring after the first occurrence given char.
        Parameters:
        c - char to scan for
        Returns:
        the substring
      • afterLast

        public final java.lang.String afterLast​(char c)
        Gets the substring after the last occurrence given char.
        Parameters:
        c - char to scan for
        Returns:
        the substring
      • beforeFirst

        public final java.lang.String beforeFirst​(char c)
        Gets the substring before the first occurrence given char.
        Parameters:
        c - char to scan for
        Returns:
        the substring
      • beforeLast

        public final java.lang.String beforeLast​(char c)
        Gets the substring before the last occurrence given char.
        Parameters:
        c - char to scan for
        Returns:
        the substring
      • replaceAll

        public final java.lang.CharSequence replaceAll​(java.lang.CharSequence searchFor,
                                                       java.lang.CharSequence replaceWith)
        Replaces on this text.
        Parameters:
        searchFor - What to search for
        replaceWith - What to replace with
        Returns:
        This string value with searchFor replaces with replaceWith
      • toBoolean

        public final boolean toBoolean​(boolean defaultValue)
        Convert to boolean, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as a boolean or the default value if text is empty or inconvertible
        See Also:
        Strings.isTrue(String)
      • toChar

        public final char toChar​(char defaultValue)
        Convert to character, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as a primitive char or the default value if text is not a single character
      • toDouble

        public final double toDouble​(double defaultValue)
        Convert to double, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as a double or the default value if text is empty or inconvertible
      • toDuration

        public final java.time.Duration toDuration​(java.time.Duration defaultValue)
        Convert to duration, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as a duration or the default value if text is empty or inconvertible
        See Also:
        Duration#valueOf(String, java.util.Locale)
      • toInt

        public final int toInt​(int defaultValue)
        Convert to integer, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as an integer or the default value if text is not an integer
      • toLong

        public final long toLong​(long defaultValue)
        Convert to long integer, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as a long integer or the default value if text is empty or inconvertible
      • toOptionalString

        public final java.lang.String toOptionalString()
        Convert to object types, returning null if text is null.
        Returns:
        converted
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        The string value
      • toString

        public final java.lang.String toString​(java.lang.String defaultValue)
        Convert to primitive types, returning default value if text is null.
        Parameters:
        defaultValue - the default value to return of text is null
        Returns:
        the converted text as a primitive or the default if text is null
      • toInstant

        public final java.time.Instant toInstant​(java.time.Instant defaultValue)
        Convert to Instant, returning default value if text is inconvertible.
        Parameters:
        defaultValue - the default value
        Returns:
        the converted text as a Instant or the default value if text is inconvertible.
      • toEnum

        public final <T extends java.lang.Enum<T>> T toEnum​(T defaultValue)
        Convert this text to an enum.
        Parameters:
        defaultValue - This will be returned if there is an error converting the value
        Returns:
        The value as an enum
      • toEnum

        public final <T extends java.lang.Enum<T>> T toEnum​(java.lang.Class<T> eClass,
                                                            T defaultValue)
        Convert this text to an enum.
        Parameters:
        eClass - enum type
        defaultValue - This will be returned if there is an error converting the value
        Returns:
        The value as an enum
      • isNull

        public boolean isNull()
        Returns whether the text is null.
        Returns:
        true if the text is null, false otherwise.
      • isEmpty

        public boolean isEmpty()
        Returns whether the text is null or empty
        Returns:
        true if the text is null or .trim().length()==0, false otherwise.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object