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

Last change on this file since 10145 was 10045, checked in by Don-vip, 8 years ago

sonar - squid:S2129 - Constructors should not be used to instantiate "String" and primitive-wrapper classes

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