Class CBORMap
- All Implemented Interfaces:
Cloneable
,Comparable<CBORObject>
{}
(map) objects.
Note: to maintain Deterministic Encoding map keys are automatically sorted during insertion.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Support interface for dynamic CBOR generation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(CBORObject key) Check CBOR map for key presence.get
(CBORObject key) Get mapped CBOR object.getConditionally
(CBORObject key, CBORObject defaultObject) Get mapped CBOR object conditionally.getKeys()
Enumerate all keys in the CBOR map.Merge CBOR map.remove
(CBORObject key) Remove mapped CBOR object.set
(CBORObject key, CBORObject object) Set mapped CBOR object.setDynamic
(CBORMap.Dynamic dynamic) Set CBOR data using an external (dynamic) interface.setSortingMode
(boolean preSortedKeys) Set sorting mode for the CBOR map.int
size()
Get size of the CBOR map.update
(CBORObject key, CBORObject object, boolean existing) Update mapped CBOR object.Methods inherited from class org.webpki.cbor.CBORObject
checkForUnread, clone, compareTo, encode, equals, getArray, getBigInteger, getBoolean, getBytes, getDateTime, getEpochTime, getFloat16, getFloat32, getFloat64, getInt16, getInt32, getInt64, getInt8, getMap, getSimple, getString, getTag, getUint16, getUint32, getUint64, getUint8, hashCode, isNull, scan, toDiagnosticNotation, toString
-
Constructor Details
-
CBORMap
public CBORMap()Creates an empty CBOR{}
(map).Equivalent to
CBORMap().setSortingMode(false)
.
-
-
Method Details
-
size
public int size()Get size of the CBOR map.- Returns:
- The number of entries (keys) in the map
-
set
Set mapped CBOR object.If
key
is already present, aCBORException
is thrown.Note that this implementation presumes that
Also seekey
objects are immutable. To createkey
objects of arbitrary complexity,key
objects must either be created inline (using chaining), or be supplied as preset variables.update(CBORObject, CBORObject, boolean)
.- Parameters:
key
- Key (name)object
- Object (value)- Returns:
this
- Throws:
CBORException
-
setDynamic
Set CBOR data using an external (dynamic) interface.Sample using a construct suitable for chained writing:
setDynamic((wr) -> optionalString == null ? wr : wr.set(KEY, new CBORString(optionalString)));
- Parameters:
dynamic
- Interface (usually Lambda)- Returns:
this
- Throws:
CBORException
-
merge
Merge CBOR map.Note that a duplicate key causes a
CBORException
to be thrown.- Parameters:
map
- Map to be merged into the current mao- Returns:
this
- Throws:
CBORException
-
setSortingMode
Set sorting mode for the CBOR map.This method provides an opportunity using keys that are presorted (in lexicographic order), which in maps with many keys can offer performance improvements.
Note that
setSortingMode
is only effective during encoding. ThesetSortingMode
method may be called multiple times, permitting certain keys to be automatically sorted and others to be provided in a presorted fashion. See alsoCBORDecoder(InputStream, int, int)
.- Parameters:
preSortedKeys
- Iftrue
, keys must be sorted. If a key is not properly sorted when callingset(CBORObject, CBORObject)
, aCBORException
is thrown.- Returns:
this
-
get
Get mapped CBOR object.If
key
is present, the associatedobject
is returned, else aCBORException
is thrown.- Parameters:
key
- Key (name)- Returns:
object
- Throws:
CBORException
-
getConditionally
Get mapped CBOR object conditionally.If
key
is present, the associatedobject
is returned, elsedefaultObject
is returned. Note:defaultObject
may benull
.- Parameters:
key
- Key (name)defaultObject
- Default object (value)- Returns:
object
ordefaultObject
-
containsKey
Check CBOR map for key presence.- Parameters:
key
- Key (name)- Returns:
true
if the key is present
-
remove
Remove mapped CBOR object.If
key
is present, thekey
and associatedobject
are removed, else aCBORException
is thrown.- Parameters:
key
- Key (name)- Returns:
- Removed object (value)
- Throws:
CBORException
-
update
Update mapped CBOR object.If
existing
istrue
,key
must already be present, else aCBORException
is thrown.If
existing
isfalse
, a map entry forkey
will be created if not already present.- Parameters:
key
- Key (name)object
- New object (value)existing
- Flag- Returns:
- Previous
object
. May benull
. - Throws:
CBORException
-
getKeys
Enumerate all keys in the CBOR map.Note: the keys are returned in proper sort order.
- Returns:
- Array of keys
-