Package dev.morphia.mapping.codec
Class Conversions
- java.lang.Object
-
- dev.morphia.mapping.codec.Conversions
-
@MorphiaInternal public final class Conversions extends Object
Defines basic type conversions. This class is mostly intended for internal use only but its methods are public so that when cases arise where certain conversions are missing, users can add their in between releases. However, this must be done with the understand that, however unlikely, this API is subject to change and any uses might break at some point.Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
convert(Object value, Class<T> target)
Attempts to convert a value to the given typestatic <S,T>
voidregister(Class<S> source, Class<T> target, Function<S,T> function)
Register a conversion between two types.static <S,T>
voidregister(Class<S> source, Class<T> target, Function<S,T> function, String warning)
Register a conversion between two types.
-
-
-
Method Detail
-
register
public static <S,T> void register(Class<S> source, Class<T> target, Function<S,T> function)
Register a conversion between two types. For example, to register the conversion ofDate
to aLong
, this method could be invoked as follows:register(Date.class, Long.class, Date::getTime);
- Type Parameters:
S
- the source typeT
- the target type.- Parameters:
source
- the source typetarget
- the target typefunction
- the function that performs the conversion. This is often just a method reference.
-
convert
@Nullable public static <T> T convert(@Nullable Object value, Class<T> target)
Attempts to convert a value to the given type- Type Parameters:
T
- the target type- Parameters:
value
- the value to converttarget
- the target type- Returns:
- the potentially converted value
-
register
public static <S,T> void register(Class<S> source, Class<T> target, Function<S,T> function, @Nullable String warning)
Register a conversion between two types. For example, to register the conversion ofDate
to aLong
, this method could be invoked as follows:register(Date.class, Long.class, Date::getTime);
- Type Parameters:
S
- the source typeT
- the target type.- Parameters:
source
- the source typetarget
- the target typefunction
- the function that performs the conversion. This is often just a method reference.warning
- if non-null, this will be the message logged on the WARN level indicating the conversion is taking place.
-
-