Changeset 16184 in josm for trunk/src


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

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

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    r14439 r16184  
    1313import org.openstreetmap.josm.gui.mappaint.mapcss.CSSColors;
    1414import org.openstreetmap.josm.tools.ColorHelper;
     15import org.openstreetmap.josm.tools.GenericParser;
    1516import org.openstreetmap.josm.tools.Logging;
    1617import org.openstreetmap.josm.tools.Utils;
     
    2627
    2728    private static final Pattern HEX_COLOR_PATTERN = Pattern.compile("#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})");
     29
     30    private static final GenericParser<Object> GENERIC_PARSER = new GenericParser<>()
     31            .registerParser(float.class, Cascade::toFloat)
     32            .registerParser(Float.class, Cascade::toFloat)
     33            .registerParser(double.class, Cascade::toDouble)
     34            .registerParser(Double.class, Cascade::toDouble)
     35            .registerParser(boolean.class, Cascade::toBool)
     36            .registerParser(Boolean.class, Cascade::toBool)
     37            .registerParser(float[].class, Cascade::toFloatArray)
     38            .registerParser(Color.class, Cascade::toColor)
     39            .registerParser(String.class, Cascade::toString);
    2840
    2941    /**
     
    137149            return (T) o;
    138150
    139         if (klass == float.class || klass == Float.class)
    140             return (T) toFloat(o);
    141 
    142         if (klass == double.class || klass == Double.class) {
    143             o = toFloat(o);
    144             if (o != null) {
    145                 o = Double.valueOf((Float) o);
    146             }
    147             return (T) o;
    148         }
    149 
    150         if (klass == boolean.class || klass == Boolean.class)
    151             return (T) toBool(o);
    152 
    153         if (klass == float[].class)
    154             return (T) toFloatArray(o);
    155 
    156         if (klass == Color.class)
    157             return (T) toColor(o);
    158 
    159         if (klass == String.class) {
    160             if (o instanceof Keyword)
    161                 return (T) ((Keyword) o).val;
    162             if (o instanceof Color) {
    163                 Color c = (Color) o;
    164                 int alpha = c.getAlpha();
    165                 if (alpha != 255)
    166                     return (T) String.format("#%06x%02x", ((Color) o).getRGB() & 0x00ffffff, alpha);
    167                 return (T) String.format("#%06x", ((Color) o).getRGB() & 0x00ffffff);
    168             }
    169 
    170             return (T) o.toString();
    171         }
    172 
    173         return null;
     151        return GENERIC_PARSER.supports(klass)
     152                ? GENERIC_PARSER.parse(klass, o)
     153                : null;
     154    }
     155
     156    private static String toString(Object o) {
     157        if (o instanceof Keyword)
     158            return ((Keyword) o).val;
     159        if (o instanceof Color) {
     160            return ColorHelper.color2html((Color) o);
     161        }
     162        return o.toString();
    174163    }
    175164
     
    185174        }
    186175        return null;
     176    }
     177
     178    private static Double toDouble(Object o) {
     179        final Float number = toFloat(o);
     180        return number != null ? Double.valueOf(number) : null;
    187181    }
    188182
  • 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.