Package com.opengamma.strata.collect
Class TypedString<T extends TypedString<T>>
- java.lang.Object
-
- com.opengamma.strata.collect.TypedString<T>
-
- Type Parameters:
T- the implementation subclass of this class
- All Implemented Interfaces:
Named,Serializable,Comparable<T>
- Direct Known Subclasses:
ColumnName,CurveGroupName,CurveInfoType,CurveSensitivitiesType,EtdContractCode,EtdContractGroupCode,ExplainKey,FieldName,LegalEntityGroup,ObservableSource,ProductType,RepoGroup,StrikeType,SurfaceInfoType,ValueType
public abstract class TypedString<T extends TypedString<T>> extends Object implements Named, Comparable<T>, Serializable
An abstract class designed to enable typed strings.The purpose of
TypedStringis 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
FooTypeas a method parameter instead ofString.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedTypedString(String name)Creates an instance.protectedTypedString(String name, CharMatcher matcher, String msg)Creates an instance, validating the name against a matcher.protectedTypedString(String name, Pattern pattern, String msg)Creates an instance, validating the name against a regex.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(T other)Compares this type to another.booleanequals(Object obj)Checks if this type equals another.StringgetName()Gets the name.inthashCode()Returns a suitable hash code.StringtoString()Returns the name.
-
-
-
Constructor Detail
-
TypedString
protected TypedString(String name)
Creates an instance.- Parameters:
name- the name, not empty
-
TypedString
protected TypedString(String name, Pattern pattern, String msg)
Creates an instance, validating the name against a regex.In most cases, a
CharMatcherwill be faster than a regexPattern, typically by over an order of magnitude.- Parameters:
name- the name, not emptypattern- the regex pattern for validating the namemsg- the message to use to explain validation failure
-
TypedString
protected TypedString(String name, CharMatcher matcher, String msg)
Creates an instance, validating the name against a matcher.In most cases, a
CharMatcherwill be faster than a regexPattern, typically by over an order of magnitude.- Parameters:
name- the name, not emptymatcher- the matcher for validating the namemsg- the message to use to explain validation failure
-
-
Method Detail
-
getName
public String getName()
Gets 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:
compareToin interfaceComparable<T extends TypedString<T>>- Parameters:
other- the object to compare to- Returns:
- the comparison
-
equals
public final boolean equals(Object obj)
Checks if this type equals another.Instances are compared based on the name.
-
hashCode
public final int hashCode()
Returns a suitable hash code.
-
-