Package ch.njol.skript.registrations
Class Converters
java.lang.Object
ch.njol.skript.registrations.Converters
Contains all registered converters and allows operating with them.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <F, T> @Nullable T
Converts an object into one of the given types.static <F, T> @Nullable T
Converts the given value to the desired type.static <F, T> T[]
static <T> T[]
convertArray(@Nullable Object[] o, Class<? extends T>[] to, Class<T> superType)
Converts multiple objects into any of the given classes.static <T> @Nullable T[]
convertArray(@Nullable Object[] o, Class<T> to)
Converts all entries in the given array to the desired type, usingconvert(Object, Class)
to convert every single value.static boolean
converterExists(Class<?> from, Class<?> to)
Tests whether a converter between the given classes exists.static boolean
converterExists(Class<?> from, Class<?>... to)
static <T> T[]
convertStrictly(Object[] original, Class<T> to)
Strictly converts an array to a non-null array of the specified class.static <T> T
convertStrictly(Object original, Class<T> to)
Strictly converts an object to the specified classstatic <F, T> T[]
convertUnsafe(F[] from, Class<?> to, Converter<? super F,? extends T> conv)
static void
static <F, T> @Nullable Converter<? super F,? extends T>
getConverter(Class<F> from, Class<T> to)
Gets a converterstatic <F, T> @Nullable Converter.ConverterInfo<? super F,? extends T>
getConverterInfo(Class<F> from, Class<T> to)
Gets a converter that has been registered before.static List<Converter.ConverterInfo<?,?>>
static <F, T> void
registerConverter(Class<F> from, Class<T> to, Converter<F,T> converter)
Registers a converter.static <F, T> void
registerConverter(Class<F> from, Class<T> to, Converter<F,T> converter, int options)
-
Method Details
-
getConverters
-
registerConverter
public static <F, T> void registerConverter(Class<F> from, Class<T> to, Converter<F,T> converter)Registers a converter.- Parameters:
from
- Type that the converter converts from.to
- Type that the converter converts to.converter
- Actual converter.
-
registerConverter
-
createMissingConverters
public static void createMissingConverters() -
convert
Converts the given value to the desired type. If you want to convert multiple values of the same type you should usegetConverter(Class, Class)
to get a converter to convert the values.- Parameters:
o
-to
-- Returns:
- The converted value or null if no converter exists or the converter returned null for the given value.
-
convert
Converts an object into one of the given types.This method does not convert the object if it is already an instance of any of the given classes.
- Parameters:
o
-to
-- Returns:
- The converted object
-
convertArray
Converts all entries in the given array to the desired type, usingconvert(Object, Class)
to convert every single value. If you want to convert an array of values of a known type, consider usingconvert(Object[], Class, Converter)
for much better performance.- Parameters:
o
-to
-- Returns:
- A T[] array without null elements
-
convertArray
public static <T> T[] convertArray(@Nullable Object[] o, Class<? extends T>[] to, Class<T> superType)Converts multiple objects into any of the given classes.- Parameters:
o
-to
-superType
- The component type of the returned array- Returns:
- The converted array
-
convertStrictly
Strictly converts an array to a non-null array of the specified class. Uses registeredConverters
to convert.- Parameters:
original
- The array to convertto
- What to convertoriginal
to- Returns:
original
converted to an array ofto
- Throws:
ClassCastException
- if one oforiginal
's elements cannot be converted to ato
-
convertStrictly
Strictly converts an object to the specified class- Parameters:
original
- The object to convertto
- What to convertoriginal
to- Returns:
original
converted to ato
- Throws:
ClassCastException
- iforiginal
could not be converted to ato
-
converterExists
Tests whether a converter between the given classes exists.- Parameters:
from
-to
-- Returns:
- Whether a converter exists
-
converterExists
-
getConverter
public static <F, T> @Nullable Converter<? super F,? extends T> getConverter(Class<F> from, Class<T> to)Gets a converter- Parameters:
from
-to
-- Returns:
- the converter or null if none exist
-
getConverterInfo
public static <F, T> @Nullable Converter.ConverterInfo<? super F,? extends T> getConverterInfo(Class<F> from, Class<T> to)Gets a converter that has been registered before.- Parameters:
from
-to
-- Returns:
- The converter info or null if no converters were found.
-
convertUnsafe
public static <F, T> T[] convertUnsafe(F[] from, Class<?> to, Converter<? super F,? extends T> conv)- Parameters:
from
-to
-conv
-- Returns:
- The converted array
- Throws:
ArrayStoreException
- if the given class is not a superclass of all objects returned by the converter
-
convert
-