Class DynamicOps<T>

java.lang.Object
com.github.darksoulq.abyssallib.common.serialization.DynamicOps<T>
Type Parameters:
T - the serialized value type used by the format
Direct Known Subclasses:
ByteOps, JsonOps, NbtOps, StringOps, YamlOps

public abstract class DynamicOps<T> extends Object
Provides format-specific operations for creating, reading, and transforming serialized data structures.

Implementations define how primitive values, lists, and maps are represented in a particular serialization format such as JSON, YAML, NBT, or binary data. This abstraction allows Codec implementations to operate independently of the underlying serialized representation.

  • Constructor Details

    • DynamicOps

      public DynamicOps()
  • Method Details

    • compressMaps

      public boolean compressMaps()
      Indicates whether this format prefers compressed map representations.
      Returns:
      true if compressed map encoding should be used, otherwise false
    • empty

      public abstract T empty()
      Returns the format-specific representation of an empty value.
      Returns:
      the empty value representation
    • emptyMap

      public T emptyMap()
      Creates an empty map value.
      Returns:
      an empty serialized map
    • emptyList

      public T emptyList()
      Creates an empty list value.
      Returns:
      an empty serialized list
    • createString

      public abstract T createString(String value)
      Creates a serialized string value.
      Parameters:
      value - the string to serialize
      Returns:
      the serialized string
    • createByte

      public abstract T createByte(byte value)
      Creates a serialized byte value.
      Parameters:
      value - the byte to serialize
      Returns:
      the serialized byte
    • createShort

      public abstract T createShort(short value)
      Creates a serialized short value.
      Parameters:
      value - the short to serialize
      Returns:
      the serialized short
    • createInt

      public abstract T createInt(int value)
      Creates a serialized integer value.
      Parameters:
      value - the integer to serialize
      Returns:
      the serialized integer
    • createLong

      public abstract T createLong(long value)
      Creates a serialized long value.
      Parameters:
      value - the long to serialize
      Returns:
      the serialized long
    • createFloat

      public abstract T createFloat(float value)
      Creates a serialized float value.
      Parameters:
      value - the float to serialize
      Returns:
      the serialized float
    • createDouble

      public abstract T createDouble(double value)
      Creates a serialized double value.
      Parameters:
      value - the double to serialize
      Returns:
      the serialized double
    • createBoolean

      public abstract T createBoolean(boolean value)
      Creates a serialized boolean value.
      Parameters:
      value - the boolean to serialize
      Returns:
      the serialized boolean
    • createList

      public abstract T createList(List<T> elements)
      Creates a serialized list.
      Parameters:
      elements - the serialized list elements
      Returns:
      the serialized list
    • createMap

      public abstract T createMap(Map<T,T> map)
      Creates a serialized map.
      Parameters:
      map - the serialized key-value pairs
      Returns:
      the serialized map
    • getStringValue

      public abstract Optional<String> getStringValue(T input)
      Attempts to read a string value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded string, if present
    • getNumberValue

      public abstract Optional<Number> getNumberValue(T input)
      Attempts to read a numeric value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded number, if present
    • getIntValue

      public abstract Optional<Integer> getIntValue(T input)
      Attempts to read an integer value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded integer, if present
    • getLongValue

      public abstract Optional<Long> getLongValue(T input)
      Attempts to read a long value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded long, if present
    • getFloatValue

      public abstract Optional<Float> getFloatValue(T input)
      Attempts to read a float value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded float, if present
    • getDoubleValue

      public abstract Optional<Double> getDoubleValue(T input)
      Attempts to read a double value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded double, if present
    • getBooleanValue

      public abstract Optional<Boolean> getBooleanValue(T input)
      Attempts to read a boolean value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded boolean, if present
    • getList

      public abstract Optional<List<T>> getList(T input)
      Attempts to read a list value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded list, if the value is a list
    • getMap

      public abstract Optional<Map<T,T>> getMap(T input)
      Attempts to read a map value.
      Parameters:
      input - the serialized value
      Returns:
      the decoded map, if the value is a map
    • getKeys

      public abstract Optional<Iterable<String>> getKeys(T input)
      Returns the string keys contained in a map value.
      Parameters:
      input - the serialized value
      Returns:
      the map keys, if the value is a map
    • size

      public abstract OptionalInt size(T input)
      Returns the number of elements contained in a list or map value.
      Parameters:
      input - the serialized value
      Returns:
      the collection size, if applicable
    • copy

      public abstract T copy(T input)
      Creates a deep copy of the specified serialized value.
      Parameters:
      input - the value to copy
      Returns:
      a copy that is independent of the original
    • mergeToList

      public T mergeToList(T list, T value)
      Appends a value to a serialized list.

      If the supplied value is not already a list, a new list containing both the original value and the appended value is created.

      Parameters:
      list - the target list value
      value - the value to append
      Returns:
      the resulting list
    • mergeToMap

      public T mergeToMap(T map, T key, T value)
      Inserts or replaces an entry in a serialized map.

      If the supplied value is not already a map, a new map containing only the provided key-value pair is created.

      Parameters:
      map - the target map value
      key - the entry key
      value - the entry value
      Returns:
      the resulting map
    • exists

      public boolean exists(T input, String path)
      Determines whether a value exists at the specified path.
      Parameters:
      input - the root value
      path - the path to check
      Returns:
      true if a value exists at the path
    • exists

      public boolean exists(T input, DataPath path)
      Determines whether a value exists at the specified path.
      Parameters:
      input - the root value
      path - the compiled path to check
      Returns:
      true if a value exists at the path
    • query

      public Optional<T> query(T input, String path)
      Retrieves a nested value using a path expression.
      Parameters:
      input - the root value
      path - the path expression
      Returns:
      the value at the path, if present
    • query

      public Optional<T> query(T input, DataPath path)
      Retrieves a nested value using a compiled path.
      Parameters:
      input - the root value
      path - the compiled path
      Returns:
      the value at the path, if present
    • set

      public T set(T input, String path, T value)
      Sets a value at the specified path, creating intermediate containers as needed.
      Parameters:
      input - the root value
      path - the path expression
      value - the value to store
      Returns:
      the updated root value
    • set

      public T set(T input, DataPath path, T value)
      Sets a value at the specified path, creating intermediate containers as needed.
      Parameters:
      input - the root value
      path - the compiled path
      value - the value to store
      Returns:
      the updated root value
    • edit

      public T edit(T input, String path, Function<T,T> editor)
      Applies a transformation to the value located at the specified path.

      Missing intermediate containers are created as needed. The operation follows copy-on-write semantics and returns a new root value containing the modification.

      Parameters:
      input - the root value
      path - the path expression
      editor - the function used to transform the target value
      Returns:
      the updated root value
    • edit

      public T edit(T input, DataPath path, Function<T,T> editor)
      Applies a transformation to the value located at the specified path.

      Missing intermediate containers are created as needed. The operation follows copy-on-write semantics and returns a new root value containing the modification.

      Parameters:
      input - the root value
      path - the compiled path
      editor - the function used to transform the target value
      Returns:
      the updated root value
    • remove

      public T remove(T input, String path)
      Removes the value located at the specified path.
      Parameters:
      input - the root value
      path - the path expression
      Returns:
      the updated root value
    • remove

      public T remove(T input, DataPath path)
      Removes the value located at the specified path.
      Parameters:
      input - the root value
      path - the compiled path
      Returns:
      the updated root value
    • convertTo

      public <R> R convertTo(DynamicOps<R> outOps, T input)
      Converts a serialized value from this format into another format.

      Primitive values, lists, and maps are recursively translated using the target DynamicOps implementation.

      Type Parameters:
      R - the target serialized value type
      Parameters:
      outOps - the target operations implementation
      input - the value to convert
      Returns:
      the converted value