Object
CodeList<E>
- Type Parameters:
E
- the type of this code list.
- All Implemented Interfaces:
Serializable
,Comparable<E>
,ControlledVocabulary
- Direct Known Subclasses:
AssociationType
,AxisDirection
,BandDefinition
,CellGeometry
,CharacterSet
,Classification
,ComparisonOperatorName
,Configuration.Key
,Context
,CoordinateDataType
,CouplingType
,CoverageContentType
,Datatype
,DateType
,DimensionNameType
,DistanceOperatorName
,DistributedComputingPlatform
,EvaluationMethodType
,GeometricObjectType
,GeometryType
,ImagingCondition
,IndeterminateValue
,InitiativeType
,KeywordType
,LogicalOperatorName
,MaintenanceFrequency
,MediumFormat
,MediumName
,ObjectiveType
,Obligation
,OnLineFunction
,OperationType
,PixelInCell
,PixelOrientation
,PolarizationOrientation
,PresentationForm
,Priority
,Progress
,RangeMeaning
,RealizationMethod
,ReferenceSystemType
,Restriction
,Role
,ScopeCode
,Sequence
,SpatialOperatorName
,SpatialRepresentationType
,TelephoneType
,TemporalOperatorName
,TopicCategory
,TopologyLevel
,TransferFunctionType
,Trigger
,ValueStructure
,VerticalDatumType
@UML(identifier="CodeList",
specification=ISO_19103)
public abstract class CodeList<E extends CodeList<E>>
extends Object
implements ControlledVocabulary, Comparable<E>, Serializable
Base class for all code lists. Code lists are like enumerations, but extensible.
For example, invoking the
Above snippet is sufficient for classes in exported packages. If a code list is defined in a non-exported package,
then its static fields may need to be initialized with
valueOf(String)
method in any subclass with a
name that was not previously defined will create a new CodeList
element
and automatically add it to the arrays returned by values()
.
Guidance for subclasses
The parameter type<E>
shall be the same type as the code list subclass.
In other words, if the subclass is Foo
, then it must extend CodeList<Foo>
.
In addition, subclasses should provide values(String)
and values()
methods.
The following code snippet is a suggested pattern for subclasses:
public final class Foo extends CodeList<Foo> {
public static final Foo BAR = new Foo("BAR");
public static final Foo BIZ = new Foo("BIZ");
private Foo(String name) {
super(name);
}
public static Foo valueOf(String name) {
return valueOf(Foo.class, name, Foo::new).get();
}
public static Foo[] values() {
return values(Foo.class);
}
@Override
public Foo[] family() {
return values();
}
}
valueOf(String)
instead of new Foo(String)
.
It will have a small performance cost, but is needed because of Java Module System restrictions on reflection.- Since:
- 1.0
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Deprecated, for removal: This API element is subject to removal in a future version. -
Constructor Summary
ModifierConstructorDescriptionprotected
Creates a new code list element.protected
CodeList
(String name, Collection<E> values) Deprecated, for removal: This API element is subject to removal in a future version.This constructor is unsafe because a reference tothis
escapes before subclass is fully initialized (see issue #91). -
Method Summary
Modifier and TypeMethodDescriptionfinal int
Compares this code with the specified object for order.abstract E[]
family()
Returns the list of codes of the same kind as this code.Returns the identifier declared in the UML annotation.final String
name()
Returns the programmatic name of this code list element.final int
ordinal()
Returns the ordinal of this code element.protected Object
Resolves the code list to an unique instance after deserialization.toString()
Returns a string representation of this code list.Returns the code of the given type and name if it exists, or optionally creates a new code.static <T extends CodeList<T>>
TDeprecated, for removal: This API element is subject to removal in a future version.This method depends on reflection, which is restricted in the context of Java Module System.static <T extends CodeList<T>>
TvalueOf
(Class<T> codeType, CodeList.Filter filter) Deprecated, for removal: This API element is subject to removal in a future version.This method depends on reflection, which is restricted in the context of Java Module System.static <E extends CodeList<E>>
E[]Returns the values for the specified type of code list.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.opengis.util.ControlledVocabulary
names
-
Constructor Details
-
CodeList
Deprecated, for removal: This API element is subject to removal in a future version.This constructor is unsafe because a reference tothis
escapes before subclass is fully initialized (see issue #91). UseCodeList(String)
instead.Creates a new code list element and add it to the given collection. Subclasses will typically give a static reference to anArrayList
for thevalues
argument. This list is used forvalues()
method implementations.- Parameters:
name
- the code name. Shall be the name of the static field if such field exist.values
- the collection to add the element to. Shall be the same for all elements.
-
CodeList
Creates a new code list element. If this constructor is invoked for initializing a static field, then the given name shall be the case-sensitive name of the static field.- Parameters:
name
- the code name. Shall be the name of the static field if such field exist.- Since:
- 3.1
-
-
Method Details
-
valueOf
@Deprecated(since="3.1", forRemoval=true) public static <T extends CodeList<T>> T valueOf(Class<T> codeType, String name) Deprecated, for removal: This API element is subject to removal in a future version.This method depends on reflection, which is restricted in the context of Java Module System. UsevalueOf(Class, String, Function)
instead.Returns the code of the given type that matches the given name, or creates a new code if there is no match. More specifically, this methods returns the first instance of the given class for whichname().equals(name)
istrue
. If no such instance is found, then a new instance is created using the constructor expecting a singleString
argument.Note that invoking this method may result in the creation of a new code value. If this is not desired, use
valueOf(Class, String, Function)
instead.- Type Parameters:
T
- the compile-time type given as thecodeType
parameter.- Parameters:
codeType
- the type of code list.name
- the name of the code to obtain (case sensitive), ornull
.- Returns:
- a code matching the given name (possible a new code), or
null
if the given name is null.
-
valueOf
@Deprecated(since="3.1", forRemoval=true) public static <T extends CodeList<T>> T valueOf(Class<T> codeType, CodeList.Filter filter) Deprecated, for removal: This API element is subject to removal in a future version.This method depends on reflection, which is restricted in the context of Java Module System. UsevalueOf(Class, String, Function)
instead.Returns the code of the given type that matches the given criterion, or returns a new one if none match it. More specifically, this methods returns the first element (in declaration order) of the given class wherefilter.accept(code)
returnstrue
. If no such element is found, then there is a choice:- If
CodeList.Filter.codename()
returnsnull
, then this method returnsnull
. - Otherwise a new instance is created using the constructor expecting a single
String
argument, which is given the value returned bycodename()
.
- Type Parameters:
T
- the compile-time type given as thecodeType
parameter.- Parameters:
codeType
- the type of code list.filter
- the criterion for the code to obtain.- Returns:
- a code matching the given criterion, or
null
if there is no match andCodeList.Filter.codename()
returnsnull
.
- If
-
valueOf
public static <E extends CodeList<E>> Optional<E> valueOf(Class<E> codeType, String name, Function<? super String, ? extends E> constructor) Returns the code of the given type and name if it exists, or optionally creates a new code. This method returns the first instance (in declaration order) of the given class for which the name is equals, ignoring case, to the given name. If no such instance is found, then there is a choice:- If
constructor
isnull
, then this method returns an empty value. - Otherwise a nullable new instance is created
by a call to
constructor.apply(name)
.
valueOf(String)
in subclasses. See the "Guidance for subclasses" section in the class Javadoc.Name matching criterion
As of GeoAPI 3.1, names are compared usingString.equalsIgnoreCase(String)
. This is different than GeoAPI 3.0, which compared names usingString.equals(Object)
. Being case-insensitive allows to recognize some UML identifiers as equivalent to the names of constants declared inCodeList
subclasses.- Type Parameters:
E
- the compile-time type given as thecodeType
parameter.- Parameters:
codeType
- the type of the code list for which to get an element.name
- the name of the code to obtain (case-insensitive), ornull
.constructor
- the constructor to use if a new code needs to be created, ornull
for not creating any new code.- Returns:
- a code matching the given name, or empty if the given name is null or blank,
or if no existing code matches the given name and
constructor
is null or returned a null value. - Since:
- 3.1
- If
-
values
Returns the values for the specified type of code list.- Type Parameters:
E
- the compile-time type given as thecodeType
parameter.- Parameters:
codeType
- the type of code list for which to get the current code list values.- Returns:
- all current values for the given type (never
null
). - Since:
- 3.1
-
family
Returns the list of codes of the same kind as this code. Invoking this method gives identical results than invoking the staticvalues()
methods provided inCodeList
subclasses, except thatfamily()
does not require the class to be known at compile-time — provided that at least one instance of the family is available.- Specified by:
family
in interfaceControlledVocabulary
- Returns:
- the codes of the same kind as this code.
-
name
Returns the programmatic name of this code list element. If this element is a constant defined in aCodeList
subclass, then this is the name of the public static field for that constant.- Specified by:
name
in interfaceControlledVocabulary
- Returns:
- the name of this code constant.
-
identifier
Returns the identifier declared in the UML annotation. The UML identifier shall be the ISO or OGC name for this code constant.- Specified by:
identifier
in interfaceControlledVocabulary
- Returns:
- the ISO/OGC identifier for this code.
- See Also:
-
ordinal
Returns the ordinal of this code element. This is the index of this element in the array returned byfamily()
. Those elements are in the order of the constants declared in subclasses, where the first element is assigned an ordinal value of zero.Stability
For a given GeoAPI version, the code list elements declared as constants in subclasses always have the same ordinal values. However, the ordinal value of the same constant may differ between different GeoAPI versions if new elements were inserted or deleted between existing elements. User-defined elements (created by calls to avalueOf(…)
method) are not guaranteed to have the same ordinal value between different execution of the same application, because these values depend on the order in which code list elements are created.- Specified by:
ordinal
in interfaceControlledVocabulary
- Returns:
- the position of this code in elements declaration.
-
compareTo
Compares this code with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.Code list constants are only comparable to other code list constants of the same type. The natural order implemented by this method is the order in which the constants are declared.
- Specified by:
compareTo
in interfaceComparable<E extends CodeList<E>>
- Parameters:
other
- the code constant to compare with this code.- Returns:
- a negative value if the given code is less than this code, a positive value if greater or 0 if equal.
-
toString
-
readResolve
Resolves the code list to an unique instance after deserialization. The instance shall be resolved using its name, not its ordinal, because the latter value is not guaranteed to be stable between different GeoAPI versions or, in the case of user-defined code list elements, between different JVM executions.The default implementation tries to replace the deserialized instance by the result of
. IfvalueOf
(getClass(), name(), null)valueOf(…)
returned an empty value, thenthis
is updated with a new ordinal value and added to the list of codes associated to its class.- Returns:
- either
this
or an existing code list for the same name (ignoring case). - Throws:
ObjectStreamException
- if the deserialization failed.
-
Predicate
from the standard Java library.