Package eu.simuline.util
Class Caster
- java.lang.Object
-
- eu.simuline.util.Caster
-
- Direct Known Subclasses:
Caster.Trivial
public abstract class Caster extends java.lang.ObjectProvides ways to transform instances of various classes into one another. The core method iscast(java.lang.Object): it performs the transformation.A simple example would be to transform
Doubles intoeu.simuline.arithmetics.left2right.FPNumbers. Another example is given byBASIC_TYPES. It is the foundation to transform arrays of elementary types to arrays of their wrappers and the other way round such asdouble[][]andDouble[][].Casters may be used to transform descriptors of objects into these objects. A snippet of typical implementation for the
cast(java.lang.Object)-method would be "return new ExaClass(descriptor)" in this case.- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classCaster.TrivialThis is a trivial caster.
-
Field Summary
Fields Modifier and Type Field Description static CasterASSIGNEMENT_COMPATIBLEA trivial caster for which compatibility is assignment compatibility.static CasterBASIC_TYPESA trivial caster for which compatibility is assignment compatibility up to wrapping and unwrapping of primitive types.static CasterSTRICTA trivial caster for which compatibility is equality of classes.
-
Constructor Summary
Constructors Constructor Description Caster()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract booleanareCompatible(java.lang.Class<?> cls, java.lang.Object obj)Decides whether the given class and object are compatible with respect to this caster.abstract java.lang.Objectcast(java.lang.Object obj)The return valuecast(obj)(which may well benull), may be casted to classclsifareCompatible(java.lang.Class<?>, java.lang.Object)returnstrue.
-
-
-
Field Detail
-
ASSIGNEMENT_COMPATIBLE
public static final Caster ASSIGNEMENT_COMPATIBLE
A trivial caster for which compatibility is assignment compatibility.
-
STRICT
public static final Caster STRICT
A trivial caster for which compatibility is equality of classes.
-
BASIC_TYPES
public static final Caster BASIC_TYPES
A trivial caster for which compatibility is assignment compatibility up to wrapping and unwrapping of primitive types. Used to implementCollectionsExt.recToArray(Object,Class)as a special case ofCollectionsExt.recToArray(Object,Class,Caster).
-
-
Method Detail
-
areCompatible
public abstract boolean areCompatible(java.lang.Class<?> cls, java.lang.Object obj)Decides whether the given class and object are compatible with respect to this caster.- Parameters:
cls- aClass.obj- anObject.- Returns:
trueif and only if either of the following holds:-
cast(obj)may be casted tocls, i.e. if eithercast(obj) == null(which is the case forobj == null) or (sequentially)cls.isInstance(cast(obj)). -
clsis a basic type,cast(obj) != nulland (sequentially)cast(obj).getClass()is the corresponding wrapper type.
-
- See Also:
BasicTypesCompatibilityChecker.areCompatible(java.lang.Class<?>, java.lang.Object)
-
cast
public abstract java.lang.Object cast(java.lang.Object obj)
The return valuecast(obj)(which may well benull), may be casted to classclsifareCompatible(java.lang.Class<?>, java.lang.Object)returnstrue.- Parameters:
obj- an arbitraryObjector evennull.- Returns:
-
nullforobj == null. -
nullifobj != nullbuttargetClass(obj.getClass()) == null. **** -
either
nullagain or an instance oftargetClass(obj.getClass())
-
-
-