Record Class ReadOnlyDocProperty<E>

java.lang.Object
java.lang.Record
eu.cloudnetservice.driver.document.property.ReadOnlyDocProperty<E>
Type Parameters:
E - the type which gets read/written by this property.
Record Components:
downstream - the downstream doc property to delegate all methods (except writing) to.
All Implemented Interfaces:
DocProperty<E>

record ReadOnlyDocProperty<E>(@NonNull DocProperty<E> downstream) extends Record implements DocProperty<E>
A doc property implementation that only allows reading the value from a document but not setting the value. All methods that wrap this property are implemented in a way that the read-only flag is not lost in a call chain.
Since:
4.0
  • Field Details

  • Constructor Details

    • ReadOnlyDocProperty

      ReadOnlyDocProperty(@NonNull @NonNull DocProperty<E> downstream)
      Creates an instance of a ReadOnlyDocProperty record class.
      Parameters:
      downstream - the value for the downstream record component
  • Method Details

    • key

      @NonNull public @NonNull String key()
      Get the key of the member that is read from/written to a document.
      Specified by:
      key in interface DocProperty<E>
      Returns:
      the key of the member that is read from/written to a document.
    • 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.
    • downstream

      @NonNull public @NonNull DocProperty<E> downstream()
      Returns the value of the downstream record component.
      Returns:
      the value of the downstream record component