Class ADEPT2UDTValue
- java.lang.Object
-
- de.aristaflow.adept2.model.datamanagement.ADEPT2UDTValue
-
- All Implemented Interfaces:
UDTValue,TypedCloneable<ADEPT2UDTValue>,Closeable,Serializable,AutoCloseable,Cloneable
- Direct Known Subclasses:
FileUDTValue,JsonUdtValue
public class ADEPT2UDTValue extends Object implements UDTValue, TypedCloneable<ADEPT2UDTValue>
This class provides a lightweight implementation for a user-defined data type (UDT). It encapsulates the value as aRereadableContentand provides the necessary logic for serialisation. The usage ofRereadableContentallows toclose()thisUDTValuewhile continue to use the created streams. Obviously, after closing thisUDTValueyou cannot create new streams any more.
This implementation reads the complete data at creation time. It then resides either in memory or in a file. Equality is based on a SHA-512 hash and not on the content itself.Note that a closed
ADEPT2UDTValuecannot be serialised.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Cleanup<IOException>cleanupThe clean-up called after as post-mortem action of thisADEPT2UDTValue.protected org.apache.commons.lang3.mutable.Mutable<RereadableContent>dataThe value of thisUDTValueasRereadableContent.protected StringuserDefinedTypeNameThe name of the user-defined type.
-
Constructor Summary
Constructors Constructor Description ADEPT2UDTValue(byte[] value, String userDefinedType)Creates a new UDTValue with the designated name and the designated value (byte array).ADEPT2UDTValue(UDTValue udt)Creates a newUDTValuewith the properties and the content of the designatedUDTValue.ADEPT2UDTValue(InputStream stream, long size, String userDefinedType)Creates a new UDTValue with the designated name and the designated value (input stream) having the designated size.ADEPT2UDTValue(InputStream stream, String userDefinedType)Creates a new UDTValue with the designated name and the designated value (input stream).ADEPT2UDTValue(String userDefinedType, byte[] value)Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(byte[], String)instead.ADEPT2UDTValue(String userDefinedType, InputStream stream)Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(InputStream, String)instead.ADEPT2UDTValue(String userDefinedType, InputStream stream, boolean eager)Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(InputStream, String)instead.ADEPT2UDTValue(String userDefinedType, InputStream stream, int length)Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(byte[], String)with aBoundedInputStreaminstead.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ADEPT2UDTValueclone()Clones thisUDTValuein case it isCloneable, otherwise aCloneNotSupportedExceptionwill be thrown (which is what this default implementation does).static UDTValueclone(UDTValue udt, boolean forceRecreation)Clones or recreates the designatedUDTValuedepending on the designatedrecreateand whether ccloning is supported.static UDTValuecloneUnchecked(UDTValue udt, boolean forceRecreation)Clones or recreates the designatedUDTValuedepending on the designatedrecreateand whether ccloning is supported.voidclose()Closes the rereadable content and thus all created input streams (if not closed yet).booleanequals(Object obj)byte[]getBytes()Gets the value as a byte array of this user-defined data type.AttributedInputStreamgetInputStream()Gets the value as anInputStreamof this user-defined data type.LonggetSize()Gets the size of this UDT value if it is known.StringgetUserDefinedType()Gets the name (a string) of the user-defined type of this value.byte[]getValueAsArray()Gets the value as a byte array of this user-defined data type.AttributedInputStreamgetValueAsStream()Gets the value as anInputStreamof this user-defined data type.inthashCode()StringtoString()-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface de.aristaflow.adept2.model.datamanagement.UDTValue
getContentString, getSize, isEmpty
-
-
-
-
Field Detail
-
userDefinedTypeName
protected String userDefinedTypeName
The name of the user-defined type.
-
data
protected org.apache.commons.lang3.mutable.Mutable<RereadableContent> data
The value of thisUDTValueasRereadableContent. This is always completely initialised.
The field will be serialised asbyte[].
-
cleanup
protected transient Cleanup<IOException> cleanup
The clean-up called after as post-mortem action of thisADEPT2UDTValue. Since this is the same logic, it is also used byclose().
-
-
Constructor Detail
-
ADEPT2UDTValue
@Deprecated(since="15.0.0", forRemoval=true) public ADEPT2UDTValue(String userDefinedType, byte[] value)
Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(byte[], String)instead.Creates a new UDTValue with the designated name and the designated value (byte array).- Parameters:
userDefinedType- The name of the user-defined type of the value.value- The bytes representing the value of thisUDTValue. Please take care of the encoding.
-
ADEPT2UDTValue
public ADEPT2UDTValue(byte[] value, String userDefinedType) throws IOExceptionCreates a new UDTValue with the designated name and the designated value (byte array).- Parameters:
value- The bytes representing the value of thisUDTValue. Please take care of the encoding.userDefinedType- The name of the user-defined type of the value.- Throws:
IOException- If there are problems creating the data structures for the content or the hash within theRereadableContent, anIOExceptionwill be thrown.
-
ADEPT2UDTValue
@Deprecated(since="15.0.0", forRemoval=true) public ADEPT2UDTValue(String userDefinedType, InputStream stream)
Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(InputStream, String)instead.Creates a new UDTValue with the designated name and the designated value (input stream). The content of the designated stream will be completely read. Note that the stream will be closed by this constructor!- Parameters:
userDefinedType- The name of the user-defined type of the value.stream- The input stream containing the value of thisUDTValue. Please take care of the encoding. The stream will be closed after the creation.
-
ADEPT2UDTValue
public ADEPT2UDTValue(InputStream stream, String userDefinedType) throws IOException
Creates a new UDTValue with the designated name and the designated value (input stream). Note that the stream will be closed by this constructor!- Parameters:
stream- The input stream containing the value of thisUDTValue. Please take care of the encoding. The stream will be closed after the creation.userDefinedType- The name of the user-defined type of the value.- Throws:
IOException- If there are problems creating the data structures for the content or the hash within theRereadableContent, anIOExceptionwill be thrown.
-
ADEPT2UDTValue
public ADEPT2UDTValue(InputStream stream, long size, String userDefinedType) throws IOException
Creates a new UDTValue with the designated name and the designated value (input stream) having the designated size. Note that the stream will be closed by this constructor!- Parameters:
stream- The input stream containing the value of thisUDTValue. Please take care of the encoding. The stream will be closed after the creation.size- The estimated size of (the contents of) the created stream. Use a negative value if the size is unknown.userDefinedType- The name of the user-defined type of the value.- Throws:
IOException- If there are problems creating the data structures for the content or the hash within theRereadableContent, anIOExceptionwill be thrown.
-
ADEPT2UDTValue
public ADEPT2UDTValue(UDTValue udt) throws IOException
Creates a newUDTValuewith the properties and the content of the designatedUDTValue.
You can also clone() anADEPT2UDTValue. This works similar to this constructor but wraps theIOexceptionin anUncheckedIOException.- Parameters:
udt- TheUDTValuewhich to clone. The caller is responsible for closing.- Throws:
IOException- If there are problems creating the data structures for the content or the hash within theRereadableContent, anIOExceptionwill be thrown.
-
ADEPT2UDTValue
@Deprecated(since="15.0.0", forRemoval=true) public ADEPT2UDTValue(String userDefinedType, InputStream stream, boolean eager)
Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(InputStream, String)instead.Creates a new UDTValue with the designated name and the designated value (input stream). Note that the stream will be closed by this constructor!
No laziness is supported any more, the complete data will be read immediately.- Parameters:
userDefinedType- The name of the user-defined type of the value.stream- The input stream containing the value of thisUDTValue. Please take care of the encoding. The stream will be closed after the creation.eager- Ignored.
-
ADEPT2UDTValue
@Deprecated(since="15.0.0", forRemoval=true) public ADEPT2UDTValue(String userDefinedType, InputStream stream, int length)
Deprecated, for removal: This API element is subject to removal in a future version.UseADEPT2UDTValue(byte[], String)with aBoundedInputStreaminstead.Creates a new UDTValue with the designated name and the designated value (input stream). The content of the designated stream will be read by the designated length eagerly. If the stream contains more content, the remaining content will be ignored. Note that the stream will not be closed!- Parameters:
userDefinedType- The name of the user-defined type of the value.stream- The input stream containing the value of thisUDTValue. Please take care of the encoding. The stream will be closed after the creation.length- The number of bytes to be read from the designated input stream.- Throws:
IllegalArgumentException- If the designated number of bytes cannot be read from the stream, anIllegalArgumentExceptionwill be thrown.
-
-
Method Detail
-
getUserDefinedType
public String getUserDefinedType()
Description copied from interface:UDTValueGets the name (a string) of the user-defined type of this value.- Specified by:
getUserDefinedTypein interfaceUDTValue- Returns:
- The name of the user-defined data type of this value.
-
getValueAsStream
public AttributedInputStream getValueAsStream()
Description copied from interface:UDTValueGets the value as anInputStreamof this user-defined data type.
You have to close the stream after usage.- Specified by:
getValueAsStreamin interfaceUDTValue- Returns:
- The value of a (this) user-defined data type as an
InputStream. The caller is responsible for closing.
-
getInputStream
public AttributedInputStream getInputStream() throws IOException
Gets the value as anInputStreamof this user-defined data type.
You have to close the stream after usage. Usually you should notUDTValue.close()thisUDTValuebefore all created input streams have been consumed. Implementations may deviate so that closing theUDTValuedoes not affect the created input streams. closing will not affect theInputStream.- Specified by:
getInputStreamin interfaceUDTValue- Returns:
- The value of a (this) user-defined data type as an
InputStream. The caller is responsible for closing. - Throws:
IOException- If there are problems creating a correspondingInputStream, anIOExceptionwill be thrown.
-
getValueAsArray
public byte[] getValueAsArray()
Description copied from interface:UDTValueGets the value as a byte array of this user-defined data type.- Specified by:
getValueAsArrayin interfaceUDTValue- Returns:
- The value of a (this) user-defined data type as an array of bytes.
-
getSize
public Long getSize()
Description copied from interface:UDTValueGets the size of this UDT value if it is known. For instance, if the content stems from a stream and the stream is not read yet, the size cannot be determined andnullwill be returned.
-
getBytes
public byte[] getBytes() throws IOExceptionDescription copied from interface:UDTValueGets the value as a byte array of this user-defined data type.- Specified by:
getBytesin interfaceUDTValue- Returns:
- The value of a (this) user-defined data type as an array of bytes.
- Throws:
IOException- If there are problems retrieving the corresponding array, e. g the length of the data exceeds the maximum length of an array, anIOExceptionwill be thrown.
-
clone
public ADEPT2UDTValue clone()
Description copied from interface:UDTValueClones thisUDTValuein case it isCloneable, otherwise aCloneNotSupportedExceptionwill be thrown (which is what this default implementation does). Subclasses should provide a corresponding implementation and also extendCloneable. Those which do not, should forward to this default implementation.- Specified by:
clonein interfaceTypedCloneable<ADEPT2UDTValue>- Specified by:
clonein interfaceUDTValue- Overrides:
clonein classObject- Returns:
- A clone of this
UDTValueinstance.
-
close
public void close() throws IOExceptionCloses the rereadable content and thus all created input streams (if not closed yet).- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceUDTValue- Throws:
IOException
-
clone
public static UDTValue clone(UDTValue udt, boolean forceRecreation) throws IOException
Clones or recreates the designatedUDTValuedepending on the designatedrecreateand whether ccloning is supported. Ifrecreateor cloning is not supported or fails, the designatedUDTValuewill be recreated as new instance ofADEPT2UDTValue.- Parameters:
udt- TheUDTValuewhich to clone or recreate. This may benull.forceRecreation- Whether recreating asADEPT2UDTValueshould be enforced. Otherwise cloning will be preferred with a fallback to recreation in case of failure.- Returns:
- A clone or recreation of the designated
UDTValueornull. - Throws:
IOException- If there are problems creating a newADEPT2UDTValue(may be a consequence of unsuccessful cloning), the correspondingIOExceptionwill be thrown.
-
cloneUnchecked
public static UDTValue cloneUnchecked(UDTValue udt, boolean forceRecreation)
Clones or recreates the designatedUDTValuedepending on the designatedrecreateand whether ccloning is supported. Ifrecreateor cloning is not supported or fails, the designatedUDTValuewill be recreated as new instance ofADEPT2UDTValue.- Parameters:
udt- TheUDTValuewhich to clone or recreate. This may benull.forceRecreation- Whether recreating asADEPT2UDTValueshould be enforced. Otherwise cloning will be preferred with a fallback to recreation in case of failure.- Returns:
- A clone or recreation of the designated
UDTValueornull. - Throws:
UncheckedIOException- If there are problems creating a newADEPT2UDTValue(may be a consequence of unsuccessful cloning), the correspondingIOExceptionwill be wrapped with anUncheckedIOExceptionand re-thrown.
-
-