001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portlet.util;
023:
024: import java.util.Map;
025:
026: /** @author <a href="theute@jboss.org">Thomas Heute </a> $Revision: 8786 $ */
027: public class Parameters {
028:
029: private Map parameters;
030:
031: /** @param parameterMap */
032: public Parameters(Map parameterMap) {
033: this .parameters = parameterMap;
034: }
035:
036: public String getParameter(String name) {
037: if (name == null) {
038: return null;
039: }
040: String[] value = (String[]) parameters.get(name);
041: return value == null ? null : value[0];
042: }
043:
044: public String get(String key, String def) {
045: String value = getParameter(key);
046: if (value != null) {
047: return value;
048: } else {
049: return def;
050: }
051: }
052:
053: /**
054: * Returns the value as a boolean
055: *
056: * @param key Key of the parameter
057: * @param def Default value if value is different from true or false
058: * @return boolean value for the string "true" or "false" (not sensitive to uppercase/lowercase, and leading/trailing
059: * spaces).
060: */
061: public boolean getBoolean(String key, boolean def) {
062:
063: String value = getParameter(key);
064: if (value != null) {
065: if ("true".equalsIgnoreCase(value.trim())) {
066: return true;
067: } else if ("false".equalsIgnoreCase(value.trim())) {
068: return false;
069: } else {
070: return def;
071: }
072: } else {
073: return def;
074: }
075: }
076:
077: public Boolean getBooleanObject(String key, boolean def) {
078: Boolean bool = getBooleanObject(key);
079: return (bool != null) ? bool : new Boolean(def);
080: }
081:
082: public Boolean getBooleanObject(String key) {
083:
084: String value = getParameter(key);
085: if (value != null) {
086: if ("true".equalsIgnoreCase(value.trim())) {
087: return Boolean.TRUE;
088: } else if ("false".equalsIgnoreCase(value.trim())) {
089: return Boolean.FALSE;
090: } else {
091: return null;
092: }
093: } else {
094: return null;
095: }
096: }
097:
098: public byte[] getByteArray(String key, byte[] def) {
099: String value = getParameter(key);
100: byte[] returnValue = def;
101: if (value != null) {
102: returnValue = value.getBytes();
103: if (returnValue == null) {
104: returnValue = def;
105: }
106: return returnValue;
107: } else {
108: return def;
109: }
110: }
111:
112: public double getDouble(String key, double def) {
113: String value = getParameter(key);
114: double returnValue = def;
115: if (value != null) {
116: try {
117: returnValue = Double.parseDouble(value);
118: return returnValue;
119: } catch (NumberFormatException e) {
120: return def;
121: }
122: } else {
123: return def;
124: }
125: }
126:
127: public Double getDoubleObject(String key, double defaultValue) {
128: Double value = getDoubleObject(key);
129: return value != null ? value : new Double(defaultValue);
130: }
131:
132: public Double getDoubleObject(String key) {
133: try {
134: String value = getParameter(key);
135: return (value != null) ? new Double(value) : null;
136: } catch (NumberFormatException e) {
137: return null;
138: }
139: }
140:
141: public float getFloat(String key, float def) {
142: String value = getParameter(key);
143: float returnValue = def;
144: if (value != null) {
145: try {
146: returnValue = Float.parseFloat(value);
147: return returnValue;
148: } catch (NumberFormatException e) {
149: return def;
150: }
151: } else {
152: return def;
153: }
154: }
155:
156: public Float getFloatObject(String key, float defaultValue) {
157: Float value = getFloatObject(key);
158: return value != null ? value : new Float(defaultValue);
159: }
160:
161: public Float getFloatObject(String key) {
162: try {
163: String value = getParameter(key);
164: return (value != null) ? new Float(value) : null;
165: } catch (NumberFormatException e) {
166: return null;
167: }
168: }
169:
170: public short getShort(String key, short def) {
171: String value = getParameter(key);
172: short returnValue = def;
173: if (value != null) {
174: try {
175: returnValue = Short.parseShort(value);
176: return returnValue;
177: } catch (NumberFormatException e) {
178: return def;
179: }
180: } else {
181: return def;
182: }
183: }
184:
185: public Short getShortObject(String key, short defaultValue) {
186: Short value = getShortObject(key);
187: return value != null ? value : new Short(defaultValue);
188: }
189:
190: public Short getShortObject(String key) {
191: try {
192: String value = getParameter(key);
193: return (value != null) ? new Short(value) : null;
194: } catch (NumberFormatException e) {
195: return null;
196: }
197: }
198:
199: public int getInt(String key, int def) {
200: String value = getParameter(key);
201: int returnValue = def;
202: if (value != null) {
203: try {
204: returnValue = Integer.parseInt(value);
205: return returnValue;
206: } catch (NumberFormatException e) {
207: return def;
208: }
209: } else {
210: return def;
211: }
212: }
213:
214: public Integer getIntObject(String key, int defaultValue) {
215: Integer value = getIntObject(key);
216: return value != null ? value : new Integer(defaultValue);
217: }
218:
219: public Integer getIntObject(String key) {
220: try {
221: String value = getParameter(key);
222: return (value != null) ? new Integer(value) : null;
223: } catch (NumberFormatException e) {
224: return null;
225: }
226: }
227:
228: public long getLong(String key, long def) {
229: String value = getParameter(key);
230: long returnValue = def;
231: if (value != null) {
232: try {
233: returnValue = Long.parseLong(value);
234: return returnValue;
235: } catch (NumberFormatException e) {
236: return def;
237: }
238: } else {
239: return def;
240:
241: }
242: }
243:
244: public Long getLongObject(String key, long defaultValue) {
245: Long value = getLongObject(key);
246: return value != null ? value : new Long(defaultValue);
247: }
248:
249: public Long getLongObject(String key) {
250: try {
251: String value = getParameter(key);
252: return (value != null) ? new Long(value) : null;
253: } catch (NumberFormatException e) {
254: return null;
255: }
256: }
257:
258: public boolean getParameterExists(String param) {
259: String result = getParameter(param);
260: if ((result != null) && (result.length() != 0)) {
261: return true;
262: } else {
263: return false;
264: }
265: }
266: }
|