Interface DataType<V>
- Type Parameters:
V
- value type
- All Known Subinterfaces:
BinaryDataType
,JSONDataType
,RecordV2DataType
implementations
.
Value types should provide a string representation
and define reasonable equality
. The value type
implementations used by Diffusion-provided data types do so.
A data type can optionally support incremental changes to values, represented
by one or more types of delta. For each type of delta it supports,
the data type provides an implementation of DeltaType
via
deltaType(String)
and deltaType(Class)
.
- Since:
- 5.7
- Author:
- DiffusionData Limited
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns the binary delta type for this data type, if any.<T> boolean
Test whether this data type is compatible withclassOfT
.Obtain aDeltaType
by class.Obtain aDeltaType
by name.Returns the external type identifier.<T> T
Create a value of a compatible class from binary.<T> T
Create a value of a compatible class from a binary.<T> T
Create a value of a compatible class from binary.readValue
(byte[] bytes) Create a value from binary.readValue
(byte[] bytes, int offset, int length) Create a value from binary.Create a value from binary.Return the binary delta type that for serialized data of this data type, if any.Returns the serialized form ofvalue
as aBytes
.void
Variant ofvalidate(Object)
that takes the serialized form of the value.void
Check whether a value is valid.void
writeValue
(V value, OutputStream out) Serialize a value to binary.
-
Method Details
-
getTypeName
String getTypeName()Returns the external type identifier.- Returns:
- a unique name identifying this data type
-
writeValue
Serialize a value to binary.- Throws:
IOException
- if the OutputStream doesIllegalArgumentException
- if the implementation does not support the provided value
-
toBytes
Returns the serialized form ofvalue
as aBytes
.- Throws:
IllegalArgumentException
- if the implementation does not support the provided value- Since:
- 6.0
-
readValue
V readValue(byte[] bytes, int offset, int length) throws InvalidDataException, IndexOutOfBoundsException Create a value from binary.Implementations can choose not to fully validate values when they are read, but instead defer parsing until it is required. See
validate(Object)
.- Parameters:
bytes
- The binary data. The implementation may re-use the array to avoid copying so the caller must ensure the array is not modified.offset
- start of the data within byteslength
- length of the data within bytes- Throws:
InvalidDataException
- Ifbytes
does not represent a validV
. The implementation may defer validation; seevalidate(Object)
.IndexOutOfBoundsException
- if eitheroffset
orlength
is negative, oroffset + length > bytes.length
-
readValue
Create a value from binary. Equivalent toreadValue(in, 0, in.length)
.- Parameters:
bytes
- The binary data. The implementation may re-use the array to avoid copying so the caller must ensure the array is not modified.- Throws:
InvalidDataException
- Ifbytes
does not represent a validV
. The implementation may defer validation; seevalidate(Object)
.
-
readValue
Create a value from binary. Equivalent toreadValue(bytes.toByteArray())
.- Parameters:
bytes
- the binary data- Throws:
InvalidDataException
- Ifbytes
does not represent a validV
. The implementation may defer validation; seevalidate(Object)
.
-
validate
Check whether a value is valid.A
DataType
implementation is not required to check the binary data supplied toreadValue(byte[], int, int)
can be parsed as a value of typeV
if doing so is unnecessarily costly. Instead a value may simply wrap the binary data, and lazily deserialize as required. This method can be used the check the value at a later time.- Throws:
InvalidDataException
- if the value is invalid
-
validate
Variant ofvalidate(Object)
that takes the serialized form of the value.- Throws:
InvalidDataException
- if the bytes cannot be deserialized to a valid value- Since:
- 6.11
-
readAs
<T> T readAs(Class<T> classOfT, byte[] bytes, int offset, int length) throws InvalidDataException, IllegalArgumentException, IndexOutOfBoundsException Create a value of a compatible class from a binary.If
valueType
is incompatible with this data type, this method will throw an IllegalArgumentException. Compatibility can be tested withcanReadAs(Class)
.- Type Parameters:
T
- type ofclassOfT
- Parameters:
classOfT
- the type of the resultbytes
- The binary data. The implementation may re-use the array to avoid copying so the caller must ensure the array is not modified.offset
- start of the data within byteslength
- length of the data within bytes- Throws:
InvalidDataException
- Ifbytes
does not represent a validV
, and by extension cannot be provided as aT
. The implementation may defer validation; seevalidate(Object)
.IllegalArgumentException
- ifclassOfT
is incompatible with this data typeIndexOutOfBoundsException
- if eitheroffset
orlength
is negative, oroffset + length > bytes.length
- Since:
- 6.0
-
readAs
Create a value of a compatible class from binary. Equivalent toreadAs(classOfT, in, 0, in.length)
.- Type Parameters:
T
- type ofclassOfT
- Parameters:
classOfT
- the type of the resultbytes
- The binary data. The implementation may re-use the array to avoid copying so the caller must ensure the array is not modified.- Throws:
InvalidDataException
- Ifbytes
does not represent a validV
, and by extension cannot be provided as aT
. The implementation may defer validation; seevalidate(Object)
.IllegalArgumentException
- ifclassOfT
is incompatible with this data type- Since:
- 6.0
-
readAs
Create a value of a compatible class from binary. Equivalent toreadAs(classOfT, bytes.toByteArray())
.- Type Parameters:
T
- type ofclassOfT
- Parameters:
classOfT
- the type of the resultbytes
- the binary data- Throws:
InvalidDataException
- Ifbytes
does not represent a validV
, and by extension cannot be provided as aT
. The implementation may defer validation; seevalidate(Object)
.IllegalArgumentException
- ifclassOfT
is incompatible with this data type- Since:
- 6.0
-
canReadAs
Test whether this data type is compatible withclassOfT
. Compatibility with aclassOfT
means that any valid binary representation of aV
can beread as
an instance ofclassOfT
.Every data type should be compatible with the following:
Class<V>
– the class corresponding to implementation's value type. For a data type with a value type ofX
,readAs(X.class, bytes)
is equivalent toreadValue(bytes)
.Class<? super V>
– any super type of the class corresponding to implementation's value type. Consequently every data type is compatible withClass<Object>
.Class<Bytes>
.
Compatibility is an asymmetric relationship. For example, the string data type is compatible with
Class<JSON>
, but the JSON data type is incompatible withClass<<String>
.In addition to compatibility with
Class<V>
andClass<Bytes>
, the standard data types implement compatibility according to the type hierarchy shown in the following diagram:For example, the string data type is compatible with
Class<String>
,Class<JSON>
andClass<Bytes>
, as well as super types ofClass<String>
such asClass<Object>
andClass<CharSequence>
.Data type compatibility is used in several places in the Diffusion API:
Value streams
only receive updates from topics with compatible data types, and convert received values appropriately.Topics.FetchRequest.withValues(java.lang.Class<? extends T>)
constrains a fetch operation to retrieve data from topics with compatible data types, and convert the values appropriately.TimeSeries.RangeQuery.as(java.lang.Class<T>)
converts the values resulting from time series range query appropriately; the query will fail if the time series topic data type is an incompatible data type.- Values sent using the
Messaging
feature will not be delivered if the recipientrequest stream
orrequest handler
is incompatible with the value data type.
- Type Parameters:
T
- type ofclassOfT
- Parameters:
classOfT
- the type to check- Returns:
- true if a binary representation created by this data type can read as an instance of classOfT
- Since:
- 6.0
-
deltaType
Obtain aDeltaType
by name.- Parameters:
name
- the name, as returned byDeltaType.getName()
- Returns:
- the delta type
- Throws:
IllegalArgumentException
- if this data type does not provide aDeltaType
with the nametypeName
- See Also:
-
deltaType
Obtain aDeltaType
by class.- Parameters:
deltaClass
- the class- Returns:
- the delta type
- Throws:
IllegalArgumentException
- if this data type does not provide aDeltaType
for deltas of classdeltaClass
IllegalArgumentException
- if the data type provides more than oneDeltaType
for deltas of classdeltaClass
, but none is preferred- See Also:
-
binaryDeltaType
DeltaType<V,BinaryDelta> binaryDeltaType()Returns the binary delta type for this data type, if any.This method behaves similarly to
deltaType(BinaryDelta.class)
except it returnsnull
if this data type does not support binary deltas rather than throwingIllegalArgumentException
.- Returns:
- the delta type, or null if none
- Since:
- 6.3
-
serializedBinaryDeltaType
DeltaType<Bytes,BinaryDelta> serializedBinaryDeltaType()Return the binary delta type that for serialized data of this data type, if any.- Returns:
- the delta type, or null if none
- Since:
- 6.11
-