Class TypeCast

  • All Implemented Interfaces:
    Function<io.sundr.model.TypeRef,​Optional<io.sundr.model.ClassRef>>

    public class TypeCast
    extends Object
    implements Function<io.sundr.model.TypeRef,​Optional<io.sundr.model.ClassRef>>
    This function can be thought as Types.isInstanceOf(TypeRef, TypeDef, Function) with added bonus that generic arguments are resolved.

    For example, when TypeCast.to(Map<?,?>) is called on HashMap<String, Integer, the result will be Optional.of(Map<String, Integer>).

    This works also for complex hierarchies with non-trivial type argument substitutions.

    Limitation: Arguments involving wildcards are currently not supported.

    • Method Detail

      • to

        public static TypeCast to​(io.sundr.model.ClassRef expectedType)
        Create the function which casts to the specified target type.
        Parameters:
        expectedType - The type to which to cast. It must not be an array, and all type arguments (if any) must be unbounded wildcards
      • apply

        public Optional<io.sundr.model.ClassRef> apply​(io.sundr.model.TypeRef type)
        Perform the type cast, if possible.
        Specified by:
        apply in interface Function<io.sundr.model.TypeRef,​Optional<io.sundr.model.ClassRef>>
        Parameters:
        type - The type which will be cast
        Returns:
        If the type can be cast to target type, Optional.of(targetType<...>) is returned with the type arguments resolved. If the type cannot be cast, Optional.empty() is returned.
        Throws:
        IllegalStateException - when the type implements or extends target type multiple times with different arguments. Currently, this may also apply to multiple inheritance of wildcard types, even if they were compatible.