Class BasicTypesCompatibilityChecker


  • public final class BasicTypesCompatibilityChecker
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> MAP_BASIC_TYPE2WRAPPER
      Maps basic classes to the corresponding wrapper classes.
      private static java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> MAP_WRAPPER2BASIC_TYPE
      Maps wrapper classes to the corresponding basic classes.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BasicTypesCompatibilityChecker()
      Prevents BasicTypesCompatibilityChecker from being instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean areCompatible​(java.lang.Class<?> cls, java.lang.Object obj)
      Decides whether the given class and object are compatible.
      static java.lang.Class<?> getWrappedCls​(java.lang.Class<?> cls, boolean allowsVoid)
      Maps a wrapper class cls to wrapped primitive class.
      static java.lang.Class<?> getWrapperCls​(java.lang.Class<?> cls, boolean allowsVoid)
      Maps a primitive class cls to the corresponding wrapper class.
      static boolean isPrimitive​(java.lang.Class<?> cls)
      Returns whether cls represents a primitive type.
      static boolean wrapsPrimitive​(java.lang.Class<?> cls)
      Returns whether cls represents a type wrapping a primitive type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAP_WRAPPER2BASIC_TYPE

        private static final java.util.Map<java.lang.Class<?>,​java.lang.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 java.util.Map<java.lang.Class<?>,​java.lang.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

        private BasicTypesCompatibilityChecker()
        Prevents BasicTypesCompatibilityChecker from being instantiated.
    • Method Detail

      • isPrimitive

        public static boolean isPrimitive​(java.lang.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​(java.lang.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​(java.lang.Class<?> cls,
                                            java.lang.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 java.lang.Class<?> getWrappedCls​(java.lang.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:
        java.lang.IllegalArgumentException - if cls wraps no primitive type or if cls is Void.TYPE but this is not allowed by allowsVoid.
      • getWrapperCls

        public static java.lang.Class<?> getWrapperCls​(java.lang.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:
        java.lang.IllegalArgumentException - if cls is no primitive type or if cls is Void.TYPE but this is not allowed by allowsVoid.