Class KiwiCasts2

java.lang.Object
org.kiwiproject.beta.base.KiwiCasts2

@Beta public final class KiwiCasts2 extends Object
Utilities related to casting.

Some of these methods may be moved into org.kiwiproject.base.KiwiCasts in kiwi.

  • Field Details

    • DEFAULT_MAX_NON_NULL_CHECKS

      public static final int DEFAULT_MAX_NON_NULL_CHECKS
      The default maximum number of non-null checks to perform when checking elements in a collection.
      See Also:
    • DEFAULT_MAX_TYPE_CHECKS

      public static final int DEFAULT_MAX_TYPE_CHECKS
      The default maximum number of type checks to perform when checking elements in a collection.
      See Also:
  • Method Details

    • uncheckedCast

      public static <T> T uncheckedCast(Object object)
      Performs an unchecked cast of the given object to the specified type.

      Note that this method simply delegates to KiwiCasts.uncheckedCast(Object) and may be removed in a future version.

      Type Parameters:
      T - the type to cast to
      Parameters:
      object - the object to cast
      Returns:
      the object cast to the specified type
    • castToCollectionAndCheckElements

      public static <T> Collection<T> castToCollectionAndCheckElements(Class<T> expectedType, Object object)
      Casts the given object to a Collection and checks that its elements are of the expected type. Uses KiwiCasts2.DefaultCollectionCheckStrategy as the collection check strategy.
      Type Parameters:
      T - the expected element type
      Parameters:
      expectedType - the expected type of elements in the collection
      object - the object to cast to a Collection
      Returns:
      the object cast to a Collection with elements of the expected type
      Throws:
      TypeMismatchException - if the object is not a Collection or contains elements of incompatible types
    • castToCollectionAndCheckElements

      public static <T> Collection<T> castToCollectionAndCheckElements(Class<T> expectedType, Object object, KiwiCasts2.CollectionCheckStrategy strategy)
      Casts the given object to a Collection and checks that its elements are of the expected type using the specified strategy.
      Type Parameters:
      T - the expected element type
      Parameters:
      expectedType - the expected type of elements in the collection
      object - the object to cast to a Collection
      strategy - the strategy to use for checking elements
      Returns:
      the object cast to a Collection with elements of the expected type
      Throws:
      TypeMismatchException - if the object is not a Collection or contains elements of incompatible types
    • castToListAndCheckElements

      public static <T> List<T> castToListAndCheckElements(Class<T> expectedType, Object object)
      Casts the given object to a List and checks that its elements are of the expected type. Uses KiwiCasts2.DefaultListCheckStrategy as the list check strategy.
      Type Parameters:
      T - the expected element type
      Parameters:
      expectedType - the expected type of elements in the list
      object - the object to cast to a List
      Returns:
      the object cast to a List with elements of the expected type
      Throws:
      TypeMismatchException - if the object is not a List or contains elements of incompatible types
    • castToListAndCheckElements

      public static <T> List<T> castToListAndCheckElements(Class<T> expectedType, Object object, KiwiCasts2.ListCheckStrategy strategy)
      Casts the given object to a List and checks that its elements are of the expected type using the specified check strategy.
      Type Parameters:
      T - the expected element type
      Parameters:
      expectedType - the expected type of elements in the list
      object - the object to cast to a List
      strategy - the strategy to use for checking elements
      Returns:
      the object cast to a List with elements of the expected type
      Throws:
      TypeMismatchException - if the object is not a List or contains elements of incompatible types
    • castToSetAndCheckElements

      public static <T> Set<T> castToSetAndCheckElements(Class<T> expectedType, Object object)
      Casts the given object to a Set and checks that its elements are of the expected type. Uses KiwiCasts2.DefaultSetCheckStrategy as the set check strategy.
      Type Parameters:
      T - the expected element type
      Parameters:
      expectedType - the expected type of elements in the set
      object - the object to cast to a Set
      Returns:
      the object cast to a Set with elements of the expected type
      Throws:
      TypeMismatchException - if the object is not a Set or contains elements of incompatible types
    • castToSetAndCheckElements

      public static <T> Set<T> castToSetAndCheckElements(Class<T> expectedType, Object object, KiwiCasts2.SetCheckStrategy strategy)
      Casts the given object to a Set and checks that its elements are of the expected type using the specified check strategy.
      Type Parameters:
      T - the expected element type
      Parameters:
      expectedType - the expected type of elements in the set
      object - the object to cast to a Set
      strategy - the strategy to use for checking elements
      Returns:
      the object cast to a Set with elements of the expected type
      Throws:
      TypeMismatchException - if the object is not a Set or contains elements of incompatible types
    • castToMapAndCheckEntries

      public static <K, V> Map<K,V> castToMapAndCheckEntries(Class<K> keyType, Class<V> valueType, Object object)
      Casts the given object to a Map and checks that its keys and values are of the expected types. Uses KiwiCasts2.DefaultMapCheckStrategy as the map check strategy.
      Type Parameters:
      K - the expected key type
      V - the expected value type
      Parameters:
      keyType - the expected type of keys in the map
      valueType - the expected type of values in the map
      object - the object to cast to a Map
      Returns:
      the object cast to a Map with keys and values of the expected types
      Throws:
      TypeMismatchException - if the object is not a Map or contains keys or values of incompatible types
    • castToMapAndCheckEntries

      public static <K, V> Map<K,V> castToMapAndCheckEntries(Class<K> keyType, Class<V> valueType, Object object, KiwiCasts2.MapCheckStrategy strategy)
      Casts the given object to a Map and checks that its keys and values are of the expected types using the specified check strategy.
      Type Parameters:
      K - the expected key type
      V - the expected value type
      Parameters:
      keyType - the expected type of keys in the map
      valueType - the expected type of values in the map
      object - the object to cast to a Map
      strategy - the strategy to use for checking entries
      Returns:
      the object cast to a Map with keys and values of the expected types
      Throws:
      TypeMismatchException - if the object is not a Map or contains keys or values of incompatible types