Class MapEntry<K extends ACell,​V extends ACell>

Type Parameters:
K - The type of keys
V - The type of values
All Implemented Interfaces:
IAssociative<CVMLong,​ACell>, IValidated, IWriteable, Comparable<MapEntry<K,​V>>, Iterable<ACell>, Collection<ACell>, List<ACell>, Map.Entry<K,​V>

public class MapEntry<K extends ACell,​V extends ACell> extends AMapEntry<K,​V> implements Comparable<MapEntry<K,​V>>
Map.Entry implementation for persistent maps. This is primarily intended as an efficient implementation class for handling entries in Convex maps, and also to support the Java Map.Entry interface for compatibility and developer convenience. From a CVM perspective, a MapEntry is just a regular 2 element Vector. As such, MapEntry is *not* canonical and getting the canonical form of a MapEntry requires converting to a Vector Contains exactly 2 Refs, one for key and one for value Implements Comparable using the Hash value of keys.
  • Method Details

    • getType

      public AType getType()
      Description copied from class: ACell
      Gets the most specific known runtime Type for this Cell.
      Overrides:
      getType in class AVector<ACell>
      Returns:
      The Type of this Call
    • createRef

      public static <K extends ACell,​ V extends ACell> MapEntry<K,​V> createRef(Ref<? extends K> keyRef, Ref<? extends V> valueRef)
    • create

      public static <K extends ACell,​ V extends ACell> MapEntry<K,​V> create(K key, V value)
      Creates a new MapEntry with the provided key and value
      Type Parameters:
      K - Type of Key
      V - Type of value
      Parameters:
      key - Key to use for MapEntry
      value - Value to use for MapEntry
      Returns:
      New MapEntry instance
    • of

      public static <K extends ACell,​ V extends ACell> MapEntry<K,​V> of(Object key, Object value)
      Create a map entry, converting key and value to correct CVM types.
      Type Parameters:
      K - Type of Keys
      V - Type of Values
      Parameters:
      key - Key to use for map entry
      value - Value to use for map entry
      Returns:
      New MapEntry
    • withValue

      public MapEntry<K,​V> withValue(V value)
      Description copied from class: AMapEntry
      Create a new MapEntry with an updated value. Shares old key. Returns the same MapEntry if unchanged
      Specified by:
      withValue in class AMapEntry<K extends ACell,​V extends ACell>
      Parameters:
      value - Value to update
      Returns:
    • assoc

      public <R extends ACell> AVector<R> assoc(long i, R a)
      Description copied from class: ASequence
      Updates a value at the given position in the sequence.
      Specified by:
      assoc in class AVector<ACell>
      Parameters:
      i - Index of element to update
      a - New element value
      Returns:
      Updated sequence, or null if index is out of range
    • withKey

      protected MapEntry<K,​V> withKey(K key)
      Description copied from class: AMapEntry
      Create a new MapEntry with an updated key. Shares old value. Returns the same MapEntry if unchanged
      Specified by:
      withKey in class AMapEntry<K extends ACell,​V extends ACell>
      Parameters:
      key - Key to update
      Returns:
    • getKey

      public K getKey()
      Specified by:
      getKey in interface Map.Entry<K extends ACell,​V extends ACell>
      Specified by:
      getKey in class AMapEntry<K extends ACell,​V extends ACell>
    • map

      public <R extends ACell> AVector<R> map(Function<? super ACell,​? extends R> mapper)
      Description copied from class: ACollection
      Maps a function over a collection, applying it to each element in turn.
      Specified by:
      map in class AVector<ACell>
      Type Parameters:
      R - Type of element in resulting collection
      Parameters:
      mapper - Function to map over collection
      Returns:
      Collection after function applied to each element
    • reduce

      public <R> R reduce(BiFunction<? super R,​? super ACell,​? extends R> func, R value)
      Specified by:
      reduce in class AVector<ACell>
    • copyToArray

      protected <R> void copyToArray(R[] arr, int offset)
      Description copied from class: ACollection
      Copies the elements of this collection in order to an array at the specified offset
      Specified by:
      copyToArray in class ACollection<ACell>
      Type Parameters:
      R - Type of array elements required
    • getKeyHash

      public Hash getKeyHash()
      Gets the Hash of the key for this MapEntry
      Returns:
      the Hash of the Key
    • getValue

      public V getValue()
      Specified by:
      getValue in interface Map.Entry<K extends ACell,​V extends ACell>
      Specified by:
      getValue in class AMapEntry<K extends ACell,​V extends ACell>
    • getKeyRef

      public Ref<K> getKeyRef()
    • getValueRef

      public Ref<V> getValueRef()
    • read

      public static <K extends ACell,​ V extends ACell> MapEntry<K,​V> read(ByteBuffer bb) throws BadFormatException
      Reads a MapEntry from a ByteBuffer. Assumes Tag already handled.
      Parameters:
      bb - ByteBuffer to read from
      Returns:
      MapEntry instance
      Throws:
      BadFormatException - If encoding is invalid
    • readCompressed

      public static <K extends ACell,​ V extends ACell> MapEntry<K,​V> readCompressed(ByteBuffer bb) throws BadFormatException
      Reads a MapEntry or null from a ByteBuffer. Assumes no Tag.
      Parameters:
      bb - ByteBuffer to read from
      Returns:
      MapEntry instance, or null
      Throws:
      BadFormatException - If encoding is invalid
    • compareTo

      public int compareTo(MapEntry<K,​V> o)
      Specified by:
      compareTo in interface Comparable<K extends ACell>
    • 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. Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class AMapEntry<K extends ACell,​V extends ACell>
      Returns:
      The number of Refs in this Cell
    • getRef

      public <R extends ACell> Ref<R> getRef(int i)
      Description copied from class: ACell
      Gets a numbered child Ref from within this Cell.
      Specified by:
      getRef in class AMapEntry<K extends ACell,​V extends ACell>
      Type Parameters:
      R - Type of referenced Cell
      Parameters:
      i - Index of ref to get
      Returns:
      The Ref at the specified index
    • updateRefs

      public MapEntry<K,​V> updateRefs(IRefFunction func)
      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. 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.
      Specified by:
      updateRefs in class AVector<ACell>
      Parameters:
      func - Ref update function
      Returns:
      Cell with updated Refs
    • equals

      public boolean equals(ACell o)
      Description copied from class: ACell
      Checks for equality with another object. In general, data objects should be considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if offer have a more efficient equals implementation. MUST NOT require reads from Store.
      Overrides:
      equals in class ACell
      Parameters:
      o - Cell to compare with. May be null??
      Returns:
      True if this cell is equal to the other object
    • equals

      public boolean equals(MapEntry<K,​V> b)
    • keyEquals

      public boolean keyEquals(MapEntry<K,​V> b)
      Checks if the keys of two map entries are equal
      Parameters:
      b - MapEntry to compare with this MapEntry
      Returns:
      true if this entry's key equals that of the other entry, false otherwise.
    • toVector

      public <R extends ACell> AVector<R> toVector()
      Description copied from class: ACollection
      Converts this collection to a canonical vector of elements
      Specified by:
      toVector in class ACollection<ACell>
      Returns:
      This collection coerced to a vector
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<K extends ACell>
      Specified by:
      contains in interface List<K extends ACell>
      Overrides:
      contains in class ASequence<ACell>
    • get

      public ACell get(long i)
      Description copied from class: AVector
      Gets the element at the specified index in this vector
      Specified by:
      get in class AMapEntry<K extends ACell,​V extends ACell>
      Parameters:
      i - The index of the element to get
      Returns:
      The element value at the specified index
    • getElementRef

      public Ref<ACell> getElementRef(long i)
      Description copied from class: ASequence
      Gets the element Ref at the specified index
      Specified by:
      getElementRef in class ASequence<ACell>
      Parameters:
      i - Index of element to get
      Returns:
      Ref to element at specified index
    • 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 interface IWriteable
      Specified by:
      encode in class AMapEntry<K extends ACell,​V extends ACell>
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRaw

      public int encodeRaw(byte[] bs, int pos)
      Writes the raw MapEntry content. Puts the key and value Refs onto the given ByteBuffer
      Specified by:
      encodeRaw in class AVector<ACell>
      Parameters:
      bs - Byte array to write to
      pos - The offset into the byte array
      Returns:
      Updated position after writing
    • encodeCompressed

      public static int encodeCompressed(MapEntry<?,​?> me, byte[] bs, int pos)
      Writes a MapEntry or null content in compressed format (no count). Useful for embedding an optional MapEntry inside a larger Encoding
      Parameters:
      me - MapEntry to encode
      bs - Byte array to write to
      pos - Starting position for encoding in byte array
      Returns:
      Updated position after writing
    • 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 class ACell
      Returns:
      true if the object is a CVM Value, false otherwise
    • 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 likely to contain the entire object when represented in binary format, including the tag byte.
      Specified by:
      estimatedEncodingSize in interface IWriteable
      Returns:
      The estimated size for the binary representation of this object.
    • visitElementRefs

      public void visitElementRefs(Consumer<Ref<ACell>> f)
      Description copied from class: ASequence
      Visits all elements in this sequence, calling the specified consumer for each.
      Specified by:
      visitElementRefs in class ASequence<ACell>
      Parameters:
      f - Function to call for each element
    • concat

      public <R extends ACell> AVector<R> concat(ASequence<R> b)
      Description copied from class: ASequence
      Concatenates the elements from another sequence to the end of this sequence. Potentially O(n) in size of resulting sequence
      Specified by:
      concat in class AVector<ACell>
      Parameters:
      b - A sequence of values to concatenate.
      Returns:
      The concatenated sequence, of the same type as this sequence.
    • subVector

      public <R extends ACell> AVector<R> subVector(long start, long length)
      Description copied from class: ASequence
      Gets a vector containing the specified subset of this sequence.
      Specified by:
      subVector in class ASequence<ACell>
      Parameters:
      start - Start index of sub vector
      length - Length of sub vector to produce
      Returns:
      Sub-vector of this sequence
    • validate

      public void validate() throws InvalidDataException
      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:
      1. Call super.validate() - which will indirectly call validateCell()
      2. Call validate() on any contained cells in this class
      Specified by:
      validate in interface IValidated
      Overrides:
      validate in class ACell
      Throws:
      InvalidDataException - If the data Valie is invalid in any way
    • validateCell

      public void validateCell() throws InvalidDataException
      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 class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • getTag

      public byte getTag()
      Description copied from class: ACell
      Gets the tag byte for this cell. The tag byte is always written as the first byte of the Cell's Encoding
      Overrides:
      getTag in class AVector<ACell>
      Returns:
      Tag byte for this Cell
    • convertOrNull

      public static MapEntry convertOrNull(AVector v)
    • isCanonical

      public boolean isCanonical()
      Description copied from class: AVector
      Returns true if this vector is in canonical format, i.e. suitable as top-level serialised representation of a vector.
      Specified by:
      isCanonical in class AMapEntry<K extends ACell,​V extends ACell>
      Returns:
      true if the vector is in canonical format, false otherwise
    • toCanonical

      public ACell toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Returns this Cell if already canonical, may be O(n) in size of value otherwise.
      Specified by:
      toCanonical in class ACell
      Returns:
      Canonical version of Cell