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 emptyGenericParser
protected
GenericParser(java.util.Map<java.lang.Class<?>,java.util.function.Function<U,?>> parsers)
Creates a newGenericParser
with the same parsers as the specified mapGenericParser(GenericParser<U> parser)
Creates a newGenericParser
by deeply copyingparser
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
parse(java.lang.Class<T> type, U value)
Parses the givenvalue
astype
and returns the result<T> GenericParser<U>
registerParser(java.lang.Class<T> type, java.util.function.Function<U,T> value)
boolean
supports(java.lang.Class<?> type)
Determines whethertype
can be parsed<T> java.util.Optional<T>
tryParse(java.lang.Class<T> type, U value)
Tries to parse the givenvalue
astype
and returns the result.
-
-
-
Constructor Detail
-
GenericParser
public GenericParser()
Creates an emptyGenericParser
-
GenericParser
public GenericParser(GenericParser<U> parser)
Creates a newGenericParser
by 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 newGenericParser
with 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 whethertype
can be parsed- Parameters:
type
- the type- Returns:
- true if
type
can be parsed
-
parse
public <T> T parse(java.lang.Class<T> type, U value)
Parses the givenvalue
astype
and returns the result- Type Parameters:
T
- the type- Parameters:
type
- the type classvalue
- the value to parse- Returns:
- the parsed value for
string
as typetype
- Throws:
java.lang.UnsupportedOperationException
- iftype
is 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 givenvalue
astype
and returns the result.- Type Parameters:
T
- the type- Parameters:
type
- the type classvalue
- the value to parse- Returns:
- the parsed value for
value
as typetype
, orOptional.empty()
(if parsing fails, or the type is not supported)
-
-