public interface ValueType<T>
Handles parsing and casting of Strings and Objects to the destination type represented by an instance of a ValueType.
| Modifier and Type | Method and Description |
|---|---|
T |
cast(Object o)
Attempt to cast the object to the generic type T, which is a concrete class in subclasses.
|
Class<T> |
getDestinationType() |
T |
parse(String sourceValue)
Attempts to parse the passed String into the destinationType.
|
String |
toString(T value)
Converts an instance of the destination type to String.
|
T parse(String sourceValue) throws ParsingException
All trimming (removing whitespace from around a value) should be assumed to already have happened for the incoming sourceValue by a Trimmer. A null sourceValue always parses to null, so callers may optionally skip calling parse with a null value.
Implementations should be careful to ONLY throw a ParsingException - Integers and other types may throw other unchecked exceptions when trying to convert values, which should be handled in this method and rethrown as a ParsingException.
This method and toString(T) should be reversible: The object generated here should be toString-able to a String equal to the one passed this method. Though this may not be possible for Boolean types, which may have multiple possible string representations for True or False.
sourceValue - ParsingException - for any type of failure.String toString(T value)
value - T cast(Object o) throws RuntimeException
AndHow uses this internally to cast values known to already be of type T, but were stored in a generic way. If used for unknown types that are not castable to T, it will throw a RuntimeException, though this should never happen unless used by application code.
o - Object to castRuntimeExceptionCopyright © 2022. All rights reserved.