TwinCollection 类

定义

Representation of a single Twin collection.

The TwinCollection is an extension of aHashMap  ofString  and Object  that contain individual and general versioning mechanism.

By the Twin definition, theObject  can contain types ofBoolean  , 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 theHashMap  , 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, get(Object) for Color will return White and the getTwinMetadataFinal(String key) for Color will return the Object TwinMetadata that contain getLastUpdated() that will returns theDate  for example 2017-09-21T02:07:44.238Z, getLastUpdatedBy() that will return theString  for example testConfig, getLastUpdatedByDigest() that will return theString  for example 637570515479675333, and getLastUpdatedVersion() that will return theInteger  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.getTwinMetadataFinal("NewValue");
    Date newValueLastUpdated = maxSpeedNewValueMetadata.getLastUpdated(); //Shall contain `2017-09-21T02:07:44.238Z`
    Integer newValueLastUpdatedVersion = maxSpeedNewValueMetadata.getLastUpdatedVersion(); //Shall contain `4`
    

public class TwinCollection
继承
java.lang.Object
java.util.HashMap<String, Object>
TwinCollection

构造函数

TwinCollection()

Constructor

Creates an empty collection. Fill it with putFinal(String key, Object value) or putAllFinal(Map).

TwinCollection(Map<? extends String, Object> map)

Constructor

Creates a new Twin collection coping the provided Map.

TwinCollection(TwinCollection collection)

Constructor

Creates a new Twin collection coping the provided collection.

方法

createFromRawCollection(Map<? extends String, Object> rawCollection)

Internal Constructor from raw map.

This internal constructor is used to the deserialization process.

During the deserialization process, the GSON will convert both tags and properties to a raw Map, which will includes the $version and $metadata as part of the collection. So, we need to reorganize this map using the TwinCollection format. This constructor will do that.

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

}

</code></pre></p>

getTwinMetadata()

Getter for the TwinCollection metadata

<xreftitle>Deprecated</xreftitle>

<xrefdescription>

  <p>as of Deps version 0.7.1, please use <xref uid="com.microsoft.azure.sdk.iot.deps.twin.TwinCollection.getTwinMetadataFinal()" data-throw-if-not-resolved="false" data-raw-source="getTwinMetadataFinal()"></xref></p>

</xrefdescription>

getTwinMetadata(String key)

Getter for the entry metadata in the TwinCollection.

<xreftitle>Deprecated</xreftitle>

<xrefdescription>

  <p>as of Deps version 0.7.1, please use <xref uid="com.microsoft.azure.sdk.iot.deps.twin.TwinCollection.getTwinMetadataFinal(String)" data-throw-if-not-resolved="false" data-raw-source="getTwinMetadataFinal(String)"></xref></p>

</xrefdescription>

getTwinMetadataFinal()

Getter for the TwinCollection metadata

getTwinMetadataFinal(String key)

Getter for the entry metadata in the TwinCollection.

getVersion()

Getter for the version.

<xreftitle>Deprecated</xreftitle>

<xrefdescription>

  <p>as of Deps version 0.7.1, please use <xref uid="com.microsoft.azure.sdk.iot.deps.twin.TwinCollection.getVersionFinal()" data-throw-if-not-resolved="false" data-raw-source="getVersionFinal()"></xref></p>

</xrefdescription>

getVersionFinal()

Getter for the version.

put(String key, Object value)

Add a single new entry in the TwinCollection.

OverrideHashMap.put(String, Object) 

</code> .</p>

This function will add a single pair key value to the TwinCollection. By the Twin definition, theObject 

</code> can contain types of<code>Boolean 

</code> , <code>Number 

</code> ,<code>String 

</code> ,<code>Object 

</code> , or up to 5 levels of sub-TwinCollection, but it cannot be types defined by the user or arrays.</p>

<xreftitle>Deprecated</xreftitle>

<xrefdescription>

  <p>as of Deps version 0.7.1, please use <xref uid="com.microsoft.azure.sdk.iot.deps.twin.TwinCollection.putFinal(String,Object)" data-throw-if-not-resolved="false" data-raw-source="putFinal(String, Object)"></xref></p>

</xrefdescription>

putAll(Map<? extends String, ?> map)

Add all information in the provided Map to the TwinCollection.

OverrideHashMap.putAll(Map) 

</code> .</p>

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.

<xreftitle>Deprecated</xreftitle>

<xrefdescription>

  <p>as of Deps version 0.7.1, please use <xref uid="" data-throw-if-not-resolved="false" data-raw-source="putAllFinal(Map)"></xref></p>

</xrefdescription>

putAllFinal(Map<? extends String, ?> map)

Add all information in the provided Map to the TwinCollection.

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.

putFinal(String key, Object value)

Add a single new entry in the TwinCollection.

This function will add a single pair key value to the TwinCollection. By the Twin definition, theObject 

</code> can contain types of<code>Boolean 

</code> , <code>Number 

</code> ,<code>String 

</code> ,<code>Object 

</code> , or up to 5 levels of sub-TwinCollection, but it cannot be types defined by the user or arrays.</p>

toJsonElement()

Serializer

Creates aJsonElement 

</code> , which the content represents the information in this class and its subclasses in a JSON format.</p>

This is useful if the caller will integrate this JSON with JSON from other classes to generate a consolidated JSON.

toJsonElementWithMetadata()

Serializer with metadata.

Return a JsonElement with the full content of this class, including the metadata.

toString()

Creates a pretty print JSON with the content of this class and subclasses.

适用于