Class TypeCast

java.lang.Object
io.sundr.model.functions.TypeCast
All Implemented Interfaces:
Function<TypeRef,Optional<ClassRef>>

public class TypeCast extends Object implements Function<TypeRef,Optional<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 Details

    • to

      public static TypeCast to(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<ClassRef> apply(TypeRef type)
      Perform the type cast, if possible.
      Specified by:
      apply in interface Function<TypeRef,Optional<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.