Class Caster

  • Direct Known Subclasses:
    Caster.Trivial

    public abstract class Caster
    extends Object
    Provides ways to transform instances of various classes into one another. The core method is cast(java.lang.Object): it performs the transformation.

    A simple example would be to transform Doubles into eu.simuline.arithmetics.left2right.FPNumbers. Another example is given by BASIC_TYPES. It is the foundation to transform arrays of elementary types to arrays of their wrappers and the other way round such as double[][] and Double[][].

    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
    • 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.
    • Constructor Detail

      • Caster

        public Caster()
    • Method Detail

      • areCompatible

        public abstract boolean areCompatible​(Class<?> cls,
                                              Object obj)
        Decides whether the given class and object are compatible with respect to this caster.
        Parameters:
        cls - a Class.
        obj - an Object.
        Returns:
        true if and only if either of the following holds:
        • cast(obj) may be casted to cls, i.e. if either cast(obj) == null (which is the case for obj == null) or (sequentially) cls.isInstance(cast(obj)).
        • cls is a basic type, cast(obj) != null and (sequentially) cast(obj).getClass() is the corresponding wrapper type.
        See Also:
        BasicTypesCompatibilityChecker.areCompatible(java.lang.Class<?>, java.lang.Object)
      • cast

        public abstract Object cast​(Object obj)
        The return value cast(obj) (which may well be null), may be casted to class cls if areCompatible(java.lang.Class<?>, java.lang.Object) returns true.
        Parameters:
        obj - an arbitrary Object or even null.
        Returns:
        • null for obj == null.
        • null if obj != null but targetClass(obj.getClass()) == null. ****
        • either null again or an instance of targetClass(obj.getClass())