source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java@ 6142

Last change on this file since 6142 was 6142, checked in by Don-vip, 11 years ago

see #8902 - fix compilation warnings

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