001: /*
002: * $RCSfile: ParameterListImpl.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:15 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: import com.sun.media.jai.util.CaselessStringArrayTable;
015: import java.text.MessageFormat;
016: import java.util.Hashtable;
017: import java.util.Locale;
018: import javax.media.jai.util.CaselessStringKey;
019:
020: /**
021: * A concrete implementation of the <code>ParameterList</code>
022: * interface. The number, names, Class types and default values are
023: * specified via the associated <code>ParameterListDescriptor</code>
024: * which should be supplied at construction time. This
025: * default implementation should be sufficient for most
026: * <code>ParameterList</code>s and normally need not be sub-classed.
027: *
028: * @see ParameterList
029: * @see ParameterListDescriptor
030: * @see ParameterListDescriptorImpl
031: *
032: * @since JAI 1.1
033: */
034: public class ParameterListImpl implements ParameterList,
035: java.io.Serializable {
036:
037: private ParameterListDescriptor pld;
038:
039: /**
040: * A <code>CaselessStringArrayTable</code> of parameter indices hashed by
041: * <code>CaselessStringKey</code> versions of the names.
042: */
043: private CaselessStringArrayTable paramIndices;
044:
045: /**
046: * Something to hold the parameter values.
047: */
048: private Object[] paramValues;
049:
050: /**
051: * The parameter classes obtained from <code>ParameterListDescriptor</code>
052: */
053: private Class[] paramClasses;
054:
055: /**
056: * Creates a <code>ParameterListImpl</code> using the specified
057: * <code>ParameterListDescriptor</code>. Initializes the
058: * parameters to the defaults (could be
059: * <code>ParameterListDescriptor.NO_PARAMETER_DEFAULT</code>)
060: * specified by <code>descriptor</code>
061: *
062: * @param descriptor a <code>ParameterListDescriptor</code> describing
063: * the parameter names, defaults etc.
064: *
065: * @throws IllegalArgumentException if descriptor is <code>null</code>
066: */
067: public ParameterListImpl(ParameterListDescriptor descriptor) {
068:
069: if (descriptor == null)
070: throw new IllegalArgumentException(JaiI18N
071: .getString("Generic0"));
072:
073: this .pld = descriptor;
074:
075: int numParams = pld.getNumParameters();
076:
077: if (numParams > 0) {
078: // Fill in the parameter defaults.
079: Object[] paramDefaults = pld.getParamDefaults();
080:
081: paramClasses = pld.getParamClasses();
082: paramIndices = new CaselessStringArrayTable(pld
083: .getParamNames());
084: paramValues = new Object[numParams];
085:
086: for (int i = 0; i < numParams; i++) {
087: paramValues[i] = paramDefaults[i];
088: }
089:
090: } else {
091: paramClasses = null;
092: paramIndices = null;
093: paramValues = null;
094: }
095: }
096:
097: /**
098: * Returns the associated <code>ParameterListDescriptor</code>.
099: */
100: public ParameterListDescriptor getParameterListDescriptor() {
101: return pld;
102: }
103:
104: /**
105: * A private method (so that it may get inlined) which sets
106: * the value of the specified parameter.
107: *
108: * Checks are made to verify that the parameter is of the right
109: * <code>Class</code> type and that the value is valid.
110: *
111: * @param paramName a <code>String</code> naming a parameter.
112: * @param b a <code>byte</code> value for the parameter.
113: *
114: * @throws IllegalArgumentException if paramName is null.
115: * @throws IllegalArgumentException if there is no parameter with the
116: * specified name.
117: * @throws IllegalArgumentException if the class type of parameter
118: * pointed to by the paramName is not a <code>Byte</code>
119: * @throws IllegalArgumentException if the parameter value is invalid.
120: */
121: private ParameterList setParameter0(String paramName, Object obj) {
122:
123: int index = paramIndices.indexOf(paramName);
124:
125: if ((obj != null) && !paramClasses[index].isInstance(obj))
126: throw new IllegalArgumentException(formatMsg(JaiI18N
127: .getString("ParameterListImpl0"), new Object[] {
128: obj.getClass().getName(),
129: paramClasses[index].getName(), paramName }));
130:
131: if (!pld.isParameterValueValid(paramName, obj))
132: throw new IllegalArgumentException(paramName + ":"
133: + JaiI18N.getString("ParameterListImpl1"));
134:
135: paramValues[index] = obj;
136:
137: return this ;
138: }
139:
140: /**
141: * Sets a named parameter to a <code>byte</code> value.
142: * Checks are made to verify that the parameter is of the right
143: * <code>Class</code> type and that the value is valid.
144: *
145: * @param paramName a <code>String</code> naming a parameter.
146: * @param b a <code>byte</code> value for the parameter.
147: *
148: * @throws IllegalArgumentException if paramName is null.
149: * @throws IllegalArgumentException if there is no parameter with the
150: * specified name.
151: * @throws IllegalArgumentException if the class type of parameter
152: * pointed to by the paramName is not a <code>Byte</code>
153: * @throws IllegalArgumentException if the parameter value is invalid.
154: */
155: public ParameterList setParameter(String paramName, byte b) {
156: return setParameter0(paramName, new Byte(b));
157: }
158:
159: /**
160: * Sets a named parameter to a <code>boolean</code> value.
161: * Checks are made to verify that the parameter is of the right
162: * <code>Class</code> type and that the value is valid.
163: *
164: * @param paramName a <code>String</code> naming a parameter.
165: * @param b a <code>boolean</code> value for the parameter.
166: *
167: * @throws IllegalArgumentException if paramName is null.
168: * @throws IllegalArgumentException if there is no parameter with the
169: * specified name.
170: * @throws IllegalArgumentException if the class type of parameter
171: * pointed to by the paramName is not a <code>Boolean</code>
172: * @throws IllegalArgumentException if the parameter value is invalid.
173: */
174: public ParameterList setParameter(String paramName, boolean b) {
175: return setParameter0(paramName, new Boolean(b));
176: }
177:
178: /**
179: * Sets a named parameter to a <code>char</code> value.
180: * Checks are made to verify that the parameter is of the right
181: * <code>Class</code> type and that the value is valid.
182: *
183: * @param paramName a <code>String</code> naming a parameter.
184: * @param c a <code>char</code> value for the parameter.
185: *
186: * @throws IllegalArgumentException if paramName is null.
187: * @throws IllegalArgumentException if there is no parameter with the
188: * specified name.
189: * @throws IllegalArgumentException if the class type of parameter
190: * pointed to by the paramName is not a <code>Character</code>
191: * @throws IllegalArgumentException if the parameter value is invalid.
192: */
193: public ParameterList setParameter(String paramName, char c) {
194: return setParameter0(paramName, new Character(c));
195: }
196:
197: /**
198: * Sets a named parameter to a <code>short</code> value.
199: * Checks are made to verify that the parameter is of the right
200: * <code>Class</code> type and that the value is valid.
201: *
202: * @param paramName a <code>String</code> naming a parameter.
203: * @param s a <code>short<code> value for the parameter.
204: *
205: * @throws IllegalArgumentException if paramName is null.
206: * @throws IllegalArgumentException if there is no parameter with the
207: * specified name.
208: * @throws IllegalArgumentException if the class type of parameter
209: * pointed to by the paramName is not a <code>Short</code>
210: * @throws IllegalArgumentException if the parameter value is invalid.
211: */
212: public ParameterList setParameter(String paramName, short s) {
213: return setParameter0(paramName, new Short(s));
214: }
215:
216: /**
217: * Sets a named parameter to an <code>int</code> value.
218: * Checks are made to verify that the parameter is of the right
219: * <code>Class</code> type and that the value is valid.
220: *
221: * @param paramName a <code>String</code> naming a parameter.
222: * @param i an <code>int</code> value for the parameter.
223: *
224: * @throws IllegalArgumentException if paramName is null.
225: * @throws IllegalArgumentException if there is no parameter with the
226: * specified name.
227: * @throws IllegalArgumentException if the class type of parameter
228: * pointed to by the paramName is not a <code>Integer</code>
229: * @throws IllegalArgumentException if the parameter value is invalid.
230: */
231: public ParameterList setParameter(String paramName, int i) {
232: return setParameter0(paramName, new Integer(i));
233: }
234:
235: /**
236: * Sets a named parameter to a <code>long</code> value.
237: * Checks are made to verify that the parameter is of the right
238: * <code>Class</code> type and that the value is valid.
239: *
240: * @param paramName a <code>String</code> naming a parameter.
241: * @param l a <code>long</code> value for the parameter.
242: *
243: * @throws IllegalArgumentException if paramName is null.
244: * @throws IllegalArgumentException if there is no parameter with the
245: * specified name.
246: * @throws IllegalArgumentException if the class type of parameter
247: * pointed to by the paramName is not a <code>Long</code>
248: * @throws IllegalArgumentException if the parameter value is invalid.
249: */
250: public ParameterList setParameter(String paramName, long l) {
251: return setParameter0(paramName, new Long(l));
252: }
253:
254: /**
255: * Sets a named parameter to a <code>float</code> value.
256: * Checks are made to verify that the parameter is of the right
257: * <code>Class</code> type and that the value is valid.
258: *
259: * @param paramName a <code>String</code> naming a parameter.
260: * @param f a <code>float</code> value for the parameter.
261: *
262: * @throws IllegalArgumentException if paramName is null.
263: * @throws IllegalArgumentException if there is no parameter with the
264: * specified name.
265: * @throws IllegalArgumentException if the class type of parameter
266: * pointed to by the paramName is not a <code>Float</code>
267: * @throws IllegalArgumentException if the parameter value is invalid.
268: */
269: public ParameterList setParameter(String paramName, float f) {
270: return setParameter0(paramName, new Float(f));
271: }
272:
273: /**
274: * Sets a named parameter to a <code>double</code> value.
275: * Checks are made to verify that the parameter is of the right
276: * <code>Class</code> type and that the value is valid.
277: *
278: * @param paramName a <code>String</code> naming a parameter.
279: * @param d a <code>double</code> value for the parameter.
280: *
281: * @throws IllegalArgumentException if paramName is null.
282: * @throws IllegalArgumentException if there is no parameter with the
283: * specified name.
284: * @throws IllegalArgumentException if the class type of parameter
285: * pointed to by the paramName is not a <code>Double</code>
286: * @throws IllegalArgumentException if the parameter value is invalid.
287: */
288: public ParameterList setParameter(String paramName, double d) {
289: return setParameter0(paramName, new Double(d));
290: }
291:
292: /**
293: * Sets a named parameter to an <code>Object</code> value.
294: * Checks are made to verify that the parameter is of the right
295: * <code>Class</code> type and that the value is valid.
296: *
297: * @param paramName a <code>String</code> naming a parameter.
298: * @param obj an <code>Object</code> value for the parameter.
299: *
300: * @throws IllegalArgumentException if paramName is null.
301: * @throws IllegalArgumentException if there is no parameter with the
302: * specified name.
303: * @throws IllegalArgumentException if the parameter value is invalid.
304: */
305: public ParameterList setParameter(String paramName, Object obj) {
306: return setParameter0(paramName, obj);
307: }
308:
309: /**
310: * A private method (so that it can be inlined) to get the
311: * value associated with the specified parameter.
312: *
313: * @throws IllegalArgumentException if paramName is null.
314: * @throws IllegalArgumentException if there is no parameter with the
315: * specified name.
316: * @throws IllegalStateException if the parameter value is still
317: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
318: */
319: private Object getObjectParameter0(String paramName) {
320:
321: Object obj = paramValues[paramIndices.indexOf(paramName)];
322:
323: if (obj == ParameterListDescriptor.NO_PARAMETER_DEFAULT)
324: throw new IllegalStateException(paramName + ":"
325: + JaiI18N.getString("ParameterListImpl2"));
326:
327: return obj;
328: }
329:
330: /**
331: * Gets a named parameter as an <code>Object</code>. Parameters
332: * belonging to a primitive type, such as int, will be returned as a
333: * member of the corresponding wrapper class, such as
334: * <code>Integer</code>
335: *
336: * @param paramName the name of the parameter to be returned.
337: *
338: * @throws IllegalArgumentException if paramName is null.
339: * @throws IllegalArgumentException if there is no parameter with the
340: * specified name.
341: * @throws IllegalStateException if the parameter value is still
342: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
343: */
344: public Object getObjectParameter(String paramName) {
345: return getObjectParameter0(paramName);
346: }
347:
348: /**
349: * A convenience method to return a parameter as a <code>byte</code>.
350: *
351: * @param paramName the name of the parameter to be returned.
352: *
353: * @throws IllegalArgumentException if paramName is null.
354: * @throws IllegalArgumentException if there is no parameter with the
355: * specified name.
356: * @throws ClassCastException if the parameter is of a different type.
357: * @throws IllegalStateException if the parameter value is still
358: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
359: */
360: public byte getByteParameter(String paramName) {
361: return ((Byte) getObjectParameter0(paramName)).byteValue();
362: }
363:
364: /**
365: * A convenience method to return a parameter as a <code>boolean</code>.
366: *
367: * @param paramName the name of the parameter to be returned.
368: *
369: * @throws IllegalArgumentException if paramName is null.
370: * @throws IllegalArgumentException if there is no parameter with the
371: * specified name.
372: * @throws ClassCastException if the parameter is of a different type.
373: * @throws IllegalStateException if the parameter value is still
374: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
375: */
376: public boolean getBooleanParameter(String paramName) {
377: return ((Boolean) getObjectParameter0(paramName))
378: .booleanValue();
379: }
380:
381: /**
382: * A convenience method to return a parameter as a <code>char</code>.
383: *
384: * @param paramName the name of the parameter to be returned.
385: *
386: * @throws IllegalArgumentException if paramName is null.
387: * @throws IllegalArgumentException if there is no parameter with the
388: * specified name.
389: * @throws ClassCastException if the parameter is of a different type.
390: * @throws IllegalStateException if the parameter value is still
391: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
392: */
393: public char getCharParameter(String paramName) {
394: return ((Character) getObjectParameter0(paramName)).charValue();
395: }
396:
397: /**
398: * A convenience method to return a parameter as a <code>short</code>.
399: *
400: * @param paramName the name of the parameter to be returned.
401: *
402: * @throws IllegalArgumentException if paramName is null.
403: * @throws IllegalArgumentException if there is no parameter with the
404: * specified name.
405: * @throws ClassCastException if the parameter is of a different type.
406: * @throws IllegalStateException if the parameter value is still
407: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
408: */
409: public short getShortParameter(String paramName) {
410: return ((Short) getObjectParameter0(paramName)).shortValue();
411: }
412:
413: /**
414: * A convenience method to return a parameter as an <code>int</code>.
415: *
416: * @param paramName the name of the parameter to be returned.
417: *
418: * @throws IllegalArgumentException if paramName is null.
419: * @throws IllegalArgumentException if there is no parameter with the
420: * specified name.
421: * @throws ClassCastException if the parameter is of a different type.
422: * @throws IllegalStateException if the parameter value is still
423: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
424: */
425: public int getIntParameter(String paramName) {
426: return ((Integer) getObjectParameter0(paramName)).intValue();
427: }
428:
429: /**
430: * A convenience method to return a parameter as a <code>long</code>.
431: *
432: * @param paramName the name of the parameter to be returned.
433: *
434: * @throws IllegalArgumentException if paramName is null.
435: * @throws IllegalArgumentException if there is no parameter with the
436: * specified name.
437: * @throws ClassCastException if the parameter is of a different type.
438: * @throws IllegalStateException if the parameter value is still
439: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
440: */
441: public long getLongParameter(String paramName) {
442: return ((Long) getObjectParameter0(paramName)).longValue();
443: }
444:
445: /**
446: * A convenience method to return a parameter as a <code>float</code>.
447: *
448: * @param paramName the name of the parameter to be returned.
449: *
450: * @throws IllegalArgumentException if paramName is null.
451: * @throws IllegalArgumentException if there is no parameter with the
452: * specified name.
453: * @throws ClassCastException if the parameter is of a different type.
454: * @throws IllegalStateException if the parameter value is still
455: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
456: */
457: public float getFloatParameter(String paramName) {
458: return ((Float) getObjectParameter0(paramName)).floatValue();
459: }
460:
461: /**
462: * A convenience method to return a parameter as a <code>double</code>.
463: *
464: * @param paramName the name of the parameter to be returned.
465: *
466: * @throws IllegalArgumentException if paramName is null.
467: * @throws IllegalArgumentException if there is no parameter with the
468: * specified name.
469: * @throws ClassCastException if the parameter is of a different type.
470: * @throws IllegalStateException if the parameter value is still
471: * ParameterListDescriptor.NO_PARAMETER_DEFAULT
472: */
473: public double getDoubleParameter(String paramName) {
474: return ((Double) getObjectParameter0(paramName)).doubleValue();
475: }
476:
477: /**
478: * Creates a <code>MessageFormat</code> object and set the
479: * <code>Locale</code> to default.
480: */
481: private String formatMsg(String key, Object[] args) {
482: MessageFormat mf = new MessageFormat(key);
483: mf.setLocale(Locale.getDefault());
484:
485: return mf.format(args);
486: }
487: }
|