Class TypedString<T extends TypedString<T>>

  • Type Parameters:
    T - the implementation subclass of this class
    All Implemented Interfaces:
    Named, java.io.Serializable, java.lang.Comparable<T>
    Direct Known Subclasses:
    AttributeType, ColumnName, CurveGroupName, CurveInfoType, CurveSensitivitiesType, EtdContractCode, ExplainKey, FieldName, LegalEntityGroup, ObservableSource, ProductType, RepoGroup, StrikeType, SurfaceInfoType, ValueType

    public abstract class TypedString<T extends TypedString<T>>
    extends java.lang.Object
    implements Named, java.lang.Comparable<T>, java.io.Serializable
    An abstract class designed to enable typed strings.

    The purpose of TypedString is to provide a Java type to a concept that might otherwise be represented as a string. It could be thought of as a way to provide a type alias for a string.

    The string wrapped by this type must not be empty.

    Subclasses must be written as follows:

      public final class FooType
          extends TypedString<FooType> {
        private static final long serialVersionUID = 1L;
        @FromString
        public static FooType of(String name) {
          return new FooType(name);
        }
        private FooType(String name) {
          super(name);
        }
      }
     

    The net result is that an API can be written with methods taking FooType as a method parameter instead of String.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected TypedString​(java.lang.String name)
      Creates an instance.
      protected TypedString​(java.lang.String name, com.google.common.base.CharMatcher matcher, java.lang.String msg)
      Creates an instance, validating the name against a matcher.
      protected TypedString​(java.lang.String name, java.util.regex.Pattern pattern, java.lang.String msg)
      Creates an instance, validating the name against a regex.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(T other)
      Compares this type to another.
      boolean equals​(java.lang.Object obj)
      Checks if this type equals another.
      java.lang.String getName()
      Gets the name.
      int hashCode()
      Returns a suitable hash code.
      java.lang.String toString()
      Returns the name.
      • Methods inherited from class java.lang.Object

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

      • TypedString

        protected TypedString​(java.lang.String name)
        Creates an instance.
        Parameters:
        name - the name, not empty
      • TypedString

        protected TypedString​(java.lang.String name,
                              java.util.regex.Pattern pattern,
                              java.lang.String msg)
        Creates an instance, validating the name against a regex.

        In most cases, a CharMatcher will be faster than a regex Pattern, typically by over an order of magnitude.

        Parameters:
        name - the name, not empty
        pattern - the regex pattern for validating the name
        msg - the message to use to explain validation failure
      • TypedString

        protected TypedString​(java.lang.String name,
                              com.google.common.base.CharMatcher matcher,
                              java.lang.String msg)
        Creates an instance, validating the name against a matcher.

        In most cases, a CharMatcher will be faster than a regex Pattern, typically by over an order of magnitude.

        Parameters:
        name - the name, not empty
        matcher - the matcher for validating the name
        msg - the message to use to explain validation failure
    • Method Detail

      • getName

        public java.lang.String getName()
        Gets the name.
        Specified by:
        getName in interface Named
        Returns:
        the name
      • compareTo

        public final int compareTo​(T other)
        Compares this type to another.

        Instances are compared in alphabetical order based on the name.

        Specified by:
        compareTo in interface java.lang.Comparable<T extends TypedString<T>>
        Parameters:
        other - the object to compare to
        Returns:
        the comparison
      • equals

        public final boolean equals​(java.lang.Object obj)
        Checks if this type equals another.

        Instances are compared based on the name.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare to, null returns false
        Returns:
        true if equal
      • hashCode

        public final int hashCode()
        Returns a suitable hash code.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a suitable hash code
      • toString

        public final java.lang.String toString()
        Returns the name.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string form, not empty