Package org.openstreetmap.josm.tools
Class GenericParser<U>
- java.lang.Object
-
- org.openstreetmap.josm.tools.GenericParser<U>
-
- Type Parameters:
U- the type of the objects to parse
- Direct Known Subclasses:
StringParser
public class GenericParser<U> extends java.lang.Object
Utility class to parse various types from other values.- Since:
- 16184
-
-
Constructor Summary
Constructors Modifier Constructor Description GenericParser()Creates an emptyGenericParserprotectedGenericParser(java.util.Map<java.lang.Class<?>,java.util.function.Function<U,?>> parsers)Creates a newGenericParserwith the same parsers as the specified mapGenericParser(GenericParser<U> parser)Creates a newGenericParserby deeply copyingparser
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Tparse(java.lang.Class<T> type, U value)Parses the givenvalueastypeand returns the result<T> GenericParser<U>registerParser(java.lang.Class<T> type, java.util.function.Function<U,T> value)booleansupports(java.lang.Class<?> type)Determines whethertypecan be parsed<T> java.util.Optional<T>tryParse(java.lang.Class<T> type, U value)Tries to parse the givenvalueastypeand returns the result.
-
-
-
Constructor Detail
-
GenericParser
public GenericParser()
Creates an emptyGenericParser
-
GenericParser
public GenericParser(GenericParser<U> parser)
Creates a newGenericParserby deeply copyingparser- Parameters:
parser- the parser to copy
-
GenericParser
protected GenericParser(java.util.Map<java.lang.Class<?>,java.util.function.Function<U,?>> parsers)
Creates a newGenericParserwith the same parsers as the specified map- Parameters:
parsers- the parsers
-
-
Method Detail
-
registerParser
public <T> GenericParser<U> registerParser(java.lang.Class<T> type, java.util.function.Function<U,T> value)
-
supports
public boolean supports(java.lang.Class<?> type)
Determines whethertypecan be parsed- Parameters:
type- the type- Returns:
- true if
typecan be parsed
-
parse
public <T> T parse(java.lang.Class<T> type, U value)
Parses the givenvalueastypeand returns the result- Type Parameters:
T- the type- Parameters:
type- the type classvalue- the value to parse- Returns:
- the parsed value for
stringas typetype - Throws:
java.lang.UnsupportedOperationException- iftypeis not supportedUncheckedParseException- when the parsing fails
-
tryParse
public <T> java.util.Optional<T> tryParse(java.lang.Class<T> type, U value)
Tries to parse the givenvalueastypeand returns the result.- Type Parameters:
T- the type- Parameters:
type- the type classvalue- the value to parse- Returns:
- the parsed value for
valueas typetype, orOptional.empty()(if parsing fails, or the type is not supported)
-
-