Changeset 3856 in josm for trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java
- Timestamp:
- 2011-02-05T14:07:23+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java
r3848 r3856 5 5 import java.util.Arrays; 6 6 import java.util.HashMap; 7 import java.util.List; 7 8 import java.util.Map; 8 9 … … 26 27 * value, def otherwise 27 28 */ 28 public <T> T get(String key, T def, Class klass) {29 public <T> T get(String key, T def, Class<T> klass) { 29 30 if (def != null && !klass.isInstance(def)) 30 31 throw new IllegalArgumentException(); … … 32 33 if (o == null) 33 34 return def; 34 if (klass.isInstance(o)) { 35 @SuppressWarnings("unchecked") T res = (T) klass.cast(o); 35 T res = convertTo(o, klass); 36 if (res == null) { 37 System.err.println(String.format("Warning: unable to convert property %s to type %s: found %s of type %s!", key, klass, o, o.getClass())); 38 return def; 39 } else 36 40 return res; 37 }38 System.err.println(String.format("Warning: wrong type for mappaint property %s: %s expected, but %s of type %s found!", key, klass, o, o.getClass()));39 return def;40 41 } 41 42 … … 44 45 } 45 46 46 public Float getFloat(String key, Float def) {47 Object o = prop.get(key);47 @SuppressWarnings("unchecked") 48 public static <T> T convertTo(Object o, Class<T> klass) { 48 49 if (o == null) 49 return def; 50 return null; 51 if (klass.isInstance(o)) 52 return (T) o; 53 54 if (klass == float.class || klass == Float.class) 55 return (T) toFloat(o); 56 57 if (klass == double.class || klass == Double.class) { 58 o = toFloat(o); 59 if (o != null) { 60 o = new Double((Float) o); 61 } 62 return (T) o; 63 } 64 65 if (klass == boolean.class || klass == Boolean.class) 66 return (T) toBool(o); 67 68 if (klass == float[].class) { 69 return (T) toFloatArray(o); 70 } 71 72 if (klass == Color.class) { 73 return (T) toColor(o); 74 } 75 return null; 76 } 77 78 private static Float toFloat(Object o) { 50 79 if (o instanceof Float) 51 80 return (Float) o; 81 if (o instanceof Double) 82 return new Float((Double) o); 52 83 if (o instanceof Integer) 53 84 return new Float((Integer) o); 54 return def; 85 if (o instanceof String) { 86 try { 87 float f = Float.parseFloat((String) o); 88 return f; 89 } catch (NumberFormatException e) { 90 } 91 } 92 return null; 55 93 } 56 94 57 public Color getColor(String key, Color def) { 58 Object o = prop.get(key); 59 if (o == null) 60 return def; 95 private static Boolean toBool(Object o) { 96 if (o instanceof Boolean) 97 return (Boolean) o; 98 if (o instanceof String) 99 return Boolean.parseBoolean((String) o); 100 return null; 101 } 102 103 private static float[] toFloatArray(Object o) { 104 if (o instanceof float[]) 105 return (float[]) o; 106 if (o instanceof List) { 107 List l = (List) o; 108 float[] a = new float[l.size()]; 109 for (int i=0; i<l.size(); ++i) { 110 Float f = toFloat(l.get(i)); 111 if (f == null) 112 return null; 113 else 114 a[i] = f; 115 } 116 return a; 117 } 118 return null; 119 } 120 121 public static Color toColor(Object o) { 61 122 if (o instanceof Color) 62 123 return (Color) o; 63 if (o instanceof String) { 64 Color clr = CSSColors.get((String) o); 65 if (clr != null) 66 return clr; 67 else 68 return def; 69 } 70 return def; 124 if (o instanceof String) 125 return CSSColors.get((String) o); 126 return null; 71 127 } 72 128 … … 111 167 return res.append("}").toString(); 112 168 } 169 170 public boolean containsKey(String key) { 171 return prop.containsKey(key); 172 } 113 173 }
Note:
See TracChangeset
for help on using the changeset viewer.