Module net.luis.utils
Package net.luis.utils.io.codec.struct
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 uses a converter to convert between numbers and the range type.
The codec can be configured by the following methods:
positive()
to only accept positive numberspositiveOrZero()
to accept positive numbers and zeronegative()
to only accept negative numbersnegativeOrZero()
to accept negative numbers and zeroatLeast(Number)
to only accept numbers greater or equal to the given numberatMost(Number)
to only accept numbers smaller or equal to the given numberrange(Number, Number)
to only accept numbers in the given range (inclusive)
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 Summary
FieldsModifier and TypeFieldDescriptionThe converter used to convert between numbers and the range type.
The converter is used to avoid casting numbers to generics.private final C
The maximum value of the range (inclusive).private final C
The minimum value of the range (inclusive).private final String
The name of the underlying range type.The function used to decode the range type from a string.The function used to encode the range type as a string.Fields inherited from interface net.luis.utils.io.codec.Codec
BOOLEAN, BYTE, BYTE_ARRAY, CHARSET, DOUBLE, DOUBLE_STREAM, FILE, FLOAT, INT_STREAM, INTEGER, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_STREAM, PATH, SHORT, STRING, URI, URL, UUID, ZONED_DATE_TIME
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
RangeCodec
(@NotNull String name, C min, C max, @NotNull Function<Number, C> converter, @NotNull Function<C, String> stringEncoder, @NotNull Function<String, C> stringDecoder) Constructs a new range codec using the given name, minimum and maximum value, converter, encoder and decoder.protected
RangeCodec
(@NotNull String name, C min, C max, @NotNull Function<Number, C> converter, @NotNull Function<String, C> decoder) Constructs a new range codec using the given name, minimum and maximum value, converter and decoder. -
Method Summary
Modifier and TypeMethodDescription@NotNull RangeCodec
<C> Returns a new range codec that only accepts numbers greater or equal to the given number.@NotNull RangeCodec
<C> Returns a new range codec that only accepts numbers smaller or equal to the given number.decodeKey
(@NotNull TypeProvider<R> provider, @NotNull String key) Decodes a key to a value of the specified type and returns the result.
The result contains the decoded key or an error message.decodeNumber
(@NotNull TypeProvider<R> provider, 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.decodeStart
(@NotNull TypeProvider<R> provider, R value) Decodes the value of the specified type and returns the result.
The result contains the decoded value or an error message.encodeKey
(@NotNull TypeProvider<R> provider, C key) 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.protected abstract <R> @NotNull Result
<R> encodeNumber
(@NotNull TypeProvider<R> provider, 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.<R> @NotNull Result
<R> encodeStart
(@NotNull TypeProvider<R> provider, R current, C value) Encodes the value of the specified type and returns the result.
The result contains the encoded value or an error message.boolean
int
hashCode()
@NotNull RangeCodec
<C> negative()
Returns a new range codec that only accepts negative numbers.@NotNull RangeCodec
<C> Returns a new range codec that accepts negative numbers and zero.@NotNull RangeCodec
<C> positive()
Returns a new range codec that only accepts positive numbers.@NotNull RangeCodec
<C> Returns a new range codec that accepts positive numbers and zero.@NotNull RangeCodec
<C> Returns a new range codec that only accepts numbers in the given range (inclusive).toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.luis.utils.io.codec.Codec
configure, getter, keyable, list, list, list, map, mapFlat, named, noneEmptyList, optional, orElse, orElseGet, stream, validate, withAlternative, xmap
Methods inherited from interface net.luis.utils.io.codec.decoder.Decoder
decode, mapDecoder
Methods inherited from interface net.luis.utils.io.codec.encoder.Encoder
encode, encode, mapEncoder
-
Field Details
-
name
The name of the underlying range type. -
minInclusive
The minimum value of the range (inclusive). -
maxInclusive
The maximum value of the range (inclusive). -
converter
The converter used to convert between numbers and the range type.
The converter is used to avoid casting numbers to generics. -
stringEncoder
The function used to encode the range type as a string. -
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 typemin
- 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 typedecoder
- 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 typemin
- 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 typestringEncoder
- The encoder used to encode the range type as a stringstringDecoder
- 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 toTypeProvider.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 interfaceEncoder<C extends Number & Comparable<C>>
- Type Parameters:
R
- The type to encode to- Parameters:
provider
- The type providercurrent
- The current valuevalue
- 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 providervalue
- 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 interfaceKeyableEncoder<C extends Number & Comparable<C>>
- Type Parameters:
R
- The type to encode to- Parameters:
provider
- The type providerkey
- 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 interfaceDecoder<C extends Number & Comparable<C>>
- Type Parameters:
R
- The type to decode from- Parameters:
provider
- The type providervalue
- 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 providervalue
- 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 interfaceKeyableDecoder<C extends Number & Comparable<C>>
- Type Parameters:
R
- The type to decode from- Parameters:
provider
- The type providerkey
- The key to decode- Returns:
- The result
-
positive
Returns a new range codec that only accepts positive numbers.- Returns:
- The new range codec
-
positiveOrZero
Returns a new range codec that accepts positive numbers and zero.- Returns:
- The new range codec
-
negative
Returns a new range codec that only accepts negative numbers.- Returns:
- The new range codec
-
negativeOrZero
Returns a new range codec that accepts negative numbers and zero.- Returns:
- The new range codec
-
atLeast
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
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
Returns a new range codec that only accepts numbers in the given range (inclusive).- Parameters:
minInclusive
- The minimum valuemaxInclusive
- The maximum value- Returns:
- The new range codec
- Throws:
NullPointerException
- If the minimum or maximum value is null
-
equals
-
hashCode
public int hashCode() -
toString
-