1 /*--------------------------------------------------------------------------
2 * Copyright 2007 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 // GenomeBrowser Project
18 //
19 // Properties.java
20 // Since: Jul 20, 2007
21 //
22 // $URL$
23 // $Author$
24 //--------------------------------------
25 package org.utgenome.gwt.utgb.client.util;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /**
31 * This class is an wrapper of the {@link HashMap}, which makes easier to convert associated values with keys into
32 * primitive types, such as int, boolean, String etc.
33 *
34 * @author leo
35 *
36 */
37 public class Properties extends HashMap<String, String> {
38
39 /**
40 *
41 */
42 private static final long serialVersionUID = 1L;
43
44 public Properties() {
45
46 }
47
48 public Properties(Map<String, String> property) {
49 this.putAll(property);
50 }
51
52 public void add(String key, String value) {
53 super.put(key, value);
54 }
55
56 public void add(String key, int value) {
57 super.put(key, Integer.toString(value));
58 }
59
60 public void add(String key, long value) {
61 super.put(key, Long.toString(value));
62 }
63
64 public void add(String key, float value) {
65 super.put(key, Float.toString(value));
66 }
67
68 public void add(String key, boolean value) {
69 super.put(key, Boolean.toString(value));
70 }
71
72 public String get(String key) {
73 return super.get(key);
74 }
75
76 /**
77 * Gets the String value associated with the given key. If any corresponding value is not found, returns the default
78 * value.
79 *
80 * @param key
81 * the key
82 * @param defaultValue
83 * the default value
84 * @return the string value
85 */
86 public String get(String key, String defaultValue) {
87 if (containsKey(key))
88 return get(key);
89 else
90 return defaultValue;
91 }
92
93 public int getInt(String key) {
94 return StringUtil.toInt(super.get(key));
95 }
96
97 /**
98 * Gets the integer value associated with the given key. If any corresponding value is not found, returns the given
99 * default value
100 *
101 * @param key
102 * @param defaultValue
103 * @return
104 */
105 public int getInt(String key, int defaultValue) {
106 return containsKey(key) ? getInt(key) : defaultValue;
107 }
108
109 public float getFloat(String key) {
110 return Float.parseFloat(super.get(key));
111 }
112
113 /**
114 * Gets the float value associated with the given key. If any corresponding value is not found, returns the given
115 * default value
116 *
117 * @param key
118 * @param defaultValue
119 * @return
120 */
121 public float getFloat(String key, float defaultValue) {
122 return containsKey(key) ? getFloat(key) : defaultValue;
123 }
124
125 public boolean getBoolean(String key) {
126 String value = get(key);
127 if (value != null) {
128 if (value.equals("true"))
129 return true;
130 }
131 return false;
132 }
133
134 /**
135 * Gets the boolean value associated with the given key. If any corresponding value is not found, returns the given
136 * default value
137 *
138 * @param key
139 * @param defaultValue
140 * @return
141 */
142 public boolean getBoolean(String key, boolean defaultValue) {
143 return containsKey(key) ? getBoolean(key) : defaultValue;
144 }
145
146 }