Changeset 16184 in josm for trunk/src/org/openstreetmap/josm/tools/StringParser.java
- Timestamp:
- 2020-03-21T15:38:15+01:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/StringParser.java
r16181 r16184 2 2 package org.openstreetmap.josm.tools; 3 3 4 import java.util.LinkedHashMap;5 4 import java.util.Map; 6 import java.util.Optional;7 5 import java.util.function.Function; 8 6 … … 12 10 * @since 16181 13 11 */ 14 public class StringParser { 12 public class StringParser extends GenericParser<String> { 15 13 16 14 /** … … 38 36 .parsers)); 39 37 40 private final Map<Class<?>, Function<String, ?>> parsers;41 42 38 public StringParser() { 43 this(new LinkedHashMap<>());39 super(); 44 40 } 45 41 … … 50 46 */ 51 47 public StringParser(StringParser parser) { 52 this(new LinkedHashMap<>(parser.parsers));48 super(parser); 53 49 } 54 50 55 pr ivateStringParser(Map<Class<?>, Function<String, ?>> parsers) {56 this.parsers= parsers;51 protected StringParser(Map<Class<?>, Function<String, ?>> parsers) { 52 super(parsers); 57 53 } 58 54 55 @Override 59 56 public <T> StringParser registerParser(Class<T> type, Function<String, T> value) { 60 parsers.put(type, value);57 super.registerParser(type, value); 61 58 return this; 62 59 } 63 64 /**65 * Determines whether {@code type} can be {@linkplain #parse parsed}66 *67 * @param type the type68 * @return true if {@code type} can be parsed69 */70 public boolean supports(Class<?> type) {71 return parsers.containsKey(type);72 }73 74 /**75 * Parses the given {@code string} as {@code type} and returns the result76 *77 * @param type the type class78 * @param string the string to parse79 * @param <T> the type80 * @return the parsed value for {@code string} as type {@code type}81 * @throws UnsupportedOperationException if {@code type} is not {@linkplain #supports supported}82 * @throws UncheckedParseException when the parsing fails83 */84 @SuppressWarnings("unchecked")85 public <T> T parse(Class<T> type, String string) {86 final Function<String, ?> parser = parsers.get(type);87 if (parser == null) {88 throw new UnsupportedOperationException(type + " is not supported");89 }90 try {91 return (T) parser.apply(string);92 } catch (RuntimeException ex) {93 throw new UncheckedParseException("Failed to parse [" + string + "] as " + type, ex);94 }95 }96 97 /**98 * Tries to parse the given {@code string} as {@code type} and returns the result.99 *100 * @param type the type class101 * @param string the string to parse102 * @param <T> the type103 * @return the parsed value for {@code string} as type {@code type},104 * or {@code Optional.empty()} (if parsing fails, or the type is not {@linkplain #supports supported})105 */106 public <T> Optional<?> tryParse(Class<?> type, String string) {107 try {108 return Optional.ofNullable(parse(type, string));109 } catch (RuntimeException ex) {110 Logging.trace(ex);111 return Optional.empty();112 }113 }114 60 }
Note:
See TracChangeset
for help on using the changeset viewer.