Package org.kiwiproject.beta.base
Class KiwiCasts2
java.lang.Object
org.kiwiproject.beta.base.KiwiCasts2
Utilities related to casting.
Some of these methods may be moved into org.kiwiproject.base.KiwiCasts
in kiwi.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceStrategy interface for checking elements in a collection.static classDefault implementation ofKiwiCasts2.CollectionCheckStrategythat usesDEFAULT_MAX_NON_NULL_CHECKSas the maximum non-null checks and checks only one (non-null) element in the collection.static classDefault implementation ofKiwiCasts2.ListCheckStrategythat usesDEFAULT_MAX_NON_NULL_CHECKSas the maximum non-null checks and checks only one (non-null) element in the list.static classDefault implementation ofKiwiCasts2.MapCheckStrategythat usesDEFAULT_MAX_NON_NULL_CHECKSas the maximum non-null checks and checks only one (non-null) entry in the map.static classDefault implementation ofKiwiCasts2.SetCheckStrategythat usesDEFAULT_MAX_NON_NULL_CHECKSas the maximum non-null checks and checks only one (non-null) element in the set.static interfaceStrategy interface for checking elements in a list.static interfaceStrategy interface for checking entries in a map.static interfaceStrategy interface for checking elements in a set.static classStandard implementation ofKiwiCasts2.CollectionCheckStrategythat allows configuring the number of non-null and type checks to perform.static classStandard implementation ofKiwiCasts2.ListCheckStrategythat allows configuring the number of non-null and type checks to perform.static classStandard implementation ofKiwiCasts2.MapCheckStrategythat allows configuring the number of non-null and type checks to perform.static classStandard implementation ofKiwiCasts2.SetCheckStrategythat allows configuring the number of non-null and type checks to perform. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum number of non-null checks to perform when checking elements in a collection.static final intThe default maximum number of type checks to perform when checking elements in a collection. -
Method Summary
Modifier and TypeMethodDescriptionstatic <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.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.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.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.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.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.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.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.static <T> TuncheckedCast(Object object) Performs an unchecked cast of the given object to the specified type.
-
Field Details
-
DEFAULT_MAX_NON_NULL_CHECKS
public static final int DEFAULT_MAX_NON_NULL_CHECKSThe 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_CHECKSThe default maximum number of type checks to perform when checking elements in a collection.- See Also:
-
-
Method Details
-
uncheckedCast
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. UsesKiwiCasts2.DefaultCollectionCheckStrategyas the collection check strategy.- Type Parameters:
T- the expected element type- Parameters:
expectedType- the expected type of elements in the collectionobject- 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 collectionobject- the object to cast to a Collectionstrategy- 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
Casts the given object to a List and checks that its elements are of the expected type. UsesKiwiCasts2.DefaultListCheckStrategyas the list check strategy.- Type Parameters:
T- the expected element type- Parameters:
expectedType- the expected type of elements in the listobject- 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 listobject- the object to cast to a Liststrategy- 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
Casts the given object to a Set and checks that its elements are of the expected type. UsesKiwiCasts2.DefaultSetCheckStrategyas the set check strategy.- Type Parameters:
T- the expected element type- Parameters:
expectedType- the expected type of elements in the setobject- 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 setobject- the object to cast to a Setstrategy- 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. UsesKiwiCasts2.DefaultMapCheckStrategyas the map check strategy.- Type Parameters:
K- the expected key typeV- the expected value type- Parameters:
keyType- the expected type of keys in the mapvalueType- the expected type of values in the mapobject- 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 typeV- the expected value type- Parameters:
keyType- the expected type of keys in the mapvalueType- the expected type of values in the mapobject- the object to cast to a Mapstrategy- 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
-