Record Class RewritingDocProperty<I,O>

java.lang.Object
java.lang.Record
eu.cloudnetservice.driver.document.property.RewritingDocProperty<I,O>
Type Parameters:
I - the type of the input property the actual read/write calls are delegated to.
O - the output type after passing the values through the convert functions.
Record Components:
upstream - the upstream doc property to delegate all method calls to.
readRewriteFunction - the function to apply when a non-null value was read from a document.
writeRewriteFunction - the function to apply when a non-null value is written to a document, can be null.
All Implemented Interfaces:
DocProperty<O>

record RewritingDocProperty<I,O>(@NonNull DocProperty<I> upstream, @NonNull Function<I,O> readRewriteFunction, @Nullable Function<O,I> writeRewriteFunction) extends Record implements DocProperty<O>
A doc property implementation that calls the given functions when reading/writing the value from a document to convert it into a different value (for example when parsing).
Since:
4.0
  • Field Details

  • Constructor Details

  • 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<I>
      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<I>
      Returns:
      true if this property is read-only, false otherwise.
    • asReadOnly

      @NonNull public @NonNull DocProperty<O> 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<I>
      Returns:
      a property that wraps this property to make it read-only.
    • withDefault

      @NonNull public @NonNull DocProperty<O> withDefault(@Nullable O 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<I>
      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<O,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<I>
      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<O,V> readRewriteFunction, @Nullable @Nullable Function<V,O> 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<I>
      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 O readFrom(@NonNull @NonNull Document document)
      Called to read the value of this property from the given document.
      Specified by:
      readFrom in interface DocProperty<I>
      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<O> writeTo(@NonNull Document.Mutable document, @Nullable O value)
      Writes the given value into the given document.
      Specified by:
      writeTo in interface DocProperty<I>
      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.
    • upstream

      @NonNull public @NonNull DocProperty<I> upstream()
      Returns the value of the upstream record component.
      Returns:
      the value of the upstream record component
    • readRewriteFunction

      @NonNull public @NonNull Function<I,O> readRewriteFunction()
      Returns the value of the readRewriteFunction record component.
      Returns:
      the value of the readRewriteFunction record component
    • writeRewriteFunction

      @Nullable public @Nullable Function<O,I> writeRewriteFunction()
      Returns the value of the writeRewriteFunction record component.
      Returns:
      the value of the writeRewriteFunction record component