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