- Timestamp:
- 2020-03-21T15:38:15+01:00 (5 years ago)
- 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 13 13 import org.openstreetmap.josm.gui.mappaint.mapcss.CSSColors; 14 14 import org.openstreetmap.josm.tools.ColorHelper; 15 import org.openstreetmap.josm.tools.GenericParser; 15 16 import org.openstreetmap.josm.tools.Logging; 16 17 import org.openstreetmap.josm.tools.Utils; … … 26 27 27 28 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); 28 40 29 41 /** … … 137 149 return (T) o; 138 150 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(); 174 163 } 175 164 … … 185 174 } 186 175 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; 187 181 } 188 182 -
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.