Package org.kiwiproject.beta.base
Class TypeMismatchException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.kiwiproject.beta.base.TypeMismatchException
- All Implemented Interfaces:
Serializable
Exception thrown when a value cannot be cast to the expected type.
This exists mainly to provide more information in certain situations
than ClassCastException does. For example, when checking that
a List contains only Integer values, but a Double is found. Or when
checking that a Map contains the expected key and value types.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionClass<?>Gets the actual type of the value that caused the type mismatch.Class<?>Gets the expected type of the value that caused the type mismatch.static TypeMismatchExceptionforUnexpectedCollectionElementType(Class<?> collectionType, Class<?> expectedType, Class<?> actualType) Factory method to create a new instance with a standardized message for a type mismatch on a collection element.static TypeMismatchExceptionforUnexpectedMapKeyType(Class<?> expectedKeyType, Class<?> actualKeyType) Factory method to create a new instance with a standardized message for a map key type mismatch.static TypeMismatchExceptionforUnexpectedMapValueType(Class<?> expectedValueType, Class<?> actualValueType) Factory method to create a new instance with a standardized message for a map value type mismatch.static TypeMismatchExceptionforUnexpectedType(Class<?> expectedType, Class<?> actualType) Factory method to create a new instance with a standardized message for a type mismatch and no cause.static TypeMismatchExceptionforUnexpectedTypeWithCause(Class<?> expectedType, Class<?> actualType, ClassCastException cause) Factory method to create a new instance with a standardized message for a type mismatch.static TypeMismatchExceptionforUnexpectedTypeWithMessage(String message, Class<?> expectedType, Class<?> actualType, ClassCastException cause) Factory method to create a new instance ofTypeMismatchExceptionwith a custom message, expected type, actual type, and an optional cause.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Method Details
-
expectedType
Gets the expected type of the value that caused the type mismatch.- Returns:
- the Class object representing the expected type
-
actualType
Gets the actual type of the value that caused the type mismatch.- Returns:
- the Class object representing the actual type
-
forUnexpectedType
Factory method to create a new instance with a standardized message for a type mismatch and no cause.- Parameters:
expectedType- the expected type of the valueactualType- the actual type of the value- Returns:
- a new instance with a descriptive message
-
forUnexpectedTypeWithCause
public static TypeMismatchException forUnexpectedTypeWithCause(Class<?> expectedType, Class<?> actualType, ClassCastException cause) Factory method to create a new instance with a standardized message for a type mismatch.- Parameters:
expectedType- the expected type of the valueactualType- the actual type of the valuecause- the ClassCastException that occurred during the cast attempt- Returns:
- a new instance with a descriptive message
-
forUnexpectedCollectionElementType
public static TypeMismatchException forUnexpectedCollectionElementType(Class<?> collectionType, Class<?> expectedType, Class<?> actualType) Factory method to create a new instance with a standardized message for a type mismatch on a collection element. The returned exception has no cause.- Parameters:
collectionType- the type of collection, such as Collection, List, or SetexpectedType- the expected type of the collection elementsactualType- the actual type found in the collection- Returns:
- a new instance with a descriptive message
-
forUnexpectedMapKeyType
public static TypeMismatchException forUnexpectedMapKeyType(Class<?> expectedKeyType, Class<?> actualKeyType) Factory method to create a new instance with a standardized message for a map key type mismatch. The returned exception has no cause.- Parameters:
expectedKeyType- the expected type of keys in the mapactualKeyType- the actual type of key found in the map- Returns:
- a new instance with a descriptive message
-
forUnexpectedMapValueType
public static TypeMismatchException forUnexpectedMapValueType(Class<?> expectedValueType, Class<?> actualValueType) Factory method to create a new instance with a standardized message for a map value type mismatch. The returned exception has no cause.- Parameters:
expectedValueType- the expected type of values in the mapactualValueType- the actual type of value found in the map- Returns:
- a new instance with a descriptive message
-
forUnexpectedTypeWithMessage
public static TypeMismatchException forUnexpectedTypeWithMessage(String message, Class<?> expectedType, Class<?> actualType, ClassCastException cause) Factory method to create a new instance ofTypeMismatchExceptionwith a custom message, expected type, actual type, and an optional cause.- Parameters:
message- a custom descriptive message explaining the type mismatchexpectedType- the expected type of the valueactualType- the actual type of the valuecause- the (optional)ClassCastExceptionthat occurred during the cast attempt- Returns:
- a new instance of
TypeMismatchExceptionrepresenting the type mismatch
-