View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // CanonicalProperties.java
20  // Since: Aug 3, 2010
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   * Using canonical parameter keys for providing case and space insensitive property map.
32   * 
33   * @author leo
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 	 * Gets the String value associated with the given key. If any corresponding value is not found, returns the default
143 	 * value.
144 	 * 
145 	 * @param key
146 	 *            the key
147 	 * @param defaultValue
148 	 *            the default value
149 	 * @return the string value
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 	 * Gets the integer value associated with the given key. If any corresponding value is not found, returns the given
167 	 * default value
168 	 * 
169 	 * @param key
170 	 * @param defaultValue
171 	 * @return
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 	 * Gets the float value associated with the given key. If any corresponding value is not found, returns the given
184 	 * default value
185 	 * 
186 	 * @param key
187 	 * @param defaultValue
188 	 * @return
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 	 * Gets the boolean value associated with the given key. If any corresponding value is not found, returns the given
206 	 * default value
207 	 * 
208 	 * @param key
209 	 * @param defaultValue
210 	 * @return
211 	 */
212 	public boolean getBoolean(String key, boolean defaultValue) {
213 		String cKey = toCanonicalName(key);
214 		return containsKey(cKey) ? getBoolean(cKey) : defaultValue;
215 	}
216 
217 }