Class Document

java.lang.Object
com.datastax.astra.client.model.Document
All Implemented Interfaces:
Serializable, Map<String,Object>
Direct Known Subclasses:
Filter, Update

public class Document extends Object implements Map<String,Object>, Serializable
Represents a document without schema constraints as a Map<String, Object>.(key/value)
See Also:
  • Field Details

    • documentMap

      public transient Map<String,Object> documentMap
      Data to be used in the document.
  • Constructor Details

    • Document

      public Document()
      Default Constructor.
    • Document

      public Document(Object id)
      Default Constructor.
      Parameters:
      id - provide the unique identifier.
    • Document

      public Document(Map<String,?> map)
      Creates a Document instance initialized with the given map.
      Parameters:
      map - initial map
  • Method Details

    • create

      public static Document create(Object id)
      Create a document with an id.
      Parameters:
      id - document identifier
      Returns:
      instance of document
    • create

      public static Document create()
      Create a document with no attributes.
      Returns:
      instance of document
    • map

      public <T> T map(Class<T> clazz)
      Marshall as a document if needed.
      Type Parameters:
      T - current type
      Parameters:
      clazz - target class
      Returns:
      instance of pojo
    • parse

      public static Document parse(String json)
      Parses a string in MongoDB Extended JSON format to a Document
      Parameters:
      json - the JSON string
      Returns:
      the document
    • append

      public Document append(String key, Object value)
      Put the given key/value pair into this Document and return this. Useful for chaining puts in a single expression, e.g.
       doc.append("a", 1).append("b", 2)}
       
      Parameters:
      key - key
      value - value
      Returns:
      this
    • appendIfNotNull

      public Document appendIfNotNull(String key, Object value)
      Put the given key/value pair into this Document and return this only if the value is not null
       doc.append("a", 1).append("b", 2)}
       
      Parameters:
      key - key
      value - value
      Returns:
      this
    • get

      public <T> T get(@NonNull @NonNull String key, @NonNull @NonNull Class<T> clazz)
      Gets the value of the given key, casting it to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same. So to get the value of a key that is of type String, you would write String name = doc.get("name", String.class) instead of String name = (String) doc.get("x") .
      Type Parameters:
      T - the type of the class
      Parameters:
      key - the key
      clazz - the non-null class to cast the value to
      Returns:
      the value of the given key, or null if the instance does not contain this key.
      Throws:
      ClassCastException - if the value of the given key is not of type T
    • getId

      public <T> T getId(@NonNull @NonNull Class<T> clazz)
      Access the document identifier
      Type Parameters:
      T - type of id
      Parameters:
      clazz - can be different type
      Returns:
      value for object
    • id

      public <T> Document id(T id)
      Set value for the identifier.
      Type Parameters:
      T - type of id
      Parameters:
      id - id value
      Returns:
      self reference
    • vectorize

      public Document vectorize(String text)
      Add a vectorize attribute to the document.
      Parameters:
      text - value for the vectorize attribute
      Returns:
      self reference
    • getVectorize

      public Optional<String> getVectorize()
      Access attribute with vectorize name if any.
      Returns:
      value for vectorize
    • getVector

      public Optional<float[]> getVector()
      Get value for vector.
      Returns:
      vector list
    • vector

      public Document vector(float[] vector)
      Set the vector value.
      Parameters:
      vector - vector value
      Returns:
      self reference
    • getSimilarity

      public Optional<Double> getSimilarity()
      Get value for similarity.
      Returns:
      vector list
    • getInteger

      public Integer getInteger(String key)
      Gets the value of the given key as an Integer.
      Parameters:
      key - the key
      Returns:
      the value as an integer, which may be null
      Throws:
      ClassCastException - if the value is not an integer
    • getLong

      public Long getLong(String key)
      Gets the value of the given key as a Long.
      Parameters:
      key - the key
      Returns:
      the value as a long, which may be null
      Throws:
      ClassCastException - if the value is not a long
    • getDouble

      public Double getDouble(String key)
      Gets the value of the given key as a Double.
      Parameters:
      key - the key
      Returns:
      the value as a double, which may be null
      Throws:
      ClassCastException - if the value is not a double
    • getString

      public String getString(String key)
      Gets the value of the given key as a String.
      Parameters:
      key - the key
      Returns:
      the value as a String, which may be null
      Throws:
      ClassCastException - if the value is not a String
    • getBoolean

      public Boolean getBoolean(String key)
      Gets the value of the given key as a Boolean.
      Parameters:
      key - the key
      Returns:
      the value as a Boolean, which may be null
      Throws:
      ClassCastException - if the value is not a boolean
    • getDate

      public Date getDate(Object key)
      Gets the value of the given key as a Date.
      Parameters:
      key - the key
      Returns:
      the value as a Date, which may be null
      Throws:
      ClassCastException - if the value is not a Date
    • getArray

      public <K> K[] getArray(String k, Class<K> itemClass)
      Return an Array of items.
      Type Parameters:
      K - type of item
      Parameters:
      k - key
      itemClass - expected class
      Returns:
      list of items
    • getObjectId

      public ObjectId getObjectId(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getUUID

      public UUID getUUID(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getFloat

      public Float getFloat(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getShort

      public Short getShort(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getByte

      public Byte getByte(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getCharacter

      public Character getCharacter(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getDate

      public Date getDate(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getCalendar

      public Calendar getCalendar(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getInstant

      public Instant getInstant(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getList

      public <T> List<T> getList(@NonNull @NonNull String key, @NonNull @NonNull Class<T> clazz)
      Gets the list value of the given key, casting the list elements to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same.
      Type Parameters:
      T - the type of the class
      Parameters:
      key - the key
      clazz - the non-null class to cast the list value to
      Returns:
      the list value of the given key, or null if the instance does not contain this key.
      Throws:
      ClassCastException - if the elements in the list value of the given key is not of type T or the value is not a list
      Since:
      3.10
    • getList

      public <T> List<T> getList(String key, Class<T> clazz, List<T> defaultValue)
      Gets the list value of the given key, casting the list elements to Class<T> or returning the default list value if null. This is useful to avoid having casts in client code, though the effect is the same.
      Type Parameters:
      T - the type of the class
      Parameters:
      key - the key
      clazz - the non-null class to cast the list value to
      defaultValue - what to return if the value is null
      Returns:
      the list value of the given key, or the default list value if the instance does not contain this key.
      Throws:
      ClassCastException - if the value of the given key is not of type T
      Since:
      3.10
    • toString

      public String toString()
      Serialization with Jackson.
      Overrides:
      toString in class Object
      Returns:
      json string
    • toJson

      public String toJson()
      Serialization with Jackson.
      Returns:
      json string
    • size

      public int size()
      Specified by:
      size in interface Map<String,Object>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,Object>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<String,Object>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,Object>
    • get

      public Object get(Object key)
      Specified by:
      get in interface Map<String,Object>
    • put

      public Object put(String key, Object value)
      Specified by:
      put in interface Map<String,Object>
    • remove

      public Object remove(Object key)
      Specified by:
      remove in interface Map<String,Object>
    • putAll

      public void putAll(Map<? extends String,?> map)
      Specified by:
      putAll in interface Map<String,Object>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,Object>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,Object>
    • values

      public Collection<Object> values()
      Specified by:
      values in interface Map<String,Object>
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Specified by:
      entrySet in interface Map<String,Object>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<String,Object>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<String,Object>
      Overrides:
      hashCode in class Object