T - The type of the serialized form. This is generally unknown to all except the DynamicOps
instance that defines the serialization behavior.public abstract class DynamicLike<T> extends Object
Instances of this class effectively encapsulate a serialized value and the DynamicOps that
can produce and parse it. This allows for more fluid handling of serialized values without needing to
separately pass a DynamicOps object everywhere.
Dynamic,
DynamicOps| Modifier and Type | Field and Description |
|---|---|
protected DynamicOps<T> |
ops
The
DynamicOps instance used to deserialize values. |
| Constructor and Description |
|---|
DynamicLike(DynamicOps<T> ops)
Constructs a new
DynamicLike with the given serialization operations. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
asBoolean(boolean defaultValue)
Coerces and returns the wrapped value as a boolean, using the given value as a fallback.
|
byte |
asByte(byte defaultValue)
Coerces and returns the wrapped value as a byte, using the given value as a fallback.
|
ByteBuffer |
asByteBuffer()
Coerces and returns the wrapped value as a
ByteBuffer. |
abstract DataResult<ByteBuffer> |
asByteBufferOpt()
Coerces and returns the wrapped value as a
ByteBuffer in a DataResult. |
double |
asDouble(double defaultValue)
Coerces and returns the wrapped value as a double, using the given value as a fallback.
|
float |
asFloat(float defaultValue)
Coerces and returns the wrapped value as a float, using the given value as a fallback.
|
int |
asInt(int defaultValue)
Coerces and returns the wrapped value as an int, using the given value as a fallback.
|
IntStream |
asIntStream()
Coerces and returns the wrapped value as a
IntStream. |
abstract DataResult<IntStream> |
asIntStreamOpt()
Coerces and returns the wrapped value as a stream of int values in a
DataResult. |
<U> List<U> |
asList((Dynamic<T>) -> U deserializer)
Coerces and returns the wrapped value as a
List. |
<U> DataResult<List<U>> |
asListOpt((Dynamic<T>) -> U deserializer)
Uses the given deserialization function to decode a list on elements from the wrapped value.
|
long |
asLong(long defaultValue)
Coerces and returns the wrapped value as a long, using the given value as a fallback.
|
LongStream |
asLongStream()
Coerces and returns the wrapped value as a
LongStream. |
abstract DataResult<LongStream> |
asLongStreamOpt()
Coerces and returns the wrapped value as a stream of long values in a
DataResult. |
<K,V> Map<K,V> |
asMap((Dynamic<T>) -> K keyDeserializer,
(Dynamic<T>) -> V valueDeserializer)
Coerces and returns the wrapped value as a
Map. |
abstract DataResult<Stream<(Dynamic<T>,Dynamic<T>)>> |
asMapOpt()
Coerces and returns the wrapped value as a map of entries in a
DataResult. |
<K,V> DataResult<Map<K,V>> |
asMapOpt((Dynamic<T>) -> K keyDeserializer,
(Dynamic<T>) -> V valueDeserializer)
Uses the given deserialization functions to decode a map on key-value pairs from the wrapped value.
|
abstract DataResult<Number> |
asNumber()
Coerces and returns the wrapped value as a number in a
DataResult. |
Number |
asNumber(Number defaultValue)
Coerces and returns the wrapped value as a number, using the given value as a fallback.
|
short |
asShort(short defaultValue)
Coerces and returns the wrapped value as a short, using the given value as a fallback.
|
Stream<Dynamic<T>> |
asStream()
Coerces and returns the wrapped value as a
Stream. |
abstract DataResult<Stream<Dynamic<T>>> |
asStreamOpt()
Coerces and returns the wrapped value as a stream of values in a
DataResult. |
abstract DataResult<String> |
asString()
Coerces and returns the wrapped value as a string in a
DataResult. |
String |
asString(String defaultValue)
Coerces and returns the wrapped value as a
String, using the given value as a fallback. |
Dynamic<T> |
createBoolean(boolean value)
Returns a dynamic value representing the given boolean value.
|
Dynamic<T> |
createByte(byte value)
Returns a dynamic value representing the given byte value.
|
Dynamic<?> |
createByteList(ByteBuffer input)
Creates a dynamic list from the given byte buffer
|
Dynamic<T> |
createDouble(double value)
Returns a dynamic value representing the given double value.
|
Dynamic<T> |
createFloat(float value)
Returns a dynamic value representing the given float value.
|
Dynamic<T> |
createInt(int value)
Returns a dynamic value representing the given byte value.
|
Dynamic<?> |
createIntList(IntStream input)
Creates a dynamic list from the given
IntStream. |
Dynamic<T> |
createList(Stream<? extends Dynamic<?>> input)
Creates a dynamic list from the given stream of dynamic values.
|
Dynamic<T> |
createLong(long value)
Returns a dynamic value representing the given long value.
|
Dynamic<?> |
createLongList(LongStream input)
Creates a dynamic list from the given
LongStream. |
Dynamic<T> |
createMap(Map<? extends Dynamic<?>,? extends Dynamic<?>> map)
Creates a dynamic map from the given map of dynamic values.
|
Dynamic<T> |
createNumeric(Number i)
Returns a dynamic value representing the given number.
|
Dynamic<T> |
createShort(short value)
Returns a dynamic value representing the given short value.
|
Dynamic<T> |
createString(String value)
Returns a dynamic value representing the given
String value. |
abstract <A> DataResult<(A,T)> |
decode(Decoder<? extends A> decoder)
Decodes an object using the given
Decoder, and returns a DataResult containing a pair
of the decoded object and the remaining serialized value. |
Dynamic<T> |
emptyList()
Returns a dynamic value representing the empty list.
|
Dynamic<T> |
emptyMap()
Returns a dynamic value representing the empty map.
|
abstract OptionalDynamic<T> |
get(String key)
If the given key is present in the wrapped value, returns the value associated with it, else returns empty.
|
abstract DataResult<T> |
getElement(String key)
If the given key is present in the wrapped value, returns the value associated with it, else returns empty.
|
T |
getElement(String key,
T defaultValue)
Retrieves the value associated with the given key in the wrapped value, or returns the default value
if none was found.
|
abstract DataResult<T> |
getElementGeneric(T key)
If the key represented by the given serialized value is present in the wrapped value, returns the value
associated with it, else returns empty.
|
T |
getElementGeneric(T key,
T defaultValue)
Retrieves the value associated with the given key in the wrapped value, or returns the default value
if none was found.
|
abstract DataResult<T> |
getGeneric(T key)
If the key represented by the given serialized value is present in the wrapped value, returns the value
associated with it, else returns empty.
|
DynamicOps<T> |
getOps()
Retrieves the
DynamicOps object that defines the serialization behavior of this. |
<A> DataResult<A> |
read(Decoder<? extends A> decoder)
Decodes an object from the wrapped value using the given
Decoder and returns it in a DataResult. |
<E> DataResult<List<E>> |
readList(Decoder<E> decoder)
Decodes a list of elements from the wrapped value using the given
Decoder, and returns it in a
DataResult. |
<E> DataResult<List<E>> |
readList((? super Dynamic<?>) -> ? extends DataResult<? extends E> decoder)
Uses the given deserialization function to deserialize a list of elements from the wrapped value.
|
<R> DataResult<R> |
readMap(DataResult<R> empty,
(R,Dynamic<T>,Dynamic<T>) -> DataResult<R> combiner)
Performs a reduction operation on the map entries extracted from the wrapped value.
|
<K,V> DataResult<List<(K,V)>> |
readMap(Decoder<K> keyDecoder,
Decoder<V> valueDecoder)
Decodes a map into a list of pairs using the given key decoder and value decoder.
|
<K,V> DataResult<List<(K,V)>> |
readMap(Decoder<K> keyDecoder,
(K) -> Decoder<V> valueDecoder)
Decodes a map into a list of pairs using the given key decoder and value decoder.
|
protected final DynamicOps<T> ops
DynamicOps instance used to deserialize values.public DynamicLike(DynamicOps<T> ops)
DynamicLike with the given serialization operations.ops - The operations for the serialized type this object wraps.public DynamicOps<T> getOps()
DynamicOps object that defines the serialization behavior of this.public abstract DataResult<Number> asNumber()
DataResult.DynamicOps.getNumberValue(Object)public abstract DataResult<String> asString()
DataResult.DynamicOps.getStringValue(Object)public abstract DataResult<Stream<Dynamic<T>>> asStreamOpt()
DataResult.DynamicOps.getStream(Object)public abstract DataResult<Stream<(Dynamic<T>,Dynamic<T>)>> asMapOpt()
DataResult.DynamicOps.getMapValues(Object)public abstract DataResult<ByteBuffer> asByteBufferOpt()
ByteBuffer in a DataResult.DynamicOps.getByteBuffer(Object)public abstract DataResult<IntStream> asIntStreamOpt()
DataResult.DynamicOps.getIntStream(Object)public abstract DataResult<LongStream> asLongStreamOpt()
DataResult.DynamicOps.getLongStream(Object)public abstract OptionalDynamic<T> get(String key)
key - The key to search for.OptionalDynamic.public abstract DataResult<T> getGeneric(T key)
key - The key to search for.DataResult.public abstract DataResult<T> getElement(String key)
getGeneric(Object).key - The key to search for.DataResult.get(String)public abstract DataResult<T> getElementGeneric(T key)
getGeneric(Object).key - The key to search for.DataResult.getGeneric(Object)public abstract <A> DataResult<(A,T)> decode(Decoder<? extends A> decoder)
Decoder, and returns a DataResult containing a pair
of the decoded object and the remaining serialized value.A - The type of the decoded object.decoder - The decoder to use.DataResult containing the decoded object and the remaining serialized value.public <U> DataResult<List<U>> asListOpt((Dynamic<T>) -> U deserializer)
U - The type of the deserialized elements.deserializer - The deserialization function.DataResult containing the list of values.public <K,V> DataResult<Map<K,V>> asMapOpt((Dynamic<T>) -> K keyDeserializer, (Dynamic<T>) -> V valueDeserializer)
asMapOpt(), then transforms the keys and values using the given
functions. In the case of duplicate keys, the key encountered last is preserved.K - The type of the deserialized keys.V - The type of the deserialized values.keyDeserializer - The key deserialization function.valueDeserializer - The value deserialization function.DataResult containing the deserialized map.public <A> DataResult<A> read(Decoder<? extends A> decoder)
Decoder and returns it in a DataResult.decode(Decoder) and returns the first element of the resulting
pair.A - The type of the decoded value.decoder - The decoder to use.DataResult containing the decoded value.public <E> DataResult<List<E>> readList(Decoder<E> decoder)
Decoder, and returns it in a
DataResult.asStreamOpt(), then decodes each element of the list using
read(Decoder).E - The element type of the decoded list.decoder - The decoder to use.DataResult containing the decoded values.public <E> DataResult<List<E>> readList((? super Dynamic<?>) -> ? extends DataResult<? extends E> decoder)
DataResult for any element, this method also returns
an error DataResult.E - The element type of the deserialized list.decoder - The deserialization function.DataResult containing the deserialized list elements.public <K,V> DataResult<List<(K,V)>> readMap(Decoder<K> keyDecoder, Decoder<V> valueDecoder)
asMapOpt(), then decodes the resulting keys and
values using the given decoders.K - The key type.V - The value type.keyDecoder - The Decoder for the keys.valueDecoder - The Decoder for the values.DataResult containing the decoded list of pairs.public <K,V> DataResult<List<(K,V)>> readMap(Decoder<K> keyDecoder, (K) -> Decoder<V> valueDecoder)
The value decoder used is determined using the valueDecoder function, which returns a decoder to
use based on the key.
asMapOpt(), then decodes the resulting keys and
values using the given decoders.K - The key type.V - The value type.keyDecoder - The Decoder for the keys.valueDecoder - The function used to determine the Decoder for the values.DataResult containing the decoded list of pairs.public <R> DataResult<R> readMap(DataResult<R> empty, (R,Dynamic<T>,Dynamic<T>) -> DataResult<R> combiner)
asMapOpt(), then performs a reduction operation on the elements
of the returned stream.R - The type of the result.empty - The empty or identity value of the result type.combiner - A function that takes the accumulated result and the serialized key and value, and produces
a new result value.DataResult containing the result value.Stream.reduce(Object, java.util.function.BinaryOperator)public Number asNumber(Number defaultValue)
defaultValue - The fallback value.asNumber()public int asInt(int defaultValue)
defaultValue - The fallback value.public long asLong(long defaultValue)
defaultValue - The fallback value.public float asFloat(float defaultValue)
defaultValue - The fallback value.public double asDouble(double defaultValue)
defaultValue - The fallback value.public byte asByte(byte defaultValue)
defaultValue - The fallback value.public short asShort(short defaultValue)
defaultValue - The fallback value.public boolean asBoolean(boolean defaultValue)
asNumber() and compares its int value with zero.defaultValue - The fallback value.public String asString(String defaultValue)
String, using the given value as a fallback.defaultValue - The fallback value.asString()public Stream<Dynamic<T>> asStream()
Stream.asStreamOpt()public ByteBuffer asByteBuffer()
ByteBuffer.asByteBufferOpt()public IntStream asIntStream()
IntStream.asIntStreamOpt()public LongStream asLongStream()
LongStream.asLongStreamOpt()public <U> List<U> asList((Dynamic<T>) -> U deserializer)
List.asListOpt(Function)public <K,V> Map<K,V> asMap((Dynamic<T>) -> K keyDeserializer, (Dynamic<T>) -> V valueDeserializer)
Map.asMapOpt(Function, Function)public T getElement(String key, T defaultValue)
key - The key to search for.defaultValue - The default value.getElement(String)public T getElementGeneric(T key, T defaultValue)
key - The representation of the key to search for.defaultValue - The default value.getElementGeneric(Object)public Dynamic<T> emptyList()
DynamicOps.emptyList()public Dynamic<T> emptyMap()
DynamicOps.emptyMap()public Dynamic<T> createNumeric(Number i)
DynamicOps.createNumeric(Number)public Dynamic<T> createByte(byte value)
DynamicOps.createByte(byte)public Dynamic<T> createShort(short value)
DynamicOps.createShort(short)public Dynamic<T> createInt(int value)
DynamicOps.createByte(byte)public Dynamic<T> createLong(long value)
DynamicOps.createLong(long)public Dynamic<T> createFloat(float value)
DynamicOps.createFloat(float)public Dynamic<T> createDouble(double value)
DynamicOps.createDouble(double)public Dynamic<T> createBoolean(boolean value)
DynamicOps.createBoolean(boolean)public Dynamic<T> createString(String value)
String value.DynamicOps.createString(String)public Dynamic<T> createList(Stream<? extends Dynamic<?>> input)
DynamicOps.createList(Stream)public Dynamic<T> createMap(Map<? extends Dynamic<?>,? extends Dynamic<?>> map)
DynamicOps.createMap(Map)public Dynamic<?> createByteList(ByteBuffer input)
DynamicOps.createByteList(ByteBuffer)public Dynamic<?> createIntList(IntStream input)
IntStream.DynamicOps.createIntList(IntStream)public Dynamic<?> createLongList(LongStream input)
LongStream.DynamicOps.createLongList(LongStream)