Class Enumeration
- All Implemented Interfaces:
Type
Riskscape type that maps a set of numbers to a set of well-known strings. A bit like WithinSet
except
that it will coerce any Number
in to a String
within the set, based on the String
's position
within the list
``` enumeration = Enumeration.oneBased("cat", "dog", "Bear"); enumeration.coerce(1) // gives "cat" enumeration.coerce(4) // raises a coercion exception - outside of range enumeration.coerce("dog") // gives "dog" enumeration.coerce("fox") // raises a coercion exception - fox not in set enumeration.coerce("bear") // gives "BEAR" - comparisons are always lower cased to avoid case consistency issues
```
-
Field Summary
Modifier and TypeFieldDescriptionstatic final ComplexTypeConstructor
static final TypeInformation
-
Method Summary
Modifier and TypeMethodDescriptionasStruct()
Attempt to convert the given value in to one represented by this type.boolean
int
estimateSize
(Object entry) Estimate the number of bytes used to serialize the given value to an output stream.String[]
int
hashCode()
Class<?>
The underlying java class used to represent this type.boolean
static Enumeration
Construct a Riskscape Enumeration from the given array of strings.void
toBytes
(DataOutputStream os, Object toWrite) toString()
<T,
U> U visit
(TypeVisitor<T, U> tv, T data) Type specific visiting logic.static Enumeration
Construct a Riskscape Enumeration from the given array of strings.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface nz.org.riskscape.engine.types.Type
checkForNull, find, findAllowNull, getUnwrappedType, isA, isNullable, isNumeric, isWrapped, toString, unwrap
-
Field Details
-
TYPE_CONSTRUCTOR
-
TYPE_INFORMATION
-
-
Method Details
-
zeroBased
Construct a Riskscape Enumeration from the given array of strings. The position they are at in the array is that string's ordinal value, starting from zero. Null values are supported, and are considered gaps in the enumeration. Attempts to coerce numbers that don't map to a non null value will raise a
CoercionException
.- Throws:
IllegalArgumentException
- if there are duplicate (case insensitive) values.
-
oneBased
Construct a Riskscape Enumeration from the given array of strings. The position they are at in the array is that string's ordinal value, starting from 1. Null values are supported, and are considered gaps in the enumeration. Attempts to coerce numbers that don't map to a non null value will raise a
CoercionException
.- Throws:
IllegalArgumentException
- if there are duplicate (case insensitive) values.
-
coerce
Description copied from interface:Type
Attempt to convert the given value in to one represented by this type.
- Specified by:
coerce
in interfaceType
- Parameters:
value
- The value to coerce- Returns:
- A value that conforms to this type
- Throws:
CoercionException
- if the given object can not be converted to something represented by this type
-
internalType
Description copied from interface:Type
The underlying java class used to represent this type.
- Specified by:
internalType
in interfaceType
-
toString
-
estimateSize
Description copied from interface:Type
Estimate the number of bytes used to serialize the given value to an output stream.
- Specified by:
estimateSize
in interfaceType
- Parameters:
entry
- the value of this type to measure. Must be ofType.internalType()
or aClassCastException
is going to happen- Returns:
- the estimated number of bytes, or zero if no estimation was possible.
-
toBytes
- Specified by:
toBytes
in interfaceType
- Throws:
IOException
-
fromBytes
- Specified by:
fromBytes
in interfaceType
- Throws:
IOException
-
equals
-
asStruct
-
hashCode
public int hashCode() -
visit
Description copied from interface:Type
Type specific visiting logic. Types must decide whether they are atomic or compound and call the relevant method on
TypeVisitor
. It's up to each compound type to decide how they want to structure their children, e.g. order, metadata, and what's considered a child or not. -
isOneBased
public boolean isOneBased() -
getValues
-