Class ImmutableGsonDocument
java.lang.Object
eu.cloudnetservice.driver.document.gson.ImmutableGsonDocument
- All Implemented Interfaces:
Document,DefaultedDocPropertyHolder,DocPropertyHolder,Serializable
- Direct Known Subclasses:
MutableGsonDocument
Immutable version of a gson document implementing the full document functionality.
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface eu.cloudnetservice.driver.document.property.DefaultedDocPropertyHolder
DefaultedDocPropertyHolder.Mutable<S extends DocPropertyHolder.Mutable<S>>Nested classes/interfaces inherited from interface eu.cloudnetservice.driver.document.Document
Document.Mutable -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final com.google.gson.JsonObjectprivate static final long -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, empty gson document instance.ImmutableGsonDocument(@NonNull com.google.gson.JsonObject internalObject) Constructs a new gson document instance using the given initial internal object. -
Method Summary
Modifier and TypeMethodDescriptionbooleanGet if this document has a value mapping for the given key.intGet the amount of key-value mappings that are present in the document.@Unmodifiable @NonNull Collection<? extends Element>elements()Get a snapshot of all serialized key-value pairs registered in this document at the moment this method was called.booleanempty()Get if the document is empty (therefore has no key-value mappings present).booleanEnsures that the given object is a document of the same type and that all members of this document are present in the given other document.Get the name of the factory that is able to construct this document type.booleangetBoolean(@NonNull String key, boolean def) Reads a boolean that is associated with the given key from this document.byteReads a byte that is associated with the given key from this document.doubleReads a double that is associated with the given key from this document.private @NonNull com.google.gson.JsonElementgetElementSafe(@NonNull String key) Internal helper method to safely read a json element from the underlying json object.floatReads a float that is associated with the given key from this document.intReads an int that is associated with the given key from this document.longReads a long that is associated with the given key from this document.private @Nullable com.google.gson.JsonPrimitiveInternal helper method to get a json element as JsonPrimitive if it is one, else returns null.shortReads a short that is associated with the given key from this document.Reads a string that is associated with the given key from this document.inthashCode()Makes an immutable copy of this document.keys()Get a snapshot view of the keys that are registered in this document at the moment this method was called.Makes a mutable copy of this document.Get the underlying document that all read and write operations are delegated to.readDocument(@NonNull String key, @Nullable Document def) Reads a document of the same type from this document or returns an empty document if no value is associated with the given key.readMutableDocument(@NonNull String key, Document.Mutable def) Reads a mutable document of the same type from this document or returns an empty document if no value is associated with the given key.private voidReads a json object from the given stream and copies all it's members into this document.<T> @UnknownNullability TreadObject(@NonNull String key, @NonNull io.leangen.geantyref.TypeToken<T> type, T def) Reads the associated value of the given key and converts it to the given type model.<T> @UnknownNullability TreadObject(@NonNull String key, @NonNull Class<T> type, T def) Reads the associated value of the given key and converts it to the given type model.<T> @UnknownNullability TreadObject(@NonNull String key, @NonNull Type type, T def) Reads the associated value of the given key and converts it to the given type model.private @NonNull com.google.gson.GsonResolves the gson instance to use to serialize this document, throwing an exception if the given serialisation style is not supported.send()Converts this document into a version that can be cross-handled by other document implementations.Serializes this document to a string with the given style.<T> @UnknownNullability TtoInstanceOf(@NonNull io.leangen.geantyref.TypeToken<T> type) Converts the underlying key-value mappings to an instance of the given type.<T> @UnknownNullability TtoInstanceOf(@NonNull Class<T> type) Converts the underlying key-value mappings to an instance of the given type.<T> @UnknownNullability TtoInstanceOf(@NonNull Type type) Converts the underlying key-value mappings to an instance of the given type.toString()Returns this document compact serialised.private voidWrites this document in a compact way to the given output stream.voidwriteTo(@NonNull OutputStream stream, @NonNull SerialisationStyle style) Writes this document with the given style to the given output stream.voidwriteTo(@NonNull Appendable appendable, @NonNull SerialisationStyle style) Appends this document with the given style to the given appendable.voidwriteTo(@NonNull Path path, @NonNull SerialisationStyle style) Writes this document with the given style into a file at the given path.voidwriteTo(DataBuf.Mutable dataBuf, @NonNull SerialisationStyle style) Writes this document with the given style as a string to the given data buffer.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface eu.cloudnetservice.driver.document.property.DefaultedDocPropertyHolder
propertyAbsent, propertyPresent, readProperty, readPropertyOrDefault, readPropertyOrGet, readPropertyOrThrow, readPropertyOrThrowMethods inherited from interface eu.cloudnetservice.driver.document.Document
getBoolean, getByte, getDouble, getFloat, getInt, getLong, getShort, getString, readDocument, readMutableDocument, readObject, readObject, readObject, serializeToString, writeTo, writeTo, writeTo, writeTo
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
internalObject
protected final com.google.gson.JsonObject internalObject
-
-
Constructor Details
-
ImmutableGsonDocument
public ImmutableGsonDocument()Constructs a new, empty gson document instance. -
ImmutableGsonDocument
Constructs a new gson document instance using the given initial internal object. Note that the given object is not copied, it is up to the caller to ensure no races or data leaks are created when using this constructor.- Parameters:
internalObject- the initial internal json object to use.- Throws:
NullPointerException- if the given internal object is null.
-
-
Method Details
-
factoryName
Get the name of the factory that is able to construct this document type.- Specified by:
factoryNamein interfaceDocument- Returns:
- the factory name of this document type.
-
empty
public boolean empty()Get if the document is empty (therefore has no key-value mappings present). -
elementCount
public int elementCount()Get the amount of key-value mappings that are present in the document. Zero indicates that the document is empty.- Specified by:
elementCountin interfaceDocument- Returns:
- the amount of key-value mappings in this document.
-
contains
Get if this document has a value mapping for the given key. Note that the given key is not interpreted as a path. -
send
Converts this document into a version that can be cross-handled by other document implementations. The serialized form of this document can be used to convert it to a different type of document. Note that the returned form is a snapshot of the document at the time the method was called. Any changes made to this document after the method call are not reflected into the document send. -
immutableCopy
Makes an immutable copy of this document. Changes to this document (in case it is mutable) are not reflected into the returned document and vice-versa.- Specified by:
immutableCopyin interfaceDocument- Returns:
- an immutable copy of this document.
-
mutableCopy
Makes a mutable copy of this document. Changes to this document (in case it is mutable) are not reflected into the returned document and vice-versa.- Specified by:
mutableCopyin interfaceDocument- Returns:
- a mutable copy of this document.
-
keys
Get a snapshot view of the keys that are registered in this document at the moment this method was called. New keys that are registered after the method call are not reflecting into the returned collection and vice-versa. -
elements
Get a snapshot of all serialized key-value pairs registered in this document at the moment this method was called. New pairs that are registered after the method call are not reflecting into the returned collection and vice-versa. -
toInstanceOf
Converts the underlying key-value mappings to an instance of the given type. Note that the type must represent an object and cannot be of any other type (like an array or primitive type).- Specified by:
toInstanceOfin interfaceDocument- Type Parameters:
T- the type to model from the underlying data.- Parameters:
type- the type to construct from the underlying key-value data.- Returns:
- the constructed instance of the given type.
-
toInstanceOf
Converts the underlying key-value mappings to an instance of the given type. Note that the type must represent an object and cannot be of any other type (like an array or primitive type).- Specified by:
toInstanceOfin interfaceDocument- Type Parameters:
T- the type to model from the underlying data.- Parameters:
type- the type to construct from the underlying key-value data.- Returns:
- the constructed instance of the given type.
-
toInstanceOf
public <T> @UnknownNullability T toInstanceOf(@NonNull @NonNull io.leangen.geantyref.TypeToken<T> type) Converts the underlying key-value mappings to an instance of the given type. Note that the type must represent an object and cannot be of any other type (like an array or primitive type).- Specified by:
toInstanceOfin interfaceDocument- Type Parameters:
T- the type to model from the underlying data.- Parameters:
type- the type to construct from the underlying key-value data.- Returns:
- the constructed instance of the given type.
-
readObject
public <T> @UnknownNullability T readObject(@NonNull @NonNull String key, @NonNull @NonNull Type type, @Nullable T def) Reads the associated value of the given key and converts it to the given type model. Note that this method is able to read all kinds of types, including arrays, primitives i.a. If an object is requested and a key is missing from the underlying mapping, it is up to the implementation to handle the case. In normal cases a default (fallback) value should get used instead.- Specified by:
readObjectin interfaceDocument- Type Parameters:
T- the type to model from the underlying data.- Parameters:
key- the key of the underlying object to convert.type- the type to convert the underlying data mapping to.def- the default value to return if no mapping exists for the given key.- Returns:
- the converted underlying data or the given default value if no value is associated with the given key.
-
readObject
public <T> @UnknownNullability T readObject(@NonNull @NonNull String key, @NonNull @NonNull Class<T> type, @Nullable T def) Reads the associated value of the given key and converts it to the given type model. Note that this method is able to read all kinds of types, including arrays, primitives i.a. If an object is requested and a key is missing from the underlying mapping, it is up to the implementation to handle the case. In normal cases a default (fallback) value should get used instead.- Specified by:
readObjectin interfaceDocument- Type Parameters:
T- the type to model from the underlying data.- Parameters:
key- the key of the underlying object to convert.type- the type to convert the underlying data mapping to.def- the default value to return if no mapping exists for the given key.- Returns:
- the converted underlying data or the given default value if no value is associated with the given key.
-
readObject
public <T> @UnknownNullability T readObject(@NonNull @NonNull String key, @NonNull @NonNull io.leangen.geantyref.TypeToken<T> type, @Nullable T def) Reads the associated value of the given key and converts it to the given type model. Note that this method is able to read all kinds of types, including arrays, primitives i.a. If an object is requested and a key is missing from the underlying mapping, it is up to the implementation to handle the case. In normal cases a default (fallback) value should get used instead.- Specified by:
readObjectin interfaceDocument- Type Parameters:
T- the type to model from the underlying data.- Parameters:
key- the key of the underlying object to convert.type- the type to convert the underlying data mapping to.def- the default value to return if no mapping exists for the given key.- Returns:
- the converted underlying data or the given default value if no value is associated with the given key.
-
readDocument
public @UnknownNullability Document readDocument(@NonNull @NonNull String key, @Nullable @Nullable Document def) Reads a document of the same type from this document or returns an empty document if no value is associated with the given key. This method returns the given default value, even if the given key is explicitly associated with null.- Specified by:
readDocumentin interfaceDocument- Parameters:
key- the key of the underlying document to read.def- the default value to return if the no mapping for the given key exists.- Returns:
- a deserialized document of the same type as this document, or the given default value if no mapping exists.
-
readMutableDocument
public @UnknownNullability Document.Mutable readMutableDocument(@NonNull @NonNull String key, @Nullable Document.Mutable def) Reads a mutable document of the same type from this document or returns an empty document if no value is associated with the given key. This method returns the given default value, even if the given key is explicitly associated with null.- Specified by:
readMutableDocumentin interfaceDocument- Parameters:
key- the key of the underlying document to read.def- the default value to return if the no mapping for the given key exists.- Returns:
- a deserialized document of the same type as this document, or the given default value if no mapping exists.
-
getByte
Reads a byte that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned. -
getShort
Reads a short that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned. -
getInt
Reads an int that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned. -
getLong
Reads a long that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned. -
getFloat
Reads a float that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned. -
getDouble
Reads a double that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned. -
getBoolean
Reads a boolean that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a number then the given default value is returned.- Specified by:
getBooleanin interfaceDocument- Parameters:
key- the key of the boolean to read.def- the default value to return if no mapping exists.- Returns:
- the boolean value associated with the given key or the given default value if no mapping exists.
-
getString
public @UnknownNullability String getString(@NonNull @NonNull String key, @Nullable @Nullable String def) Reads a string that is associated with the given key from this document. If no value is associated with the given key or the associated value is not a string then the given default value is returned. -
getPrimitiveElement
@Nullable private @Nullable com.google.gson.JsonPrimitive getPrimitiveElement(@NonNull @NonNull String key) Internal helper method to get a json element as JsonPrimitive if it is one, else returns null. This method is purely to make the code in the methods that return primitive values more readable and should not be used externally.- Parameters:
key- the key of the json primitive to get.- Returns:
- the json primitive instance of the json element associated with the key, null if not a primitive entry.
- Throws:
NullPointerException- if the given key is null.
-
getElementSafe
Internal helper method to safely read a json element from the underlying json object. In comparison to the JsonObject.get(String) method, this method returns the jvm-static JsonNull instance if no element with the given key exists.- Parameters:
key- the key to get the json element of.- Returns:
- the json element associated with the key, or the jvm-static JsonNull instance if no mapping exists.
- Throws:
NullPointerException- if the given key is null.
-
writeTo
Writes this document with the given style into a file at the given path. If one of the parent directory does not exist it gets created as well. Not every custom serialisation style is supported, but at least the standard styles must be supported. -
writeTo
public void writeTo(@NonNull @NonNull OutputStream stream, @NonNull @NonNull SerialisationStyle style) Writes this document with the given style to the given output stream. Not every custom serialisation style is supported, but at least the standard styles must be supported. -
writeTo
public void writeTo(@NonNull @NonNull Appendable appendable, @NonNull @NonNull SerialisationStyle style) Appends this document with the given style to the given appendable. Not every custom serialisation style is supported, but at least the standard styles must be supported. -
writeTo
Writes this document with the given style as a string to the given data buffer. Not every custom serialisation style is supported, but at least the standard styles must be supported. -
serializeToString
Serializes this document to a string with the given style. Not every custom serialisation style is supported, but at least the standard styles must be supported.- Specified by:
serializeToStringin interfaceDocument- Parameters:
style- the serialization style to use when writing the document.- Returns:
- a serialized string with the given style based on this document.
-
resolveSerialisationGsonInstance
@NonNull private @NonNull com.google.gson.Gson resolveSerialisationGsonInstance(@NonNull @NonNull SerialisationStyle style) Resolves the gson instance to use to serialize this document, throwing an exception if the given serialisation style is not supported.- Parameters:
style- the requested serialisation style.- Returns:
- the gson instance to use to serialize this document with the given style.
- Throws:
NullPointerException- if the given style is null.UnsupportedOperationException- if the given style is not supported.
-
propertyHolder
Get the underlying document that all read and write operations are delegated to.- Specified by:
propertyHolderin interfaceDocPropertyHolder- Returns:
- the underlying document.
-
equals
Ensures that the given object is a document of the same type and that all members of this document are present in the given other document. This method will not take entry order into account. -
hashCode
public int hashCode() -
toString
Returns this document compact serialised. This method should only be used for debug reasons, as an api user you should prefer usingDocument.serializeToString(SerialisationStyle)with theCOMPACTstandard serialisation style instead. -
writeObject
Writes this document in a compact way to the given output stream. This method is part of the java serialisation api.- Parameters:
out- the target stream to write the content of this document to.- Throws:
IOException- if an i/o error occurs while writing the content.DocumentSerialisationException- if an exception occurs serialising the document.
-
readObject
Reads a json object from the given stream and copies all it's members into this document. This method does not take into account whether a key of the deserialized object is already present in this document. This method is part of the java serialisation api.- Parameters:
in- the stream to read the json content from.- Throws:
IOException- if an i/o error occurs while reading the document content.IllegalArgumentException- if the decoded json element from the stream is not a json object.
-