public class TwinCollection
extends java.util.HashMap<java.lang.String,java.lang.Object>
The TwinCollection is an extension of a HashMap
of String
and
Object
that contain individual and general versioning mechanism.
By the Twin definition, the Object
can contain types of Boolean
,
Number
, String
, Object
, or a sub-TwinCollection, but
it cannot be types defined by the user or arrays.
A TwinCollection can contain up to 5 levels of sub TwinCollections. Once the
TwinCollection is a extension of the HashMap
, both TwinCollection as well
as its sub-TwinCollections can be casted to Map of String and Object.
The collection will be represented in the rest API as a JSON in the body. It can or cannot contain the metadata (identified by the $ character at the beginning of the key.
Because of the Twin metadata, the character $ is not allowed in the entry key.
For instance, the following JSON is a valid TwinCollection with its metadata.
{
"Color":"White",
"MaxSpeed":{
"Value":500,
"NewValue":300
},
"$metadata":{
"$lastUpdated":"2017-09-21T02:07:44.238Z",
"$lastUpdatedVersion":4,
"Color":{
"$lastUpdated":"2017-09-21T02:07:44.238Z",
"$lastUpdatedVersion":4,
},
"MaxSpeed":{
"$lastUpdated":"2017-09-21T02:07:44.238Z",
"$lastUpdatedVersion":4,
"Value":{
"$lastUpdated":"2017-09-21T02:07:44.238Z",
"$lastUpdatedVersion":4
},
"NewValue":{
"$lastUpdated":"2017-09-21T02:07:44.238Z",
"$lastUpdatedVersion":4
}
}
},
"$version":4
}
This class exposes the Twin collection with or without metadata as a Map here
user can get both the value and the metadata. For instance, in the above TwinCollection,
HashMap.get(Object)
for Color will return White and the getTwinMetadata(String)
for Color will return the Object TwinMetadata that contain TwinMetadata.getLastUpdated()
that will returns the Date
for example 2017-09-21T02:07:44.238Z, TwinMetadata.getLastUpdatedBy()
that will return the String
for example testConfig, TwinMetadata.getLastUpdatedByDigest()
that will return the String
for example 637570515479675333, and TwinMetadata.getLastUpdatedVersion()
that will return the Integer
for example 4.
For the nested TwinCollection, you can do the same, for instance, the following code will return the value and metadata of the NewValue nested in MaxSpeed:
// Get the value of the MaxSpeed, which is a inner TwinCollection.
TwinCollection innerMaxSpeed = (TwinCollection) twinCollection.get("MaxSpeed");
// From the inner TwinCollection, get the value of the NewValue.
Long maxSpeedNewValue = innerMaxSpeed.get("NewValue");
// As in the root TwinCollection, the inner TwinCollection contain its own metadata.
// So, get the metadata information for the inner NewValue.
TwinMetadata maxSpeedNewValueMetadata = innerMaxSpeed.getTwinMetadata("NewValue");
Date newValueLastUpdated = maxSpeedNewValueMetadata.getLastUpdated(); //Shall contain `2017-09-21T02:07:44.238Z`
Integer newValueLastUpdatedVersion = maxSpeedNewValueMetadata.getLastUpdatedVersion(); //Shall contain `4`
Constructor and Description |
---|
TwinCollection()
Constructor
|
TwinCollection(java.util.Map<? extends java.lang.String,java.lang.Object> map)
Constructor
|
TwinCollection(TwinCollection collection)
Constructor
|
Modifier and Type | Method and Description |
---|---|
TwinMetadata |
getTwinMetadata()
Getter for the TwinCollection metadata
|
TwinMetadata |
getTwinMetadata(java.lang.String key)
Getter for the entry metadata in the TwinCollection.
|
java.lang.Integer |
getVersion()
Getter for the version.
|
java.lang.Object |
put(java.lang.String key,
java.lang.Object value)
Add a single new entry in the TwinCollection.
|
void |
putAll(java.util.Map<? extends java.lang.String,?> map)
Add all information in the provided Map to the TwinCollection.
|
void |
setVersion(java.lang.Integer version)
Setter for the version.
|
com.google.gson.JsonElement |
toJsonElement()
Serializer
|
java.lang.String |
toString()
Creates a pretty print JSON with the content of this class and subclasses.
|
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
public TwinCollection()
Creates an empty collection. Fill it with put(String, Object)
or putAll(Map)
.
public TwinCollection(java.util.Map<? extends java.lang.String,java.lang.Object> map)
Creates a new Twin collection coping the provided Map.
map
- the Map of ? extends String
and Object
with the Twin collectionpublic TwinCollection(TwinCollection collection)
Creates a new Twin collection coping the provided collection.
collection
- the Collection of ? extends String
and Object
with the Twin collectionpublic void putAll(java.util.Map<? extends java.lang.String,?> map)
Override HashMap.putAll(Map)
.
This function will add all entries in the Map to the TwinCollection. If the provided key already exists, it will replace the value by the new one. This function will not delete or change the content of the other keys in the Map.
As defined by the Twin, the value of a entry can be an inner Map. TwinCollection will accept up to 5 levels of inner Maps.
putAll
in interface java.util.Map<java.lang.String,java.lang.Object>
putAll
in class java.util.HashMap<java.lang.String,java.lang.Object>
map
- A Map
of entries to add to the TwinCollection.public java.lang.Object put(java.lang.String key, java.lang.Object value)
Override HashMap.put(String, Object)
.
This function will add a single pair key value to the TwinCollection. By the
Twin definition, the Object
can contain types of Boolean
,
Number
, String
, Object
, or up to 5 levels of
sub-TwinCollection, but it cannot be types defined by the user or arrays.
put
in interface java.util.Map<java.lang.String,java.lang.Object>
put
in class java.util.HashMap<java.lang.String,java.lang.Object>
key
- the String
that represents the key of the new entry. It cannot be null
or empty.value
- the Object
that represents the value of the new entry. It cannot be user defined type or array.Object
that correspond to the last value of this key. It will be null
if there is no previous value.public com.google.gson.JsonElement toJsonElement()
Creates a JsonElement
, which the content represents
the information in this class and its subclasses in a JSON format.
This is useful if the caller will integrate this JSON with JSON from other classes to generate a consolidated JSON.
JsonElement
with the content of this class.public final java.lang.Integer getVersion()
Integer
with the version content. It can be null
.public final void setVersion(java.lang.Integer version)
public final TwinMetadata getTwinMetadata()
TwinMetadata
of the Whole TwinCollection. It can be null
.public final TwinMetadata getTwinMetadata(java.lang.String key)
key
- the String
with the name of the entry to retrieve the metadata.TwinMetadata
ot the specific entry in the TwinCollection. It can be null
.public java.lang.String toString()
toString
in class java.util.AbstractMap<java.lang.String,java.lang.Object>
String
with the pretty print JSON.Copyright © 2023. All rights reserved.