Ignore:
Timestamp:
2020-03-21T15:38:15+01:00 (4 years ago)
Author:
simon04
Message:

see #18954, see #18864 - Introduce GenericParser for Cascade.convertTo

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/StringParser.java

    r16181 r16184  
    22package org.openstreetmap.josm.tools;
    33
    4 import java.util.LinkedHashMap;
    54import java.util.Map;
    6 import java.util.Optional;
    75import java.util.function.Function;
    86
     
    1210 * @since 16181
    1311 */
    14 public class StringParser {
     12public class StringParser extends GenericParser<String> {
    1513
    1614    /**
     
    3836            .parsers));
    3937
    40     private final Map<Class<?>, Function<String, ?>> parsers;
    41 
    4238    public StringParser() {
    43         this(new LinkedHashMap<>());
     39        super();
    4440    }
    4541
     
    5046     */
    5147    public StringParser(StringParser parser) {
    52         this(new LinkedHashMap<>(parser.parsers));
     48        super(parser);
    5349    }
    5450
    55     private StringParser(Map<Class<?>, Function<String, ?>> parsers) {
    56         this.parsers = parsers;
     51    protected StringParser(Map<Class<?>, Function<String, ?>> parsers) {
     52        super(parsers);
    5753    }
    5854
     55    @Override
    5956    public <T> StringParser registerParser(Class<T> type, Function<String, T> value) {
    60         parsers.put(type, value);
     57        super.registerParser(type, value);
    6158        return this;
    6259    }
    63 
    64     /**
    65      * Determines whether {@code type} can be {@linkplain #parse parsed}
    66      *
    67      * @param type the type
    68      * @return true if {@code type} can be parsed
    69      */
    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 result
    76      *
    77      * @param type   the type class
    78      * @param string the string to parse
    79      * @param <T>    the type
    80      * @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 fails
    83      */
    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 class
    101      * @param string the string to parse
    102      * @param <T>    the type
    103      * @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     }
    11460}
Note: See TracChangeset for help on using the changeset viewer.