Class BasicTypesCompatibilityChecker


  • public abstract class BasicTypesCompatibilityChecker
    extends Object
    Provides methods to map basic types to their wrappers, to map wrapper types to the corresponding basic types and for compatibility checks.
    Version:
    1.0
    Author:
    Ernst Reissner
    • Field Detail

      • MAP_WRAPPER2BASIC_TYPE

        private static final Map<Class<?>,​Class<?>> MAP_WRAPPER2BASIC_TYPE
        Maps wrapper classes to the corresponding basic classes. E.g. Boolean is mapped to boolean. Note that Void.TYPE is mapped to itself.
        See Also:
        areCompatible(Class, Object)
      • MAP_BASIC_TYPE2WRAPPER

        private static final Map<Class<?>,​Class<?>> MAP_BASIC_TYPE2WRAPPER
        Maps basic classes to the corresponding wrapper classes. E.g. boolean is mapped to Boolean. Note that Void.TYPE is mapped to itself.
        See Also:
        (Class, Object)
    • Constructor Detail

      • BasicTypesCompatibilityChecker

        public BasicTypesCompatibilityChecker()
    • Method Detail

      • isPrimitive

        public static boolean isPrimitive​(Class<?> cls)
        Returns whether cls represents a primitive type. Note that also Void.TYPE is a primitive type. An implementation of Class.isPrimitive().
        Returns:
        Whether cls represents a primitive type.
        See Also:
        wrapsPrimitive(Class)
      • wrapsPrimitive

        public static boolean wrapsPrimitive​(Class<?> cls)
        Returns whether cls represents a type wrapping a primitive type. As an example Integer wraps int and Void.TYPE wraps itself.
        Returns:
        Whether cls wraps a primitive type.
        See Also:
        isPrimitive(Class)
      • areCompatible

        public static boolean areCompatible​(Class<?> cls,
                                            Object obj)
        Decides whether the given class and object are compatible.
        Parameters:
        cls - a Class (of course not null).
        obj - an Object including null.
        Returns:
        true if and only if either of the following holds:
        • cls does not refer to a primitive type and obj may be casted to cls, i.e. if either obj == null or (sequentially) cls.isInstance(obj).
        • cls refers to a primitive type and (sequentially) obj != null and obj.getClass() is the corresponding wrapper type.
        See Also:
        MAP_WRAPPER2BASIC_TYPE
      • getWrappedCls

        public static Class<?> getWrappedCls​(Class<?> cls,
                                             boolean allowsVoid)
        Maps a wrapper class cls to wrapped primitive class.
        Parameters:
        cls - some Class object.
        allowsVoid - whether Void.TYPE is wrapped by itself.
        Returns:
        the primitive type wrapped type cls if cls is the wrapper of a primitive type. Note that Void.TYPE may be wrapped by itself.
        Throws:
        IllegalArgumentException - if cls wraps no primitive type or if cls is Void.TYPE but this is not allowed by allowsVoid.
      • getWrapperCls

        public static Class<?> getWrapperCls​(Class<?> cls,
                                             boolean allowsVoid)
        Maps a primitive class cls to the corresponding wrapper class.
        Parameters:
        cls - some Class object.
        allowsVoid - whether Void.TYPE wraps itself.
        Returns:
        the wrapper type of type cls if cls is a primitive type. Note that Void.TYPE may wrap itself.
        Throws:
        IllegalArgumentException - if cls is no primitive type or if cls is Void.TYPE but this is not allowed by allowsVoid.