Class RangeCodec<C extends Number & Comparable<C>>

java.lang.Object
net.luis.utils.io.codec.struct.RangeCodec<C>
Type Parameters:
C - The type of the range
All Implemented Interfaces:
Codec<C>, Decoder<C>, KeyableDecoder<C>, Encoder<C>, KeyableEncoder<C>, KeyableCodec<C>

public abstract class RangeCodec<C extends Number & Comparable<C>> extends Object implements KeyableCodec<C>
A codec for encoding and decoding number values of a certain range.
This codec uses a converter to convert between numbers and the range type.
The codec can be configured by the following methods:

This codec is keyable, so it can be used as a key in a map codec.
The key is encoded as a string using the given string encoder and decoder.

  • Field Details

    • name

      private final String name
      The name of the underlying range type.
    • minInclusive

      private final C extends Number & Comparable<C> minInclusive
      The minimum value of the range (inclusive).
    • maxInclusive

      private final C extends Number & Comparable<C> maxInclusive
      The maximum value of the range (inclusive).
    • converter

      private final Function<Number,C extends Number & Comparable<C>> converter
      The converter used to convert between numbers and the range type.
      The converter is used to avoid casting numbers to generics.
    • stringEncoder

      private final Function<C extends Number & Comparable<C>,String> stringEncoder
      The function used to encode the range type as a string.
    • stringDecoder

      private final Function<String,C extends Number & Comparable<C>> stringDecoder
      The function used to decode the range type from a string.
  • Constructor Details

    • RangeCodec

      protected RangeCodec(@NotNull @NotNull String name, @NotNull C min, @NotNull C max, @NotNull @NotNull Function<Number,C> converter, @NotNull @NotNull Function<String,C> decoder)
      Constructs a new range codec using the given name, minimum and maximum value, converter and decoder.
      Parameters:
      name - The name of the range type
      min - The minimum value of the range (inclusive)
      max - The maximum value of the range (inclusive)
      converter - The converter used to convert between numbers and the range type
      decoder - The decoder used to decode the range type from a string
      Throws:
      NullPointerException - If the name, minimum value, maximum value, converter or decoder is null
    • RangeCodec

      protected RangeCodec(@NotNull @NotNull String name, @NotNull C min, @NotNull C max, @NotNull @NotNull Function<Number,C> converter, @NotNull @NotNull Function<C,String> stringEncoder, @NotNull @NotNull Function<String,C> stringDecoder)
      Constructs a new range codec using the given name, minimum and maximum value, converter, encoder and decoder.
      Parameters:
      name - The name of the range type
      min - The minimum value of the range (inclusive)
      max - The maximum value of the range (inclusive)
      converter - The converter used to convert between numbers and the range type
      stringEncoder - The encoder used to encode the range type as a string
      stringDecoder - The decoder used to decode the range type from a string
      Throws:
      NullPointerException - If the name, minimum value, maximum value, converter, encoder or decoder is null
  • Method Details

    • encodeStart

      @NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable C value)
      Description copied from interface: Encoder
      Encodes the value of the specified type and returns the result.
      The result contains the encoded value or an error message.

      The current value is the value that is currently encoded.
      In the most cases this value should be equal to TypeProvider.empty().
      In the case of encoding a value that is part of a bigger structure, the current value should be the structure.

      Specified by:
      encodeStart in interface Encoder<C extends Number & Comparable<C>>
      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      current - The current value
      value - The value to encode
      Returns:
      The result
    • encodeNumber

      @NotNull protected abstract <R> @NotNull Result<R> encodeNumber(@NotNull @NotNull TypeProvider<R> provider, @NotNull C value)
      Encodes the given number value using the given type provider.
      The result contains the encoded value or an error message if the encoding failed.
      Type Parameters:
      R - The type of the encoded value
      Parameters:
      provider - The type provider
      value - The number value
      Returns:
      The result of the encoding process
      Throws:
      NullPointerException - If the type provider or value is null
    • encodeKey

      @NotNull public <R> @NotNull Result<String> encodeKey(@NotNull @NotNull TypeProvider<R> provider, @NotNull C key)
      Description copied from interface: KeyableEncoder
      Encodes the key of the specified type and returns the encoded key as a result.
      The result contains the encoded key or an error message.
      Specified by:
      encodeKey in interface KeyableEncoder<C extends Number & Comparable<C>>
      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      key - The key to encode
      Returns:
      The result
    • decodeStart

      @NotNull public <R> @NotNull Result<C> decodeStart(@NotNull @NotNull TypeProvider<R> provider, @Nullable R value)
      Description copied from interface: Decoder
      Decodes the value of the specified type and returns the result.
      The result contains the decoded value or an error message.
      Specified by:
      decodeStart in interface Decoder<C extends Number & Comparable<C>>
      Type Parameters:
      R - The type to decode from
      Parameters:
      provider - The type provider
      value - The value to decode
      Returns:
      The result
    • decodeNumber

      @NotNull protected abstract <R> @NotNull Result<C> decodeNumber(@NotNull @NotNull TypeProvider<R> provider, @NotNull R value)
      Decodes the given value using the given type provider.
      The result contains the decoded value or an error message if the decoding failed.
      Type Parameters:
      R - The type of the decoded value
      Parameters:
      provider - The type provider
      value - The value
      Returns:
      The result of the decoding process
      Throws:
      NullPointerException - If the type provider or value is null
    • decodeKey

      @NotNull public <R> @NotNull Result<C> decodeKey(@NotNull @NotNull TypeProvider<R> provider, @NotNull @NotNull String key)
      Description copied from interface: KeyableDecoder
      Decodes a key to a value of the specified type and returns the result.
      The result contains the decoded key or an error message.
      Specified by:
      decodeKey in interface KeyableDecoder<C extends Number & Comparable<C>>
      Type Parameters:
      R - The type to decode from
      Parameters:
      provider - The type provider
      key - The key to decode
      Returns:
      The result
    • positive

      @NotNull public @NotNull RangeCodec<C> positive()
      Returns a new range codec that only accepts positive numbers.
      Returns:
      The new range codec
    • positiveOrZero

      @NotNull public @NotNull RangeCodec<C> positiveOrZero()
      Returns a new range codec that accepts positive numbers and zero.
      Returns:
      The new range codec
    • negative

      @NotNull public @NotNull RangeCodec<C> negative()
      Returns a new range codec that only accepts negative numbers.
      Returns:
      The new range codec
    • negativeOrZero

      @NotNull public @NotNull RangeCodec<C> negativeOrZero()
      Returns a new range codec that accepts negative numbers and zero.
      Returns:
      The new range codec
    • atLeast

      @NotNull public @NotNull RangeCodec<C> atLeast(@NotNull C min)
      Returns a new range codec that only accepts numbers greater or equal to the given number.
      Parameters:
      min - The minimum value
      Returns:
      The new range codec
      Throws:
      NullPointerException - If the minimum value is null
    • atMost

      @NotNull public @NotNull RangeCodec<C> atMost(@NotNull C max)
      Returns a new range codec that only accepts numbers smaller or equal to the given number.
      Parameters:
      max - The maximum value
      Returns:
      The new range codec
      Throws:
      NullPointerException - If the maximum value is null
    • range

      @NotNull public @NotNull RangeCodec<C> range(@NotNull C minInclusive, @NotNull C maxInclusive)
      Returns a new range codec that only accepts numbers in the given range (inclusive).
      Parameters:
      minInclusive - The minimum value
      maxInclusive - The maximum value
      Returns:
      The new range codec
      Throws:
      NullPointerException - If the minimum or maximum value is null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object