153
|
1 /* Parameter -- General digilib parameter class.
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de)
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 *
|
|
20 * Created on 02.09.2003 by casties
|
|
21 *
|
|
22 */
|
|
23 package digilib.servlet;
|
|
24
|
|
25 /** General digilib parameter class.
|
|
26 *
|
|
27 * @author casties
|
|
28 *
|
|
29 */
|
|
30 public class Parameter {
|
|
31 /** real value */
|
|
32 protected Object value = null;
|
|
33
|
|
34 /** default value */
|
|
35 protected Object defval = null;
|
|
36
|
|
37 /** parameter name (e.g. in config file) */
|
|
38 protected String name = null;
|
|
39
|
|
40 /** parameter type */
|
|
41 protected int type = 0;
|
|
42
|
|
43 /** Default constructor.
|
|
44 *
|
|
45 */
|
|
46 public Parameter() {
|
|
47 super();
|
|
48 }
|
|
49
|
|
50 /** Constructor with name, default, and value.
|
|
51 *
|
|
52 * @param value
|
|
53 * @param defval
|
|
54 */
|
|
55 public Parameter(String name, Object defval, Object value) {
|
|
56 this.name = name;
|
|
57 this.value = value;
|
|
58 this.defval = defval;
|
|
59 }
|
|
60
|
|
61 /** Constructor with name, default, value, and type.
|
|
62 * @param value
|
|
63 * @param defval
|
|
64 */
|
|
65 public Parameter(String name, Object defval, Object value, int type) {
|
|
66 this.name = name;
|
|
67 this.value = value;
|
|
68 this.defval = defval;
|
|
69 this.type = type;
|
|
70 }
|
|
71
|
|
72 /** Is the value valid.
|
|
73 *
|
|
74 * @return
|
|
75 */
|
|
76 public boolean hasValue() {
|
|
77 return (value != null);
|
|
78 }
|
|
79
|
|
80 /** Try to set the value from a String.
|
|
81 *
|
|
82 * Tries to convert the String to the same type as the default value.
|
|
83 * Sets the value anyway if the default is null.
|
|
84 * Returns if the value could be set.
|
|
85 *
|
|
86 * @param val
|
|
87 * @return
|
|
88 */
|
|
89 public boolean setValueFromString(String val) {
|
|
90 if (defval == null) {
|
|
91 this.value = val;
|
|
92 return true;
|
|
93 }
|
|
94 Class c = defval.getClass();
|
|
95 // take String as is
|
|
96 if (c == String.class) {
|
|
97 this.value = val;
|
|
98 return true;
|
|
99 }
|
|
100 // set Boolean if string == "true"
|
|
101 if (c == Boolean.class) {
|
|
102 this.value = new Boolean(val.compareToIgnoreCase("true") == 0);
|
|
103 return true;
|
|
104 }
|
|
105 // set Integer
|
|
106 if (c == Integer.class) {
|
|
107 this.value = new Integer(Integer.parseInt(val));
|
|
108 return true;
|
|
109 }
|
|
110 // set Float
|
|
111 if (c == Float.class) {
|
|
112 this.value = new Float(Float.parseFloat(val));
|
|
113 return true;
|
|
114 }
|
|
115 // then it's unknown
|
|
116 return false;
|
|
117 }
|
|
118
|
|
119 /** Get the default as Object.
|
|
120 *
|
|
121 * @return
|
|
122 */
|
|
123 public Object getDefault() {
|
|
124 return defval;
|
|
125 }
|
|
126
|
|
127 /** Set the default.
|
|
128 * @param defval
|
|
129 */
|
|
130 public void setDefault(Object defval) {
|
|
131 this.defval = defval;
|
|
132 }
|
|
133
|
|
134 /** Get the value as Object.
|
|
135 *
|
|
136 * Returns the default if the value is not set.
|
|
137 *
|
|
138 * @return
|
|
139 */
|
|
140 public Object getValue() {
|
|
141 return (value != null) ? value : defval;
|
|
142 }
|
|
143
|
|
144 public int getAsInt() {
|
|
145 Integer i = (Integer) getValue();
|
|
146 return (i != null) ? i.intValue() : 0;
|
|
147 }
|
|
148
|
|
149 public float getAsFloat() {
|
|
150 Float f = (Float) getValue();
|
|
151 return (f != null) ? f.floatValue() : 0f;
|
|
152 }
|
|
153
|
|
154 public String getAsString() {
|
|
155 return (String) getValue();
|
|
156 }
|
|
157
|
|
158 public boolean getAsBoolean() {
|
|
159 Boolean b = (Boolean) getValue();
|
|
160 return (b != null) ? b.booleanValue() : false;
|
|
161 }
|
|
162
|
|
163 /** Set the value.
|
|
164 *
|
|
165 * @param value
|
|
166 */
|
|
167 public void setValue(Object value) {
|
|
168 this.value = value;
|
|
169 }
|
|
170
|
|
171 /**
|
|
172 * @return
|
|
173 */
|
|
174 public String getName() {
|
|
175 return name;
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * @param name
|
|
180 */
|
|
181 public void setName(String name) {
|
|
182 this.name = name;
|
|
183 }
|
|
184
|
|
185 /**
|
|
186 * @return
|
|
187 */
|
|
188 public int getType() {
|
|
189 return type;
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * @param type
|
|
194 */
|
|
195 public void setType(int type) {
|
|
196 this.type = type;
|
|
197 }
|
|
198
|
|
199 }
|