1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.Map;
|
---|
5 | import java.util.function.Function;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Utility class to parse various types from strings.
|
---|
9 | *
|
---|
10 | * @since 16181
|
---|
11 | */
|
---|
12 | public class StringParser extends GenericParser<String> {
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * The default instance supports parsing {@link String}, {@link Character}, {@link Boolean},
|
---|
16 | * {@link Byte}, {@link Short}, {@link Integer}, {@link Long}, {@link Float}, {@link Double} (in their primitive and boxed form).
|
---|
17 | */
|
---|
18 | public static final StringParser DEFAULT = new StringParser(Utils.toUnmodifiableMap(new StringParser()
|
---|
19 | .registerParser(String.class, Function.identity())
|
---|
20 | .registerParser(char.class, value -> value.charAt(0))
|
---|
21 | .registerParser(Character.class, value -> value.charAt(0))
|
---|
22 | .registerParser(boolean.class, Boolean::parseBoolean)
|
---|
23 | .registerParser(Boolean.class, Boolean::parseBoolean)
|
---|
24 | .registerParser(byte.class, Byte::parseByte)
|
---|
25 | .registerParser(Byte.class, Byte::parseByte)
|
---|
26 | .registerParser(short.class, Short::parseShort)
|
---|
27 | .registerParser(Short.class, Short::parseShort)
|
---|
28 | .registerParser(int.class, Integer::parseInt)
|
---|
29 | .registerParser(Integer.class, Integer::parseInt)
|
---|
30 | .registerParser(long.class, Long::parseLong)
|
---|
31 | .registerParser(Long.class, Long::parseLong)
|
---|
32 | .registerParser(float.class, Float::parseFloat)
|
---|
33 | .registerParser(Float.class, Float::parseFloat)
|
---|
34 | .registerParser(double.class, Double::parseDouble)
|
---|
35 | .registerParser(Double.class, Double::parseDouble)
|
---|
36 | .parsers));
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Creates an empty {@code StringParser}
|
---|
40 | */
|
---|
41 | public StringParser() {
|
---|
42 | super();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates a new {@code StringParser} by deeply copying {@code parser}
|
---|
47 | *
|
---|
48 | * @param parser the parser to copy
|
---|
49 | */
|
---|
50 | public StringParser(StringParser parser) {
|
---|
51 | super(parser);
|
---|
52 | }
|
---|
53 |
|
---|
54 | private StringParser(Map<Class<?>, Function<String, ?>> parsers) {
|
---|
55 | super(parsers);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public <T> StringParser registerParser(Class<T> type, Function<String, T> value) {
|
---|
60 | super.registerParser(type, value);
|
---|
61 | return this;
|
---|
62 | }
|
---|
63 | }
|
---|