Package io.sundr.model.functions
Class TypeCast
- java.lang.Object
-
- io.sundr.model.functions.TypeCast
-
public class TypeCast extends Object implements Function<io.sundr.model.TypeRef,Optional<io.sundr.model.ClassRef>>
This function can be thought asTypes.isInstanceOf(TypeRef, TypeDef, Function)with added bonus that generic arguments are resolved.For example, when
TypeCast.to(Map<?,?>)is called onHashMap<String, Integer, the result will beOptional.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<io.sundr.model.ClassRef>apply(io.sundr.model.TypeRef type)Perform the type cast, if possible.static TypeCastto(io.sundr.model.ClassRef expectedType)Create the function which casts to the specified target type.
-
-
-
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:
applyin interfaceFunction<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.
-
-