Record Class StandardDocProperty<E>

java.lang.Object
java.lang.Record
eu.cloudnetservice.driver.document.property.StandardDocProperty<E>
Type Parameters:
E - the type which gets read/written by this property.
Record Components:
key - the key that is used when the member is written to/read from the underlying document.
type - the type of the member that gets wrapped by the property.
All Implemented Interfaces:
DocProperty<E>

record StandardDocProperty<E>(@NonNull String key, @NonNull Type type) extends Record implements DocProperty<E>
The standard implementation of a doc property that does no special processing of the read/written values and supports read and write operations from/to the given document.
Since:
4.0
  • Field Details

  • Constructor Details

    • StandardDocProperty

      StandardDocProperty(@NonNull @NonNull String key, @NonNull @NonNull Type type)
      Creates an instance of a StandardDocProperty record class.
      Parameters:
      key - the value for the key record component
      type - the value for the type record component
  • Method Details

    • readOnly

      public boolean readOnly()
      Get if this property only supports being read from a document but not being written to one.
      Specified by:
      readOnly in interface DocProperty<E>
      Returns:
      true if this property is read-only, false otherwise.
    • asReadOnly

      @NonNull public @NonNull DocProperty<E> asReadOnly()
      Wraps this property in a new property that makes any write operation with this property throw an exception. Read operations will be delegated to the property this method is being called on.

      If this property is already read-only the current property is returned without wrapping.

      Specified by:
      asReadOnly in interface DocProperty<E>
      Returns:
      a property that wraps this property to make it read-only.
    • withDefault

      @NonNull public @NonNull DocProperty<E> withDefault(@Nullable E def)
      Wraps this property to make read and write operations set a default value in case the given value is either absent (not appended to the document) or set to null. Read and write operations are delegated to the property this method is being called on.

      If this property already has a default and the given default is the same as current one (as defined by ==) the current property is returned without wrapping.

      Specified by:
      withDefault in interface DocProperty<E>
      Parameters:
      def - the default value to use for the property.
      Returns:
      a property that wraps this property and sets a default value during read and write operations.
    • withReadRewrite

      @NonNull public <V> @NonNull DocProperty<V> withReadRewrite(@NonNull @NonNull Function<E,V> rewriteFunction)
      Wraps this property to apply a rewrite function when reading the property from a document. The parsed type of this property is passed to the function and returned as the new property value. Null values are not passed to the function and returned without processing.

      Note: as this method only sets a function to rewrite while reading the value, the returned property will be read-only as values passed for writing cannot be converted. Use DocProperty.withReadWriteRewrite(Function, Function) to set a function that is called during writing as well.

      Specified by:
      withReadRewrite in interface DocProperty<E>
      Type Parameters:
      V - the type of value that is the result of the value convert process.
      Parameters:
      rewriteFunction - the rewrite function to call when reading the value from a document.
      Returns:
      a read-only property that wraps this property and applies the given rewrite function when reading.
    • withReadWriteRewrite

      @NonNull public <V> @NonNull DocProperty<V> withReadWriteRewrite(@NonNull @NonNull Function<E,V> readRewriteFunction, @Nullable @Nullable Function<V,E> writeRewriteFunction)
      Wraps this property to apply a rewrite function when reading and writing the property from/to a document. The parsed type of this property is passed to the function and returned/written as the new property value. Null values are not passed to the function and returned without processing.

      Note: if the call to this method only sets a function to rewrite while reading the value (setting the rewrite function for reads to null), the returned property will be read-only as values passed for writing cannot be converted.

      Specified by:
      withReadWriteRewrite in interface DocProperty<E>
      Type Parameters:
      V - the type of value that is the result of the value convert process.
      Parameters:
      readRewriteFunction - the rewrite function to call when reading the value from a document.
      writeRewriteFunction - the rewrite function to call when writing the value to a document, can be null.
      Returns:
      a property that wraps this property and applies the given rewrite function when reading or writing.
    • readFrom

      @Nullable public E readFrom(@NonNull @NonNull Document document)
      Called to read the value of this property from the given document.
      Specified by:
      readFrom in interface DocProperty<E>
      Parameters:
      document - the document to read the value from.
      Returns:
      the parsed value from the document, or null if the value is not present.
    • writeTo

      @NonNull public @NonNull DocProperty<E> writeTo(@NonNull Document.Mutable document, @Nullable E value)
      Writes the given value into the given document.
      Specified by:
      writeTo in interface DocProperty<E>
      Parameters:
      document - the document to write the value to.
      value - the value to write.
      Returns:
      the same property as used to call the method, for chaining.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • key

      @NonNull public @NonNull String key()
      Returns the value of the key record component.
      Specified by:
      key in interface DocProperty<E>
      Returns:
      the value of the key record component
    • type

      @NonNull public @NonNull Type type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component