T
- a type of the wrapped valuepublic class Variant<T> extends Object implements Serializable
ofOrigin()
or ofUnknownOrigin()
if the value is a record,wrap(Object, LspsContextHolder)
if the value is a general object and the origin is not known,Variant(Object, Type, Origin, LspsContextHolder)
if the value is a general object and the origin is knownequals(Object)
and hashCode()
delegates to valueOrNull()
.Type | Property and Description |
---|---|
boolean |
is
Gets the value of the property property.
|
Modifier and Type | Class and Description |
---|---|
static class |
Variant.ClosureResultOrigin |
static class |
Variant.ClosureVariant
Wraps a closure and forces you to specify the scope in which the closure is executed.
|
static class |
Variant.CollectionVariant<T extends Collection<V>,V>
A set variant, allows iterating over collections.
|
static class |
Variant.DereferenceOrigin |
static class |
Variant.ListVariant<V>
A list variant, allows access to list items.
|
static class |
Variant.MapVariant<K,V>
A map variant, allows access to map items.
|
static class |
Variant.MethodResultOrigin |
static class |
Variant.Origin
Tracks where variants came from.
|
static class |
Variant.PropertyOrigin |
static class |
Variant.RecordOrigin |
static class |
Variant.RecordVariant
Accesses
RecordHolder properties. |
static class |
Variant.ReferenceOrigin |
static class |
Variant.ScopedClosureVariant
Wraps a closure and an execution scope.
|
Modifier and Type | Field and Description |
---|---|
protected LspsContextHolder |
context
Context used to serialize the value.
|
Type |
definitionType
The defined type of the value, either the record property type, or the closure result type.
|
static Variant<?> |
NULL
A variant which wraps the 'null' value, with no origin.
|
protected Variant.Origin |
origin
The value origin, such as "ui::Geolocation.latitude".
|
Constructor and Description |
---|
Variant(T data,
Type definitionType,
Variant.Origin origin,
LspsContextHolder contextHolder)
Creates the variant.
|
Modifier and Type | Method and Description |
---|---|
Variant<Boolean> |
bool()
Casts the value to boolean.
|
<N> Variant<N> |
cast(Class<N> type)
Casts the value.
|
Variant<T> |
checkPresent()
Fails if the value is absent.
|
Variant.ClosureVariant |
closure()
Casts the value to a closure.
|
Variant.CollectionVariant<Collection<Object>,Object> |
collection()
Casts the value to a
Collection . |
<V> Variant.CollectionVariant<Collection<V>,V> |
collectionOf(Class<V> itemType)
Casts the value to a
Collection . |
Variant<Date> |
date()
Casts the value to Date.
|
Variant<Decimal> |
decimal()
Casts the value to a
Decimal . |
static Variant.RecordVariant |
definitionOf(UIComponent component)
Returns record variant which accesses given component definition.
|
static Variant.RecordVariant |
definitionOf(UIComponentData data)
Returns record variant which accesses given component definition.
|
Variant<Object> |
dereference(UIComponent scope)
De-references the referenced type (calls
ReferenceHolder.getValue() ). |
Variant<String> |
enumerationConstant()
Expects this variant to hold an
EnumerationImpl instance. |
<E extends Enum<E>> |
enumValueOf(Class<E> clazz)
Casts this variant to String and then calls
Enum.valueOf(Class, String) on the value (if the value is not null). |
<E extends Enum<E>> |
enumValueOfIgnoreCase(Class<E> clazz)
Casts this variant to String and then matches enumeration constant by name, ignoring case differences
(if the value is not null).
|
boolean |
equals(Object o) |
IllegalStateException |
fail(String cause)
Throws an
IllegalStateException with the following message: "[origin]: invalid value [value]: [cause]" . |
IllegalStateException |
fail(Throwable cause)
Throws an
IllegalStateException with the following message: "[origin]: invalid value [value]: [cause]" . |
T |
get()
Returns the contained instance, which must be present.
|
Handle |
getHandle()
Returns a serializable handle for the value of this variant.
|
Type |
getInstanceType()
Returns the actual type of the value (or instance).
|
Variant.Origin |
getOrigin()
Returns the variant origin, or null if the origin is not known.
|
boolean |
hasHandle()
Checks whether
getHandle() succeeds (the value is a non-null ValueHolder. |
int |
hashCode() |
boolean |
instanceOf(Class<?> valueClass)
|
Variant<Integer> |
integer()
Casts the variant's value to a
Integer . |
boolean |
isAbsent()
Returns true if the value is null.
|
boolean |
isClosure()
Returns true if this variant holds a non-null ClosureHolder.
|
boolean |
isPath()
Returns true if this variant holds a non-null
Path . |
boolean |
isPresent()
Returns true if this holder contains a (non-null) instance.
|
boolean |
isProperty()
Gets the value of the property property.
|
boolean |
isPropertyPath()
Deprecated.
This method was replaced by
isPath() to avoid confusion
when one could think this method checks the Variant type to
PropertyPath instead of
com.whitestein.lsps.lang.exec.Path . |
boolean |
isRecord()
Returns true if this variant holds a non-null RecordHolder.
|
Variant.ListVariant<Object> |
list()
Casts the value to a list of objects.
|
Variant.ListVariant<Object> |
listHolder()
Casts the value to a
ListHolder . |
<V> Variant.ListVariant<V> |
listOf(Class<V> itemType)
Casts the value to a list of objects.
|
Variant.MapVariant<Object,Object> |
map() |
Variant.MapVariant<Object,Object> |
mapHolder()
Casts the value to
MapHolder . |
<K,V> Variant.MapVariant<K,V> |
mapOf(Class<K> keyClass,
Class<V> valueClass) |
static Variant.RecordVariant |
ofOrigin(RecordHolder value,
Variant.Origin origin,
LspsContextHolder context)
Wraps given RecordHolder, with given origin.
|
static Variant.RecordVariant |
ofUnknownOrigin(RecordHolder value,
LspsContextHolder context)
Wraps given RecordHolder, with no origin.
|
static Variant.RecordVariant |
ofUnknownOrigin(RecordHolder value,
UIComponent origin)
Wraps given RecordHolder which somehow originated from given component (e.g.
|
T |
or(T defaultValue)
Returns the contained instance if it is present; defaultValue otherwise.
|
Variant<T> |
orVariant(T defaultValue)
Returns this instance if it is present; a new variant with the defaultValue otherwise.
|
static <T> Variant<T> |
param(T value,
String paramName,
LspsContextHolder context)
Wraps a value which came as a function parameter.
|
Variant<Path> |
path()
Casts the variant's value to a
Path . |
Variant<Property> |
property()
Casts the variant's value to a
Property . |
Variant<Path> |
propertyPath()
Deprecated.
This method was replaced by
path() to which better fits the type returned. |
Variant.RecordVariant |
record()
Casts the value.
|
Variant<ReferenceHolder> |
reference()
Casts the value to a
ReferenceHolder . |
Variant<T> |
requireDefined()
Asserts that the
definitionType field is present. |
SetVariant<Object> |
set()
Casts the value to a
Set . |
SetVariant<Object> |
setHolder()
Casts the value to a
SetHolder . |
<V> SetVariant<V> |
setOf(Class<V> itemType)
Casts the value to a
Set . |
Variant<ReferenceHolder> |
setReferencedValue(UIComponent scope,
Object newValue)
Sets the referenced value to given value.
|
Variant<ReferenceHolder> |
setReferencedValueCollection(UIComponent scope,
Collection<?> newValue)
Sets the referenced value to given value.
|
protected void |
startTransaction() |
Variant<String> |
string()
Casts the value to String.
|
Variant<T> |
toNull()
Creates a new variant of same type and origin, but with null value.
|
String |
toString() |
Variant<String> |
toUpperCase()
First casts the value to
string , then calls String.toUpperCase() and returns variant holding such string. |
Variant<T> |
toValue(T newValue)
Creates a new variant of same type and origin, but with given value.
|
T |
valueOrNull()
Returns the contained instance if it is present; null otherwise.
|
static <T> Variant<T> |
wrap(T data,
LspsContextHolder context)
Wraps given object in a variant, making it effectively serializable.
|
public static final Variant<?> NULL
public final Type definitionType
requireDefined()
.
Take care when using this value: for example, the source field may be of type SimpleType.OBJECT while the instance is in fact
of type ClosureType.protected final Variant.Origin origin
protected final LspsContextHolder context
public Variant(T data, Type definitionType, Variant.Origin origin, LspsContextHolder contextHolder)
data
- the value, may be null.definitionType
- The type of the value, either the record property type, or the closure result type. null: see requireDefined()
.origin
- The value origin, such as "ui::Geolocation.latitude". Must not be null.contextHolder
- used to serialize ValueHolders. May be null in very special cases when the Variant serialization will not be performed.public Handle getHandle()
IllegalStateException
- if the value is null or is not a ValueHolderpublic boolean hasHandle()
getHandle()
succeeds (the value is a non-null ValueHolder.getHandle()
succeeds and returns a non-null value, false if getHandle()
throws IllegalStateException.public Type getInstanceType()
Decimal
instance,
consults definitionType
to differentiate between DecimalType and IntegerType.SimpleType.NULL
when absent.public static <T> Variant<T> wrap(T data, LspsContextHolder context)
UIComponents.wrap(Object, com.whitestein.lsps.vaadin.LspsContextHolder)
T
- the object typedata
- the object, may be null. Must be either serializable, or a value holder.context
- the context, not nullpublic boolean isPresent()
public Variant<T> checkPresent()
IllegalStateException
- if the value is absent (value is null)public static Variant.RecordVariant definitionOf(UIComponent component)
component
- the component, not null.public static Variant.RecordVariant definitionOf(UIComponentData data)
data
- the component data, not null.public <N> Variant<N> cast(Class<N> type)
absent
.N
- the target typetype
- the target type, not null.IllegalStateException
- if the value is not of required type.public Variant<Boolean> bool()
absent
.IllegalStateException
- if the value is not Boolean.public Variant<String> string()
absent
.IllegalStateException
- if the value is not String.public Variant<Date> date()
absent
.IllegalStateException
- if the value is not Date.public T valueOrNull()
get()
instead.public T get()
or(Object)
or valueOrNull()
instead.IllegalStateException
- if the value is null.public final IllegalStateException fail(String cause)
IllegalStateException
with the following message: "[origin]: invalid value [value]: [cause]"
.
Never returns normally.
To satisfy the compiler, use as follows: throw fail("must be 3..5")
.cause
- the cause, not null.public final IllegalStateException fail(Throwable cause)
IllegalStateException
with the following message: "[origin]: invalid value [value]: [cause]"
.
Never returns normally.
To satisfy the compiler, use as follows: throw fail("must be 3..5")
.cause
- the cause, not null.public T or(T defaultValue)
get()
instead.
For a default value of null, use valueOrNull()
.defaultValue
- the default value, may be null.public Variant<T> orVariant(T defaultValue)
get()
instead.
For a default value of null, use valueOrNull()
.defaultValue
- the default value, may be null.public Variant.RecordVariant record()
absent
.IllegalStateException
- if the value is not RecordHolder.public Variant.ClosureVariant closure()
absent
.public Variant<String> toUpperCase()
string
, then calls String.toUpperCase()
and returns variant holding such string.
If the value is absent, a variant holding null is returned.public <E extends Enum<E>> Variant<E> enumValueOf(Class<E> clazz)
Enum.valueOf(Class, String)
on the value (if the value is not null).E
- the enum typeclazz
- the enum class, not null.IllegalStateException
- if the variant is not string or there is no such enum constant in thepublic <E extends Enum<E>> Variant<E> enumValueOfIgnoreCase(Class<E> clazz)
E
- the enum typeclazz
- the enum class, not null.IllegalStateException
- if the variant is not string or there is no such enum constant in thepublic static Variant.RecordVariant ofUnknownOrigin(RecordHolder value, UIComponent origin)
value
- the record, may be nullorigin
- the originating component, not null.public static Variant.RecordVariant ofUnknownOrigin(RecordHolder value, LspsContextHolder context)
value
- the record holder, may be null.context
- the context. If null, the serialization will fail!public static <T> Variant<T> param(T value, String paramName, LspsContextHolder context)
T
- the value typevalue
- the value, may be nullparamName
- the parameter name, not nullcontext
- context, may be null ONLY if this variant is not going to be serialized.protected void startTransaction()
public static Variant.RecordVariant ofOrigin(RecordHolder value, Variant.Origin origin, LspsContextHolder context)
value
- the record holder, may be null.origin
- the origin of the valuecontext
- the context. If null, the serialization will fail!public Variant<String> enumerationConstant()
EnumerationImpl
instance. Returns the enumeration constant.
Returns an absent variant if this variant is absent.public Variant<Property> property()
Property
.
Fails if the value can not be cast to given type. Never fails if the variant is absent
.Property
.IllegalStateException
- if the value is not of required type.@Deprecated public Variant<Path> propertyPath()
path()
to which better fits the type returned.Path
.
Fails if the value can not be cast to given type. Never fails if the variant is absent
.Path
.IllegalStateException
- if the value is not of required type.public Variant<Path> path()
Path
.
Fails if the value can not be cast to given type. Never fails if the variant is absent
.Path
.IllegalStateException
- if the value is not of required type.public boolean isClosure()
public boolean isProperty()
@Deprecated public boolean isPropertyPath()
isPath()
to avoid confusion
when one could think this method checks the Variant type to
PropertyPath
instead of
com.whitestein.lsps.lang.exec.Path
.Path
.public boolean isPath()
Path
.public Variant.Origin getOrigin()
public Variant<Integer> integer()
Integer
.
Fails if the value can not be cast to given type. Never fails if the variant is absent
.Integer
.IllegalStateException
- if the value is not of required type.public boolean isRecord()
public Variant<Decimal> decimal()
Decimal
. Fails if the value can not be cast to the Decimal
. Never fails if the variant is absent
.Decimal
, never null.IllegalStateException
- if the value is not of required type.public Variant<ReferenceHolder> reference()
ReferenceHolder
. Fails if the value can not be cast to the ReferenceHolder
. Never fails if the variant is absent
.ReferenceHolder
, never null.IllegalStateException
- if the value is not of required type.public Variant<Object> dereference(UIComponent scope)
ReferenceHolder.getValue()
). Returns null value if reference itself is null.scope
- the scope component, not null.IllegalStateException
- if the value is not ReferenceHolder.public Variant<ReferenceHolder> setReferencedValue(UIComponent scope, Object newValue)
absent
).scope
- the scope component, not null.newValue
- the new value, may be null.public Variant<ReferenceHolder> setReferencedValueCollection(UIComponent scope, Collection<?> newValue)
absent
).
Expects the reference to refer a collection type. Creates a compatible type (Set or List), depending on the current value
of the reference and/or the referred type.scope
- the scope component, not null.newValue
- the new value, may be null.public Variant<T> requireDefined()
definitionType
field is present. If the field is null, this may be caused by closure being null, or the field not defined in the record.public SetVariant<Object> setHolder()
SetHolder
. Fails if the value can not be cast to the SetHolder
. Never fails if the variant is absent
.SetHolder
, never null.IllegalStateException
- if the value is not of required type.public SetVariant<Object> set()
Set
. Fails if the value can not be cast to the Set
. Never fails if the variant is absent
.Set
, never null.IllegalStateException
- if the value is not of required type.public <V> SetVariant<V> setOf(Class<V> itemType)
Set
. Fails if the value can not be cast to the Set
. Never fails if the variant is absent
.V
- itemType
- all items are required to be of given type.Set
, never null.IllegalStateException
- if the value is not of required type.public <V> Variant.CollectionVariant<Collection<V>,V> collectionOf(Class<V> itemType)
Collection
. Fails if the value can not be cast to the Collection
. Never fails if the variant is absent
.V
- itemType
- all items are required to be of given type.Collection
, never null.IllegalStateException
- if the value is not of required type.public Variant.CollectionVariant<Collection<Object>,Object> collection()
Collection
. Fails if the value can not be cast to the Collection
. Never fails if the variant is absent
.Collection
, never null.IllegalStateException
- if the value is not of required type.public Variant.ListVariant<Object> listHolder()
ListHolder
. Fails if the value can not be cast to the ListHolder
. Never fails if the variant is absent
.ListHolder
, never null.IllegalStateException
- if the value is not of required type.public Variant.ListVariant<Object> list()
public <V> Variant.ListVariant<V> listOf(Class<V> itemType)
public Variant.MapVariant<Object,Object> map()
public <K,V> Variant.MapVariant<K,V> mapOf(Class<K> keyClass, Class<V> valueClass)
public Variant.MapVariant<Object,Object> mapHolder()
MapHolder
.public boolean instanceOf(Class<?> valueClass)
valueClass
- the expected class, not null.valueClass
, false otherwise.public boolean isAbsent()
public Variant<T> toNull()
public Variant<T> toValue(T newValue)
newValue
- the new value, may be null.Copyright © 2007-2018 Whitestein Technologies. All Rights Reserved.