1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.gwt.utgb.client.util;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30
31
32
33
34
35
36 public class CanonicalProperties extends HashMap<String, String> {
37
38
39
40
41 private static final long serialVersionUID = 1L;
42
43 private static HashMap<String, String> canonicalNameTable = new HashMap<String, String>();
44 private static HashMap<String, String> naturalNameTable = new HashMap<String, String>();
45
46 public CanonicalProperties() {
47 super();
48 }
49
50 public static String toCanonicalName(String key) {
51 if (key == null)
52 return key;
53
54 if (!canonicalNameTable.containsKey(key)) {
55 String cKey = key.replaceAll("[\\s-_]", "");
56 cKey = cKey.toLowerCase();
57 canonicalNameTable.put(key, cKey);
58 naturalNameTable.put(cKey, toNaturalName(key));
59 }
60 return canonicalNameTable.get(key);
61 }
62
63 public static String toNaturalName(String keyName) {
64 if (keyName == null)
65 return null;
66
67 String nName = naturalNameTable.get(keyName);
68 if (nName == null) {
69 ArrayList<String> components = new ArrayList<String>();
70 int start = 0;
71 int cursor = 0;
72 while (cursor < keyName.length()) {
73 while (cursor < keyName.length() && Character.isUpperCase(keyName.charAt(cursor))) {
74 cursor++;
75 }
76 if ((cursor - start) >= 2) {
77 components.add(keyName.substring(start, cursor));
78 start = cursor;
79 continue;
80 }
81 while (cursor < keyName.length()) {
82 char c = keyName.charAt(cursor);
83 if (isSplitChar(c)) {
84 break;
85 }
86 cursor++;
87 }
88 if (start < cursor) {
89 components.add(keyName.substring(start, cursor).toLowerCase());
90 }
91 else
92 cursor++;
93 start = cursor;
94 }
95 nName = StringUtil.join(components, " ");
96 naturalNameTable.put(keyName, nName);
97 }
98 return nName;
99 }
100
101 @Override
102 public void putAll(Map<? extends String, ? extends String> m) {
103 for (Entry<? extends String, ? extends String> e : m.entrySet()) {
104 put(e.getKey(), e.getValue());
105 }
106 }
107
108 @Override
109 public String put(String key, String value) {
110 return super.put(toCanonicalName(key), value);
111 }
112
113 private static boolean isSplitChar(char c) {
114 return Character.isUpperCase(c) || c == '_' || c == '-' || c == ' ';
115 }
116
117 public void add(String key, String value) {
118 put(key, value);
119 }
120
121 public void add(String key, int value) {
122 put(key, Integer.toString(value));
123 }
124
125 public void add(String key, long value) {
126 put(key, Long.toString(value));
127 }
128
129 public void add(String key, float value) {
130 put(key, Float.toString(value));
131 }
132
133 public void add(String key, boolean value) {
134 put(key, Boolean.toString(value));
135 }
136
137 public String get(String key) {
138 return super.get(toCanonicalName(key));
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public String get(String key, String defaultValue) {
152
153 String cKey = toCanonicalName(key);
154
155 if (containsKey(cKey))
156 return get(cKey);
157 else
158 return defaultValue;
159 }
160
161 public int getInt(String key) {
162 return StringUtil.toInt(get(key));
163 }
164
165
166
167
168
169
170
171
172
173 public int getInt(String key, int defaultValue) {
174 String cKey = toCanonicalName(key);
175 return containsKey(cKey) ? getInt(cKey) : defaultValue;
176 }
177
178 public float getFloat(String key) {
179 return Float.parseFloat(get(key));
180 }
181
182
183
184
185
186
187
188
189
190 public float getFloat(String key, float defaultValue) {
191 String cKey = toCanonicalName(key);
192 return containsKey(cKey) ? getFloat(cKey) : defaultValue;
193 }
194
195 public boolean getBoolean(String key) {
196 String value = get(key);
197 if (value != null) {
198 if (value.equals("true"))
199 return true;
200 }
201 return false;
202 }
203
204
205
206
207
208
209
210
211
212 public boolean getBoolean(String key, boolean defaultValue) {
213 String cKey = toCanonicalName(key);
214 return containsKey(cKey) ? getBoolean(cKey) : defaultValue;
215 }
216
217 }