-
- Type Parameters:
S- serialized typeD- deserialized type
- All Superinterfaces:
Deserializer<S,D>,Serializer<D,S>
public interface Adapter<S,D> extends Deserializer<S,D>, Serializer<D,S>
Combines a serializer with a compatible deserializer.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceAdapter.StandardSet<S>A collection of standard adapters.
-
Method Summary
Static Methods Modifier and Type Method Description static <S,D extends S>
Adapter<S,D>cast(Predicate<S> isInstance)Adapts objects by casting if they're compatible.static <S> Adapter<S,S>identity()Gets an adapter that simply returns the input.static <S,D>
Adapter<S,D>of(Deserializer<S,D> deserializer, Serializer<D,S> serializer)Creates a newAdapterby delegating to the provided arguments.static Adapter.StandardSet<Object>ofObject()Gets standard adapters for casting objects.static Adapter.StandardSet<String>ofString()Gets standard adapters for serialized strings.static <S,D>
Adapter<S,D>unsupported()Gets an adapter that always returnsOptional.empty()for both deserialization and serialization.-
Methods inherited from interface com.rezzedup.util.valuables.Deserializer
deserialize
-
Methods inherited from interface com.rezzedup.util.valuables.Serializer
serialize
-
-
-
-
Method Detail
-
of
static <S,D> Adapter<S,D> of(Deserializer<S,D> deserializer, Serializer<D,S> serializer)
Creates a newAdapterby delegating to the provided arguments.- Type Parameters:
S- serialized typeD- deserialized type- Parameters:
deserializer- the deserializerserializer- the serializer- Returns:
- a new instance composed of the arguments
-
identity
static <S> Adapter<S,S> identity()
Gets an adapter that simply returns the input.- Type Parameters:
S- serialized type- Returns:
- an adapter that always returns whatever it received as input
-
unsupported
static <S,D> Adapter<S,D> unsupported()
Gets an adapter that always returnsOptional.empty()for both deserialization and serialization.- Type Parameters:
S- serialized typeD- deserialized type- Returns:
- an adapter that always returns empty
-
cast
static <S,D extends S> Adapter<S,D> cast(Predicate<S> isInstance)
Adapts objects by casting if they're compatible. Whether or not a serialized value can be cast is determined by checking the providedisInstancepredicate.Example:
Adapter<Number, Double> adapter = Adapter.cast(o -> o instanceof Double); Number serialized = adapter.serialize(5.0).orElseThrow(); Double deserialized = adapter.deserialize(serialized).orElseThrow();- Type Parameters:
S- serialized (super) typeD- deserialized (sub) type- Parameters:
isInstance- checks whether the serialized value is an instance of the desired type- Returns:
- an adapter that deserializes to a subtype by casting
-
ofString
static Adapter.StandardSet<String> ofString()
Gets standard adapters for serialized strings.Strings will be parsed into deserialized values.
- Returns:
- adapters for strings
-
ofObject
static Adapter.StandardSet<Object> ofObject()
Gets standard adapters for casting objects.Objects will be cast into deserialized values, with a few exceptions:
intoString()converts objects into their string representation.- All number types are compatible with each
other since they all extend
Number.
- Returns:
- adapters for objects
-
-