001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2001, 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; either
010: * version 2.1 of the License, or (at your option) any later version.
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.coverage.processing;
018:
019: // J2SE dependencies
020: import java.awt.RenderingHints;
021: import java.io.Serializable;
022: import java.util.Iterator;
023:
024: // OpenGIS dependencies
025: import org.opengis.coverage.Coverage;
026: import org.opengis.coverage.processing.Operation;
027: import org.opengis.parameter.ParameterValueGroup;
028: import org.opengis.parameter.ParameterDescriptor;
029: import org.opengis.parameter.ParameterDescriptorGroup;
030: import org.opengis.parameter.GeneralParameterDescriptor;
031: import org.opengis.util.InternationalString;
032:
033: // Geotools dependencies
034: import org.geotools.factory.Hints;
035: import org.geotools.resources.Utilities;
036: import org.geotools.resources.i18n.Errors;
037: import org.geotools.resources.i18n.ErrorKeys;
038:
039: /**
040: * Provides descriptive information for a {@linkplain Coverage coverage} processing operation.
041: * The descriptive information includes such information as the name of the operation, operation
042: * description, and number of source grid coverages required for the operation.
043: *
044: * @since 2.2
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/main/java/org/geotools/coverage/processing/AbstractOperation.java $
046: * @version $Id: AbstractOperation.java 25262 2007-04-23 21:11:16Z desruisseaux $
047: * @author Martin Desruisseaux
048: */
049: public abstract class AbstractOperation implements Operation,
050: Serializable {
051: /**
052: * Serial number for interoperability with different versions.
053: */
054: private static final long serialVersionUID = -1441856042779942954L;;
055:
056: /**
057: * The parameters descriptor.
058: */
059: protected final ParameterDescriptorGroup descriptor;
060:
061: /**
062: * Constructs an operation. The operation name will be the same than the
063: * parameter descriptor name.
064: *
065: * @param descriptor The parameters descriptor.
066: */
067: public AbstractOperation(final ParameterDescriptorGroup descriptor) {
068: ensureNonNull("descriptor", descriptor);
069: this .descriptor = descriptor;
070: }
071:
072: /**
073: * Returns the name of the processing operation. The default implementation
074: * returns the {@linkplain #descriptor} code name.
075: */
076: public String getName() {
077: return descriptor.getName().getCode();
078: }
079:
080: /**
081: * Returns the description of the processing operation. If there is no description,
082: * returns {@code null}. The default implementation returns the {@linkplain #descriptor}
083: * remarks.
084: */
085: public String getDescription() {
086: final InternationalString remarks = descriptor.getRemarks();
087: return (remarks != null) ? remarks.toString() : null;
088: }
089:
090: /**
091: * Returns the URL for documentation on the processing operation. If no online documentation
092: * is available the string will be null. The default implementation returns {@code null}.
093: */
094: public String getDocURL() {
095: return null;
096: }
097:
098: /**
099: * Returns the version number of the implementation.
100: */
101: public String getVersion() {
102: return descriptor.getName().getVersion();
103: }
104:
105: /**
106: * Returns the vendor name of the processing operation implementation.
107: * The default implementation returns "Geotools 2".
108: */
109: public String getVendor() {
110: return "Geotools 2";
111: }
112:
113: /**
114: * Returns the number of source coverages required for the operation.
115: */
116: public int getNumSources() {
117: return getNumSources(descriptor);
118: }
119:
120: /**
121: * Returns the number of source coverages in the specified parameter group.
122: */
123: private static int getNumSources(
124: final ParameterDescriptorGroup descriptor) {
125: int count = 0;
126: for (final Iterator it = descriptor.descriptors().iterator(); it
127: .hasNext();) {
128: final GeneralParameterDescriptor candidate = (GeneralParameterDescriptor) it
129: .next();
130: if (candidate instanceof ParameterDescriptorGroup) {
131: count += getNumSources((ParameterDescriptorGroup) candidate);
132: continue;
133: }
134: if (candidate instanceof ParameterDescriptor) {
135: final Class type = ((ParameterDescriptor) candidate)
136: .getValueClass();
137: if (Coverage.class.isAssignableFrom(type)) {
138: count++;
139: }
140: }
141: }
142: return count;
143: }
144:
145: /**
146: * Returns an initially empty set of parameters.
147: */
148: public ParameterValueGroup getParameters() {
149: return (ParameterValueGroup) descriptor.createValue(); // TODO: remove cast with J2SE 1.5.
150: }
151:
152: /**
153: * Applies a process operation to a coverage. This method is invoked by {@link DefaultProcessor}.
154: *
155: * @param parameters List of name value pairs for the parameters required for the operation.
156: * @param hints A set of rendering hints, or {@code null} if none. The {@code DefaultProcessor}
157: * may provides hints for the following keys: {@link Hints#COORDINATE_OPERATION_FACTORY}
158: * and {@link Hints#JAI_INSTANCE}.
159: * @return The result as a coverage.
160: *
161: * @throws CoverageProcessingException if the operation can't be applied.
162: */
163: public abstract Coverage doOperation(
164: final ParameterValueGroup parameters, final Hints hints)
165: throws CoverageProcessingException;
166:
167: /**
168: * Returns the {@link AbstractProcessor} instance used for an operation. The instance is fetch
169: * from the rendering hints given to the {@link #doOperation} method. If no processor is
170: * specified, then a default one is returned.
171: *
172: * @param hints The rendering hints, or {@code null} if none.
173: * @return The {@code AbstractProcessor} instance in use (never {@code null}).
174: */
175: protected static AbstractProcessor getProcessor(
176: final RenderingHints hints) {
177: if (hints != null) {
178: final Object value = hints
179: .get(Hints.GRID_COVERAGE_PROCESSOR);
180: if (value instanceof AbstractProcessor) {
181: return (AbstractProcessor) value;
182: }
183: }
184: return AbstractProcessor.getInstance();
185: }
186:
187: /**
188: * Makes sure that an argument is non-null. This is a convenience method for
189: * implementations in subclasses.
190: *
191: * @param name Argument name.
192: * @param object User argument.
193: * @throws IllegalArgumentException if {@code object} is null.
194: */
195: protected static void ensureNonNull(final String name,
196: final Object object) throws IllegalArgumentException {
197: if (object == null) {
198: throw new IllegalArgumentException(Errors.format(
199: ErrorKeys.NULL_ARGUMENT_$1, name));
200: }
201: }
202:
203: /**
204: * Returns a hash value for this operation. This value need not remain consistent between
205: * different implementations of the same class.
206: */
207: public int hashCode() {
208: // Since we should have only one operation registered for each name,
209: // the descriptors hash code should be enough.
210: return descriptor.hashCode() ^ (int) serialVersionUID;
211: }
212:
213: /**
214: * Compares the specified object with this operation for equality.
215: */
216: public boolean equals(final Object object) {
217: if (object != null && object.getClass().equals(getClass())) {
218: final AbstractOperation that = (AbstractOperation) object;
219: return Utilities.equals(this .descriptor, that.descriptor);
220: }
221: return false;
222: }
223:
224: /**
225: * Returns a string représentation of this operation. The returned string is
226: * implementation dependent. It is usually provided for debugging purposes only.
227: */
228: public String toString() {
229: return Utilities.getShortClassName(this ) + '[' + getName()
230: + ']';
231: }
232: }
|