Package convex.core.data
Class Syntax
java.lang.Object
convex.core.data.AObject
convex.core.data.ACell
convex.core.data.Syntax
- All Implemented Interfaces:
IValidated
,IWriteable
Class representing a Syntax Object.
A Syntax Object wraps:
- A Form (which may contain nested Syntax Objects)
- Metadata for the Syntax Object, which may be any arbitrary hashmap
-
Field Summary
FieldsFields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
Method Summary
Modifier and TypeMethodDescriptionstatic Syntax
Wraps a value as a Syntax Object with empty metadata.static Syntax
Wraps a value as a Syntax Object, adding the given new metadatastatic Syntax
createUnchecked
(ACell value, AHashMap<ACell, ACell> meta) int
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.int
encodeRaw
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, excluding the tag byte.int
Estimate the encoded data size for this Cell.getEnd()
getMeta()
Gets the metadata for this syntax object.getRef
(int i) Gets a numbered child Ref from within this Cell.int
Gets the number of Refs contained within this Cell.getStart()
byte
getTag()
Gets the tag byte for this cell.getType()
Gets the most specific known runtime Type for this Cell.<R> R
getValue()
Gets the value datum from this Syntax Objectboolean
Returns true if this Cell is in a canonical representation for message writing.final boolean
Returns true if this Cell represents a first class CVM Value.static Syntax
Merge metadata into a Cell, after wrapping as a Syntax ObjectMerges metadata into this syntax object, overriding existing metadatastatic Syntax
Wraps a value as a Syntax Object with empty metadata.static Syntax
Create a Syntax Object with the given value.boolean
print
(BlobBuilder bb, long limit) Prints this Object to a readable String Representation.static Syntax
Decodes a Syntax object from a Blob encodingConverts this Cell to its canonical version.static <R> R
Unwraps a Syntax Object to get the underlying value.static <R extends ACell>
RRecursively unwraps a Syntax objectupdateRefs
(IRefFunction func) Updates all Refs in this object using the given function.void
validate()
Validates the complete structure of this object.void
Validates the local structure and invariants of this cell.Replaces metadata on this Syntax Object.Removes all metadata from this Syntax ObjectMethods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, equals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, hashCode, isCompletelyEncoded, isEmbedded, mark, mark, toCVMString, toString
Methods inherited from class convex.core.data.AObject
attachEncoding, print, print
-
Field Details
-
EMPTY
-
EMPTY_META_PREFIX
-
-
Method Details
-
getType
Description copied from class:ACell
Gets the most specific known runtime Type for this Cell. -
createUnchecked
-
create
Wraps a value as a Syntax Object, adding the given new metadata- Parameters:
value
- Value to wrap in Syntax Objectmeta
- Metadata to merge, may be null- Returns:
- Syntax instance
-
create
Wraps a value as a Syntax Object with empty metadata. Does not change existing Syntax objects.- Parameters:
value
- Any CVM value- Returns:
- Syntax instance
-
of
Wraps a value as a Syntax Object with empty metadata. Does not change existing Syntax objects.- Parameters:
value
- Any value, will be converted to valid CVM type- Returns:
- Syntax instance
-
of
Create a Syntax Object with the given value. Converts to appropriate CVM type as a convenience- Parameters:
value
- Value to wrap- Returns:
- Syntax instance
-
getValue
public <R> R getValue()Gets the value datum from this Syntax Object- Type Parameters:
R
- Expected datum type from Syntax object- Returns:
- Value datum
-
getMeta
Gets the metadata for this syntax object. May be empty, but never null.- Returns:
- Metadata for this Syntax Object as a hashmap
-
getStart
-
getEnd
-
getSource
-
isCanonical
public boolean isCanonical()Description copied from class:ACell
Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).- Specified by:
isCanonical
in classACell
- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACell
Returns true if this Cell represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false, pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures- Specified by:
isCVMValue
in classACell
- Returns:
- true if the object is a CVM Value, false otherwise
-
read
Decodes a Syntax object from a Blob encoding- Parameters:
b
- Blob to read frompos
- Start position in Blob (location of tag byte)- Returns:
- New decoded instance
- Throws:
BadFormatException
- In the event of any encoding error
-
encode
public int encode(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encode
in interfaceIWriteable
- Specified by:
encode
in classACell
- Parameters:
bs
- A byte array to which to write the encodingpos
- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
public int encodeRaw(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, excluding the tag byte. -
print
Description copied from class:AObject
Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible. -
validateCell
Description copied from class:ACell
Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Specified by:
validateCell
in classACell
- Throws:
InvalidDataException
- If the Cell is invalid
-
validate
Description copied from interface:IValidated
Validates the complete structure of this object. It is necessary to ensure all child Refs are validated, so the general contract for validate is:- Call super.validate() - which will indirectly call validateCell()
- Call validate() on any contained cells in this class
- Specified by:
validate
in interfaceIValidated
- Overrides:
validate
in classACell
- Throws:
InvalidDataException
- If the data Value is invalid in any way
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteable
Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- Returns:
- The estimated size for the binary representation of this object.
-
getRefCount
public int getRefCount()Description copied from class:ACell
Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. WARNING: may not be valid id Cell is not canonical Contained Refs may be either external or embedded.- Specified by:
getRefCount
in classACell
- Returns:
- The number of Refs in this Cell
-
getRef
Description copied from class:ACell
Gets a numbered child Ref from within this Cell. WARNING: May be unreliable is cell is not canonical -
updateRefs
Description copied from class:ACell
Updates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. The implementation *should* re-attach any original encoding in order to prevent re-encoding or surplus hashing This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.- Overrides:
updateRefs
in classACell
- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
mergeMeta
Merges metadata into this syntax object, overriding existing metadata- Parameters:
additionalMetadata
- Extra metadata to merge- Returns:
- Syntax Object with updated metadata
-
mergeMeta
Merge metadata into a Cell, after wrapping as a Syntax Object- Parameters:
original
- Cell to enhance with merged metadataadditional
- Syntax Object containing additional metadata. Any value will be ignored.- Returns:
- Syntax object with merged metadata
-
withMeta
Replaces metadata on this Syntax Object. Old metadata is discarded.- Parameters:
newMetadata
- New metadata map- Returns:
- Syntax Object with updated metadata
-
withoutMeta
Removes all metadata from this Syntax Object- Returns:
- Syntax Object with empty metadata
-
unwrap
Unwraps a Syntax Object to get the underlying value. If the argument is not a Syntax object, return it unchanged (already unwrapped)- Type Parameters:
R
- Expected type of value- Parameters:
x
- Any Object, which may be a Syntax Object- Returns:
- The unwrapped value
-
unwrapAll
Recursively unwraps a Syntax object- Parameters:
maybeSyntax
- Syntax Object to unwrap- Returns:
- Unwrapped object
-
getTag
public byte getTag()Description copied from class:ACell
Gets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding. -
toCanonical
Description copied from class:ACell
Converts this Cell to its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-