001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.parameter;
018:
019: // J2SE dependencies and extensions
020: import java.net.URI;
021: import javax.units.Unit;
022:
023: // JAI dependencies
024: import javax.media.jai.ParameterList;
025:
026: // OpenGIS dependencies
027: import org.opengis.parameter.ParameterValue;
028: import org.opengis.parameter.ParameterDescriptor;
029: import org.opengis.parameter.InvalidParameterTypeException;
030: import org.opengis.parameter.InvalidParameterValueException;
031:
032: // Geotools dependencies
033: import org.geotools.resources.Utilities;
034: import org.geotools.resources.i18n.Errors;
035: import org.geotools.resources.i18n.ErrorKeys;
036:
037: /**
038: * A particular parameter in a JAI's {@link ParameterList}.
039: *
040: * @since 2.2
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/parameter/ImagingParameter.java $
042: * @version $Id: ImagingParameter.java 20874 2006-08-07 10:00:01Z jgarnett $
043: * @author Martin Desruisseaux
044: */
045: final class ImagingParameter extends AbstractParameter implements
046: ParameterValue {
047: /**
048: * Serial number for interoperability with different versions.
049: */
050: private static final long serialVersionUID = -170895429717041733L;
051:
052: /**
053: * The JAI's parameter list used as the backing store for parameter values.
054: */
055: private final ParameterList parameters;
056:
057: /**
058: * Creates a new parameter from the specified list.
059: */
060: public ImagingParameter(final ParameterDescriptor descriptor,
061: final ParameterList parameters) {
062: super (descriptor);
063: this .parameters = parameters;
064: }
065:
066: /**
067: * Returns the exception to be throws for an operation on a wrong parameter type.
068: */
069: private InvalidParameterTypeException invalidType(
070: final ClassCastException cause) {
071: final InvalidParameterTypeException exception = new InvalidParameterTypeException(
072: Errors.format(
073: ErrorKeys.ILLEGAL_OPERATION_FOR_VALUE_CLASS_$1,
074: Utilities.getShortName(getType())),
075: getName(descriptor));
076: exception.initCause(cause);
077: return exception;
078: }
079:
080: /**
081: * Returns the unlocalized operation name. This is different from
082: * {@link AbstractParameter#getName}, which may returns a localized name.
083: */
084: private String getName() {
085: return descriptor.getName().getCode();
086: }
087:
088: /**
089: * Returns the parameter type.
090: */
091: private Class getType() {
092: return ((ParameterDescriptor) descriptor).getValueClass();
093: }
094:
095: /**
096: * Returns {@code null} since JAI's parameters have no units.
097: */
098: public Unit getUnit() {
099: return null;
100: }
101:
102: /**
103: * Always throws an exception, since this parameter has no unit.
104: */
105: public double doubleValue(final Unit unit)
106: throws InvalidParameterTypeException {
107: throw unitlessParameter(descriptor);
108: }
109:
110: /**
111: * Returns the numeric value of the coordinate operation parameter.
112: */
113: public double doubleValue() throws InvalidParameterTypeException {
114: final String name = getName();
115: final Class type = getType();
116: try {
117: if (type.equals(Float.class))
118: parameters.getFloatParameter(name);
119: if (type.equals(Long.class))
120: parameters.getLongParameter(name);
121: if (type.equals(Integer.class))
122: parameters.getIntParameter(name);
123: if (type.equals(Short.class))
124: parameters.getShortParameter(name);
125: if (type.equals(Byte.class))
126: parameters.getByteParameter(name);
127: return parameters.getDoubleParameter(name);
128: } catch (ClassCastException exception) {
129: throw invalidType(exception);
130: }
131: }
132:
133: /**
134: * Returns the positive integer value of an operation parameter.
135: */
136: public int intValue() throws InvalidParameterTypeException {
137: final String name = getName();
138: final Class type = getType();
139: try {
140: if (type.equals(Short.class))
141: parameters.getShortParameter(name);
142: if (type.equals(Byte.class))
143: parameters.getByteParameter(name);
144: return parameters.getIntParameter(name);
145: } catch (ClassCastException exception) {
146: throw invalidType(exception);
147: }
148: }
149:
150: /**
151: * Returns the boolean value of an operation parameter.
152: */
153: public boolean booleanValue() throws InvalidParameterTypeException {
154: final String name = getName();
155: try {
156: return parameters.getBooleanParameter(name);
157: } catch (ClassCastException exception) {
158: throw invalidType(exception);
159: }
160: }
161:
162: /**
163: * Returns the string value of an operation parameter.
164: */
165: public String stringValue() throws InvalidParameterTypeException {
166: final String name = getName();
167: try {
168: // Really cast to CharSequence (even if not needed for toString())
169: // because we want the ClassCastException if the type mismatch.
170: return ((CharSequence) parameters.getObjectParameter(name))
171: .toString();
172: } catch (ClassCastException exception) {
173: throw invalidType(exception);
174: }
175: }
176:
177: /**
178: * Always throws an exception, since this parameter has no unit.
179: */
180: public double[] doubleValueList(Unit unit)
181: throws InvalidParameterTypeException {
182: throw unitlessParameter(descriptor);
183: }
184:
185: /**
186: * Returns an ordered sequence of two or more numeric values of an operation parameter list.
187: */
188: public double[] doubleValueList()
189: throws InvalidParameterTypeException {
190: final String name = getName();
191: try {
192: return (double[]) parameters.getObjectParameter(name);
193: } catch (ClassCastException exception) {
194: throw invalidType(exception);
195: }
196: }
197:
198: /**
199: * Returns an ordered sequence of two or more integer values of an operation parameter list.
200: */
201: public int[] intValueList() throws InvalidParameterTypeException {
202: final String name = getName();
203: try {
204: return (int[]) parameters.getObjectParameter(name);
205: } catch (ClassCastException exception) {
206: throw invalidType(exception);
207: }
208: }
209:
210: /**
211: * Returns a reference to a file or a part of a file containing one or more parameter value.
212: *
213: * @todo Add automatic conversions, if it appears usefull for JAI parameters.
214: */
215: public URI valueFile() throws InvalidParameterTypeException {
216: final String name = getName();
217: try {
218: return (URI) parameters.getObjectParameter(name);
219: } catch (ClassCastException exception) {
220: throw invalidType(exception);
221: }
222: }
223:
224: /**
225: * Returns the parameter value as an object. The object type is typically a {@link Double},
226: * {@link Integer}, {@link Boolean}, {@link String}, {@link URI}, {@code double[]} or
227: * {@code int[]}.
228: */
229: public Object getValue() {
230: final String name = getName();
231: try {
232: return parameters.getObjectParameter(name);
233: } catch (IllegalStateException ignore) {
234: /*
235: * Thrown when the value still ParameterListDescriptor.NO_PARAMETER_DEFAULT.
236: * In this framework, the desired behavior in this case is to returns null.
237: */
238: return null;
239: }
240: }
241:
242: /**
243: * Always throws an exception, since this parameter has no unit.
244: */
245: public void setValue(final double value, Unit unit)
246: throws InvalidParameterValueException {
247: throw unitlessParameter(descriptor);
248: }
249:
250: /**
251: * Set the parameter value as a floating point.
252: */
253: public void setValue(final double value)
254: throws InvalidParameterValueException {
255: final String name = getName();
256: final Class type = getType();
257: try {
258: if (type.equals(Float.class)) {
259: parameters.setParameter(name, (float) value);
260: return;
261: }
262: if (type.equals(Long.class)) {
263: parameters.setParameter(name, (long) value);
264: return;
265: }
266: if (type.equals(Integer.class)) {
267: parameters.setParameter(name, (int) value);
268: return;
269: }
270: if (type.equals(Short.class)) {
271: parameters.setParameter(name, (short) value);
272: return;
273: }
274: if (type.equals(Byte.class)) {
275: parameters.setParameter(name, (byte) value);
276: return;
277: }
278: parameters.setParameter(name, value);
279: } catch (ClassCastException exception) {
280: throw invalidType(exception);
281: }
282: }
283:
284: /**
285: * Set the parameter value as an integer.
286: */
287: public void setValue(final int value)
288: throws InvalidParameterValueException {
289: final String name = getName();
290: final Class type = getType();
291: try {
292: if (type.equals(Short.class)) {
293: parameters.setParameter(name, (short) value);
294: return;
295: }
296: if (type.equals(Byte.class)) {
297: parameters.setParameter(name, (byte) value);
298: return;
299: }
300: parameters.setParameter(name, value);
301: } catch (ClassCastException exception) {
302: throw invalidType(exception);
303: }
304: }
305:
306: /**
307: * Set the parameter value as a boolean.
308: */
309: public void setValue(final boolean value)
310: throws InvalidParameterValueException {
311: final String name = getName();
312: try {
313: parameters.setParameter(name, value);
314: } catch (ClassCastException exception) {
315: throw invalidType(exception);
316: }
317: }
318:
319: /**
320: * Set the parameter value as an object. The object type is typically a {@link Double},
321: * {@link Integer}, {@link Boolean}, {@link String}, {@link URI}, {@code double[]}
322: * or {@code int[]}.
323: */
324: public void setValue(final Object value)
325: throws InvalidParameterValueException {
326: final String name = getName();
327: try {
328: parameters.setParameter(name, value);
329: } catch (ClassCastException exception) {
330: throw invalidType(exception);
331: }
332: }
333:
334: /**
335: * Always throws an exception, since this parameter has no unit.
336: */
337: public void setValue(double[] values, Unit unit)
338: throws InvalidParameterValueException {
339: throw unitlessParameter(descriptor);
340: }
341:
342: /**
343: * Compares the specified object with this parameter for equality.
344: */
345: public boolean equals(final Object object) {
346: if (object == this ) {
347: // Slight optimization
348: return true;
349: }
350: if (super .equals(object)) {
351: final ImagingParameter that = (ImagingParameter) object;
352: return Utilities.equals(this .getValue(), that.getValue());
353: }
354: return false;
355: }
356:
357: /**
358: * Returns a hash value for this parameter.
359: */
360: public int hashCode() {
361: int code = super .hashCode() * 37;
362: final Object value = getValue();
363: if (value != null) {
364: code += value.hashCode();
365: }
366: return code ^ (int) serialVersionUID;
367: }
368:
369: /**
370: * Returns a clone of this parameter. Actually returns a different classes, since this
371: * parameter is not really cloneable (it would requires a clone of {@link #parameters} first).
372: */
373: public Object clone() {
374: final Parameter parameter = new Parameter(
375: (ParameterDescriptor) descriptor);
376: parameter.setValue(getValue());
377: return parameter;
378: }
379: }
|