public abstract class AdapterUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
AdapterUtils.AdapterNotFoundException
Thrown by
getAdapterChecked(Object, Class) when no
adapter has been found. |
Constructor and Description |
---|
AdapterUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
getAdapter(Object source,
Class<T> type)
Returns the given source object adapted to the
type, or null if no adapter has been found.
|
static <T> T |
getAdapterChecked(Object source,
Class<T> type)
A version of
getAdapter(Object, Class) that throws an
AdapterUtils.AdapterNotFoundException if no adapter could be found. |
static <T> T[] |
getAdapters(Object[] sources,
Class<T> type,
boolean withNulls)
Adapts a whole array of items to the target type.
|
public static <T> T getAdapter(Object source, Class<T> type)
IAdaptable
(which is perfectly OK for the
platform's IAdapterManager
), and it uses generics to avoid all
these class cast orgies. You can simply write something like:
TargetType t = AdapterUtils.getAdapter(sourceObject, TargetType.class)
T
- The type to adapt to.source
- The object that needs to be adapted.type
- The class of the target type.null
if none could be found. Note that his does not
load plugins.public static <T> T getAdapterChecked(Object source, Class<T> type) throws AdapterUtils.AdapterNotFoundException
getAdapter(Object, Class)
that throws an
AdapterUtils.AdapterNotFoundException
if no adapter could be found.T
- The target typesource
- The object to adapttype
- The target type's classAdapterUtils.AdapterNotFoundException
- if no adapter coud be foundpublic static <T> T[] getAdapters(Object[] sources, Class<T> type, boolean withNulls)
T
- The type of the individual target objects.sources
- An array of source objects to convert.type
- The class of the individual target objects.withNulls
- Determines what happens with objects that adapt to
null
: If true
, insert a
null
in the result array, if false
,
omit the item from the result array.getAdapter(Object, Class)