/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2005-2006, GeoTools Project Managment Committee (PMC)
 *    (C) 2005, Institut de Recherche pour le Développement
 *   
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    This package contains documentation from OpenGIS specifications.
 *    OpenGIS consortium's work is fully acknowledged here.
 */
package org.geotools.referencing.operation;

// J2SE dependencies
import java.util.Map;
import java.util.Collections;

// OpenGIS dependencies
import org.opengis.parameter.ParameterDescriptorGroup;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.crs.GeneralDerivedCRS;
import org.opengis.referencing.operation.OperationMethod;
import org.geotools.referencing.wkt.Formatter;


/**
 * A conversion used for the definition of a {@linkplain GeneralDerivedCRS derived CRS}
 * (including projections). This conversion has no source and target CRS, and no math
 * transform. Those elements are created by the derived CRS itself.
 *  
 * @since 2.1
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/operation/DefiningConversion.java $
 * @version $Id: DefiningConversion.java 20874 2006-08-07 10:00:01Z jgarnett $
 * @author Martin Desruisseaux
 * @author Matthias Basler
 */
public class DefiningConversion extends DefaultConversion {
    /**
     * Serial number for interoperability with different versions.
     */
    private static final long serialVersionUID = 7399026512478064721L;

    /**
     * The parameter values.
     */
    private final ParameterValueGroup parameters;

    /**
     * Convenience constructor for creating a defining conversion with a default operation method.
     * The operation method is assumed two-dimensional.
     *
     * @param name       The conversion name.
     * @param parameters The parameter values.
     *
     * @since 2.2
     */
    public DefiningConversion(final String              name,
                              final ParameterValueGroup parameters)
    {
        this(Collections.singletonMap(NAME_KEY, name), getOperationMethod(parameters), parameters);
    }

    /**
     * Work around for RFE #4093999 in Sun's bug database
     * ("Relax constraint on placement of this()/super() call in constructors").
     */
    private static OperationMethod getOperationMethod(final ParameterValueGroup parameters) {
        ensureNonNull("parameters", parameters);
        // TODO: remove the cast when we will be allowed to compile for J2SE 1.5.
        final ParameterDescriptorGroup descriptor = (ParameterDescriptorGroup) parameters.getDescriptor();
        return new DefaultOperationMethod(getProperties(descriptor, null), 2, 2, descriptor);
    }

    /**
     * Constructs a conversion from a set of properties. The properties given in argument
     * follow the same rules than for the {@link AbstractCoordinateOperation} constructor.
     *
     * @param properties Set of properties. Should contains at least <code>"name"</code>.
     * @param method The operation method.
     * @param parameters The parameter values.
     */
    public DefiningConversion(final Map                 properties,
                              final OperationMethod     method,
                              final ParameterValueGroup parameters)
    {
        super(properties, null, null, null, method);
        ensureNonNull("parameters", parameters);
        this.parameters = (ParameterValueGroup) parameters.clone();
    }

    /**
     * Returns the parameter values.
     */
    public ParameterValueGroup getParameterValues() {
        return (ParameterValueGroup) parameters.clone();
    }

    /**
     * {@inheritDoc}
     */
    protected String formatWKT(final Formatter formatter) {
        final String name = super.formatWKT(formatter);
        formatter.append(parameters);
        return name;
    }
}
