1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.regex.Pattern;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.mapcss.CSSColors;
|
---|
15 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Simple map of properties with dynamic typing.
|
---|
20 | */
|
---|
21 | public final class Cascade implements Cloneable {
|
---|
22 |
|
---|
23 | public static final Cascade EMPTY_CASCADE = new Cascade();
|
---|
24 |
|
---|
25 | protected Map<String, Object> prop = new HashMap<>();
|
---|
26 |
|
---|
27 | private static final Pattern HEX_COLOR_PATTERN = Pattern.compile("#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})");
|
---|
28 |
|
---|
29 | public <T> T get(String key, T def, Class<T> klass) {
|
---|
30 | return get(key, def, klass, false);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Get value for the given key
|
---|
35 | * @param <T> the expected type
|
---|
36 | * @param key the key
|
---|
37 | * @param def default value, can be null
|
---|
38 | * @param klass the same as T
|
---|
39 | * @param suppressWarnings show or don't show a warning when some value is
|
---|
40 | * found, but cannot be converted to the requested type
|
---|
41 | * @return if a value with class klass has been mapped to key, returns this
|
---|
42 | * value, def otherwise
|
---|
43 | */
|
---|
44 | public <T> T get(String key, T def, Class<T> klass, boolean suppressWarnings) {
|
---|
45 | if (def != null && !klass.isInstance(def))
|
---|
46 | throw new IllegalArgumentException();
|
---|
47 | Object o = prop.get(key);
|
---|
48 | if (o == null)
|
---|
49 | return def;
|
---|
50 | T res = convertTo(o, klass);
|
---|
51 | if (res == null) {
|
---|
52 | if (!suppressWarnings) {
|
---|
53 | Main.warn(String.format("Unable to convert property %s to type %s: found %s of type %s!", key, klass, o, o.getClass()));
|
---|
54 | }
|
---|
55 | return def;
|
---|
56 | } else
|
---|
57 | return res;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public Object get(String key) {
|
---|
61 | return prop.get(key);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void put(String key, Object val) {
|
---|
65 | prop.put(key, val);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void putOrClear(String key, Object val) {
|
---|
69 | if (val != null) {
|
---|
70 | prop.put(key, val);
|
---|
71 | } else {
|
---|
72 | prop.remove(key);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void remove(String key) {
|
---|
77 | prop.remove(key);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @SuppressWarnings("unchecked")
|
---|
81 | public static <T> T convertTo(Object o, Class<T> klass) {
|
---|
82 | if (o == null)
|
---|
83 | return null;
|
---|
84 | if (klass.isInstance(o))
|
---|
85 | return (T) o;
|
---|
86 |
|
---|
87 | if (klass == float.class || klass == Float.class)
|
---|
88 | return (T) toFloat(o);
|
---|
89 |
|
---|
90 | if (klass == double.class || klass == Double.class) {
|
---|
91 | o = toFloat(o);
|
---|
92 | if (o != null) {
|
---|
93 | o = new Double((Float) o);
|
---|
94 | }
|
---|
95 | return (T) o;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (klass == boolean.class || klass == Boolean.class)
|
---|
99 | return (T) toBool(o);
|
---|
100 |
|
---|
101 | if (klass == float[].class)
|
---|
102 | return (T) toFloatArray(o);
|
---|
103 |
|
---|
104 | if (klass == Color.class)
|
---|
105 | return (T) toColor(o);
|
---|
106 |
|
---|
107 | if (klass == String.class) {
|
---|
108 | if (o instanceof Keyword)
|
---|
109 | return (T) ((Keyword) o).val;
|
---|
110 |
|
---|
111 | return (T) o.toString();
|
---|
112 | }
|
---|
113 |
|
---|
114 | return null;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private static Float toFloat(Object o) {
|
---|
118 | if (o instanceof Number)
|
---|
119 | return ((Number) o).floatValue();
|
---|
120 | if (o instanceof String && !((String) o).isEmpty()) {
|
---|
121 | try {
|
---|
122 | return Float.parseFloat((String) o);
|
---|
123 | } catch (NumberFormatException e) {
|
---|
124 | Main.debug("'"+o+"' cannot be converted to float");
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | private static Boolean toBool(Object o) {
|
---|
131 | if (o instanceof Boolean)
|
---|
132 | return (Boolean) o;
|
---|
133 | if (o instanceof Keyword) {
|
---|
134 | String s = ((Keyword) o).val;
|
---|
135 | if (equal(s, "true") || equal(s, "yes"))
|
---|
136 | return true;
|
---|
137 | if (equal(s, "false") || equal(s, "no"))
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 | return null;
|
---|
141 | }
|
---|
142 |
|
---|
143 | private static float[] toFloatArray(Object o) {
|
---|
144 | if (o instanceof float[])
|
---|
145 | return (float[]) o;
|
---|
146 | if (o instanceof List) {
|
---|
147 | List<?> l = (List<?>) o;
|
---|
148 | float[] a = new float[l.size()];
|
---|
149 | for (int i=0; i<l.size(); ++i) {
|
---|
150 | Float f = toFloat(l.get(i));
|
---|
151 | if (f == null)
|
---|
152 | return null;
|
---|
153 | else
|
---|
154 | a[i] = f;
|
---|
155 | }
|
---|
156 | return a;
|
---|
157 | }
|
---|
158 | Float f = toFloat(o);
|
---|
159 | if (f != null)
|
---|
160 | return new float[] { f };
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | private static Color toColor(Object o) {
|
---|
165 | if (o instanceof Color)
|
---|
166 | return (Color) o;
|
---|
167 | if (o instanceof Keyword)
|
---|
168 | return CSSColors.get(((Keyword) o).val);
|
---|
169 | if (o instanceof String) {
|
---|
170 | Color c = CSSColors.get((String) o);
|
---|
171 | if (c != null)
|
---|
172 | return c;
|
---|
173 | if (HEX_COLOR_PATTERN.matcher((String) o).matches()) {
|
---|
174 | return ColorHelper.html2color((String) o);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | return null;
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | public Cascade clone() {
|
---|
182 | @SuppressWarnings("unchecked")
|
---|
183 | HashMap<String, Object> clonedProp = (HashMap) ((HashMap) this.prop).clone();
|
---|
184 | Cascade c = new Cascade();
|
---|
185 | c.prop = clonedProp;
|
---|
186 | return c;
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public String toString() {
|
---|
191 | StringBuilder res = new StringBuilder("Cascade{ ");
|
---|
192 | for (String key : prop.keySet()) {
|
---|
193 | res.append(key+":");
|
---|
194 | Object val = prop.get(key);
|
---|
195 | if (val instanceof float[]) {
|
---|
196 | res.append(Arrays.toString((float[]) val));
|
---|
197 | } else if (val instanceof Color) {
|
---|
198 | res.append(Utils.toString((Color)val));
|
---|
199 | } else if (val != null) {
|
---|
200 | res.append(val.toString());
|
---|
201 | }
|
---|
202 | res.append("; ");
|
---|
203 | }
|
---|
204 | return res.append("}").toString();
|
---|
205 | }
|
---|
206 |
|
---|
207 | public boolean containsKey(String key) {
|
---|
208 | return prop.containsKey(key);
|
---|
209 | }
|
---|
210 | }
|
---|