1 /*
2 * Copyright (c) 1998-2004 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.experiment;
20
21
22 /**
23 * An instance of class <code>PropertyDefinition</code> contains
24 * information about valid values for a named property.
25 * The constructors of <code>PropertyDefinition</code> usually take
26 * a property name, the type of property, and the valid values of
27 * the property, as parameters. If the valid values are not given,
28 * all values are valid, as long as they are of the given type.
29 * <br>
30 * Valid types are boolean, String, int, Integer and Range,
31 * or an array of these types. The list of valid values is
32 * also passed as an array of one of these types.
33 * Each type is represented by a member constant according to this list:<br>
34 * <br>
35 * boolean PropertyDefinition.BOOLEAN_TYPE<br>
36 * String PropertyDefinition.STRING_TYPE<br>
37 * int PropertyDefinition.INT_TYPE<br>
38 * Integer PropertyDefinition.INTEGER_TYPE<br>
39 * Range PropertyDefinition.RANGE_TYPE<br>
40 *
41 * @author Bjarte Svaeren
42 */
43 public class PropertyDefinition
44 {
45 ////////////////////////////////////////////////////////////////////////////////////////////
46 // Constants
47 ////////////////////////////////////////////////////////////////////////////////////////////
48
49 public static final int BOOLEAN_TYPE = 0;
50 public static final int STRING_TYPE = 1;
51 public static final int INT_TYPE = 2;
52 public static final int INTEGER_TYPE = 3;
53 public static final int RANGE_TYPE = 4;
54 public static final int BOOLEAN_ARRAY_TYPE = 5;
55 public static final int STRING_ARRAY_TYPE = 6;
56 public static final int INT_ARRAY_TYPE = 7;
57 public static final int INTEGER_ARRAY_TYPE = 8;
58 public static final int RANGE_ARRAY_TYPE = 9;
59
60 ////////////////////////////////////////////////////////////////////////////////////////////
61 // Data fields
62 ////////////////////////////////////////////////////////////////////////////////////////////
63
64 private String propertyName;
65 private int propertyType;
66 private Object validValues;
67
68
69 ////////////////////////////////////////////////////////////////////////////////////////////
70 // Constructors
71 ////////////////////////////////////////////////////////////////////////////////////////////
72
73 /**
74 * Creates a <code>PropertyDefinition</code> object with
75 * the specified name and type, with no restrictions on
76 * value. If the property is of type boolean, only this
77 * constructor can be used. boolean values cannot be
78 * restriced more than the implicit 'true' and 'false'.
79 * <code>type</code> has to be one of the member constants
80 * listed above.
81 *
82 * @param name The name of the property
83 * @param type The type of the property
84 */
85 public PropertyDefinition(String name, int type)
86 {
87 propertyName = name;
88 propertyType = type;
89 validValues = null;
90 }
91
92
93 /**
94 * Creates a <code>PropertyDefinition</code> object with
95 * the specified name and type. The valid values will be
96 * interpreted according to the type of the property.
97 *
98 * @param name The name of the property
99 * @param type The type of the property
100 * @param validValues The valid values of the property
101 */
102 public PropertyDefinition(String name, int type, Object[] values)
103 {
104 propertyName = name;
105 propertyType = type;
106
107 switch (propertyType) {
108 case STRING_TYPE:
109 case STRING_ARRAY_TYPE:
110 registerString(values);
111 break;
112 case INTEGER_TYPE:
113 case INTEGER_ARRAY_TYPE:
114 registerInteger(values);
115 break;
116 case RANGE_TYPE:
117 case RANGE_ARRAY_TYPE:
118 registerRange(values);
119 break;
120
121 default:
122 throw new IllegalArgumentException("Unknown property type");
123 }
124 }
125
126
127 /**
128 * Creates a <code>PropertyDefinition</code> object of type
129 * int, with the specified name. The valid values will be
130 * interpreted accordingly.
131 *
132 * @param name The name of the property
133 * @param type The type of the property
134 * @param validValues The valid values of the property
135 */
136 public PropertyDefinition(String name, int type, int[] values)
137 {
138 if(type != INT_TYPE && type != INT_ARRAY_TYPE)
139 throw new IllegalArgumentException();
140
141 registerInt(values);
142 }
143
144
145 ////////////////////////////////////////////////////////////////////////////////////////////
146 // Public methods
147 ////////////////////////////////////////////////////////////////////////////////////////////
148
149 /**
150 * Returns the name of the property as a String.
151 *
152 * @return The name of the property
153 */
154 public String getPropertyName()
155 {
156 return propertyName;
157 }
158
159
160 /**
161 * Returns the type of the property as one of the
162 * constants from the list above.
163 *
164 * @return The type of the property
165 */
166 public int getPropertyType()
167 {
168 return propertyType;
169 }
170
171
172 /**
173 * Returns true if the value is a valid type
174 * for this property, and has a valid value.
175 * Otherwise it returns false.
176 *
177 * @param value The value to check.
178 * @return True if the value is valid, false if it's not.
179 */
180 public boolean isValid(Object value)
181 {
182 // If validValues is null, all values are valid
183 if(validValues == null)
184 return true;
185
186 switch (propertyType) {
187 case STRING_TYPE:
188 return isStringValid(value);
189 case INTEGER_TYPE:
190 return isIntegerValid(value);
191 case RANGE_TYPE:
192 return isRangeValid(value);
193
194 default:
195 throw new IllegalArgumentException("Uknown property type");
196 }
197 }
198
199
200 /**
201 * Returns true if the int is a valid type
202 * for this property, and has a valid value.
203 * Otherwise, it returns false.
204 *
205 * @param value The value to check.
206 * @return True if the value is valid, false if it's not.
207 */
208 public boolean isValid(int value)
209 {
210 if(propertyType != INT_TYPE && propertyType != INT_ARRAY_TYPE)
211 throw new IllegalArgumentException();
212
213 // If validValues is null, all values are valid
214 if(validValues == null)
215 return true;
216
217 Integer[] validIntegers = (Integer[]) validValues;
218
219 for (int i = 0; i < validIntegers.length; i++) {
220 if(validIntegers[i].intValue() == value)
221 return true;
222 }
223
224 return false;
225 }
226
227
228 /**
229 * Returns true if boolean is a valid type
230 * for this property, otherwise it returns false.
231 *
232 * @param value The value to check.
233 * @return True if the value is valid, false if it's not.
234 */
235 public boolean isValid(boolean value)
236 {
237 if(propertyType != BOOLEAN_TYPE && propertyType != BOOLEAN_ARRAY_TYPE)
238 throw new IllegalArgumentException();
239
240 // If the type is boolean, it can either be
241 // true or false - both are valid
242 return true;
243 }
244
245
246 ////////////////////////////////////////////////////////////////////////////////////////////
247 // Private methods
248 ////////////////////////////////////////////////////////////////////////////////////////////
249
250 private void registerString(Object[] values)
251 {
252 String[] validStrings = (String[]) values;
253 validValues = validStrings;
254 }
255
256
257 private void registerInt(int[] values)
258 {
259 Integer[] validIntegers = new Integer[values.length];
260
261 // Transform from ints to Integers
262 for (int i = 0; i < validIntegers.length; i++) {
263 validIntegers[i] = new Integer(values[i]);
264 }
265
266 validValues = validIntegers;
267 }
268
269
270 private void registerInteger(Object[] values)
271 {
272 Integer[] validIntegers = (Integer[]) values;
273 validValues = validIntegers;
274 }
275
276
277 private void registerRange(Object[] values)
278 {
279 Range[] validRanges = (Range[]) values;
280 validValues = validRanges;
281 }
282
283
284 private boolean isStringValid(Object value)
285 {
286 String[] validStrings = (String[]) validValues;
287
288 for (int i = 0; i < validStrings.length; i++) {
289 if(validStrings[i].equals(value))
290 return true;
291 }
292
293 return false;
294 }
295
296
297 private boolean isIntegerValid(Object value)
298 {
299 Integer[] validIntegers = (Integer[]) validValues;
300
301 for (int i = 0; i < validIntegers.length; i++) {
302 if(validIntegers[i].compareTo((Integer) value) == 0)
303 return true;
304 }
305
306 return false;
307 }
308
309
310 private boolean isRangeValid(Object value)
311 {
312 Range[] validRanges = (Range[]) validValues;
313
314 for (int i = 0; i < validRanges.length; i++) {
315 if(validRanges[i].equals(value))
316 return true;
317 }
318
319 return false;
320 }
321 }