MatrixParameters changes for revisions 7705:7860

In this commit:

Command line:

svn diff --extensions "--unified --ignore-space-change --ignore-all-space --ignore-eol-style" -r7705:7860 https://svn.osgeo.org/geotools/trunk/modules/library/referencing/src/main/java/org/geotools/parameter/MatrixParameters.java
Revision 7705Revision 7860
 */
package org.geotools.parameter;

// J2SE dependencies
import java.util.Locale;

// OpenGIS dependencies
import org.opengis.metadata.Identifier;
import org.opengis.referencing.operation.Matrix;
import org.opengis.parameter.ParameterValue;
import org.opengis.parameter.OperationParameter;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.OperationParameterGroup;
import org.opengis.parameter.GeneralOperationParameter;
import org.opengis.parameter.ParameterNotFoundException;

// Geotools dependencies
 */
package org.geotools.parameter;

// OpenGIS dependencies
import java.util.ArrayList;
import java.util.List;

import org.opengis.metadata.Identifier;
import org.opengis.referencing.operation.Matrix;
import org.opengis.util.InternationalString;
import org.opengis.parameter.ParameterValue;
import org.opengis.parameter.ParameterDescriptor;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterDescriptorGroup;
import org.opengis.parameter.GeneralParameterDescriptor;
import org.opengis.parameter.ParameterNotFoundException;

// Geotools dependencies
* The values for a group of {@linkplain MatrixParameters matrix parameters}. This value group
* is extensible, i.e. the number of <code>"elt_<var>row</var>_<var>col</var>"</code> parameters
* depends on the <code>"num_row"</code> and <code>"num_col"</code> parameter values. Concequently,
* this {@linkplain ParameterValueGroup parameter value group} is also its own mutable
* {@linkplain OperationParameterGroup operation parameter group}.
*
* @version $Id$
* @author Martin Desruisseaux
* The values for a group of {@linkplain MatrixParameters matrix parameters}. This value group
* is extensible, i.e. the number of <code>"elt_<var>row</var>_<var>col</var>"</code> parameters
* depends on the <code>"num_row"</code> and <code>"num_col"</code> parameter values. Concequently,
* this {@linkplain ParameterGroup parameter value group} is also its own mutable
* {@linkplain ParameterGroupDescriptor operation parameter group}.
*
* @version $Id$
* @author Martin Desruisseaux
 *
 * @see MatrixParameters
 */
public class MatrixParameterValues extends ParameterValueGroup implements OperationParameterGroup {
    /**
     * The parameter values. Will be constructed only when first requested.
     */
    private ParameterValue[][] values;

    /**
     * The value for the {@link MatrixParameters#numRow} parameter.
 *
 * @see MatrixParameters
 */
public class MatrixParameterValues extends ParameterGroup implements ParameterDescriptorGroup {
    private static final long serialVersionUID = 1L;

    /**
     * The parameter values. Will be constructed only when first requested.
     */
    private ParameterValue[][] matrixValues;

    /**
     * The value for the {@link MatrixParameters#numRow} parameter.
 * since the description depends on <code>"num_row"</code> and <code>"num_col"</code>
 * parameter values.
 */
public GeneralOperationParameter getDescriptor() {
    return this;
}
 * since the description depends on <code>"num_row"</code> and <code>"num_col"</code>
 * parameter values.
 */
public GeneralParameterDescriptor getDescriptor() {
    return this;
}
 * Forward the call to the {@linkplain MatrixParameters matrix parameters} descriptor
 * specified at construction time.
 */
public String getName(final Locale locale) {
    return descriptor.getName(locale);
}

/**
 * Forward the call to the {@linkplain MatrixParameters matrix parameters} descriptor
 * specified at construction time.
 */
public InternationalString getName() {
    return descriptor.getName();
}

/**
 * Forward the call to the {@linkplain MatrixParameters matrix parameters} descriptor
 * specified at construction time.
 */
public String getRemarks(Locale locale) {
    return descriptor.getRemarks(locale);
}

/**
 * Forward the call to the {@linkplain MatrixParameters matrix parameters} descriptor
 * specified at construction time.
 */
public InternationalString getRemarks() {
    return descriptor.getRemarks();
}

/**
 * @return The parameter for the given name.
 * @throws ParameterNotFoundException if there is no parameter for the given name.
 */
public OperationParameter getParameter(final String name)
        throws ParameterNotFoundException
{
    return ((MatrixParameters) descriptor).getParameter(name, numRow.intValue(),
 * @return The parameter for the given name.
 * @throws ParameterNotFoundException if there is no parameter for the given name.
 */
public ParameterDescriptor getParameter(final String name)
        throws ParameterNotFoundException
{
    return ((MatrixParameters) descriptor).getParameter(name, numRow.intValue(),
 * class for other parameters, especially "num_row" and "num_col".
 */
try {
    return super.getValue(name);
} catch (ParameterNotFoundException exception) {
    if (cause!=null) try {
        exception.initCause(cause);
 * class for other parameters, especially "num_row" and "num_col".
 */
try {
    return super.parameter(name);
} catch (ParameterNotFoundException exception) {
    if (cause!=null) try {
        exception.initCause(cause);
{
    return getValue(row, column, numRow.intValue(), numCol.intValue());
}

/**
 * Implementation of {@link #getValue(int,int)}.
 *
{
    return getValue(row, column, numRow.intValue(), numCol.intValue());
}
/**
 * Implementation of {@link #getValue(int,int)}.
 *
{
    MatrixParameters.checkIndice("row",    row,    numRow);
    MatrixParameters.checkIndice("column", column, numCol);
    if (values == null) {
        values = new ParameterValue[numRow][];
    }
    if (row >= values.length) {
        values = (ParameterValue[][]) XArray.resize(values, numRow);
    }
    ParameterValue[] rowValues = values[row];
    if (rowValues == null) {
        values[row] = rowValues = new ParameterValue[numCol];
    }
    if (column >= rowValues.length) {
        values[row] = rowValues = (ParameterValue[]) XArray.resize(rowValues, numCol);
    }
    ParameterValue param = rowValues[column];
    if (param == null) {
        rowValues[column] = param = new ParameterRealValue(
                ((MatrixParameters) descriptor).getParameter(row, column, numRow, numCol));
    }
    return param;
}

/**
 * Returns the parameters descriptors in this group. The amount of parameters depends
 * on the value of <code>"num_row"</code> and <code>"num_col"</code> parameters.
 */
public GeneralOperationParameter[] getParameters() {
    return ((MatrixParameters) descriptor).getParameters(numRow.intValue(),
                                                         numCol.intValue());
}

/**
 * Returns the parameters values in this group. The amount of parameters depends
 * on the value of <code>"num_row"</code> and <code>"num_col"</code> parameters.
{
    MatrixParameters.checkIndice("row",    row,    numRow);
    MatrixParameters.checkIndice("column", column, numCol);
    if (matrixValues == null) {
        matrixValues = new ParameterValue[numRow][];
    }
    if (row >= matrixValues.length) {
        matrixValues = (ParameterValue[][]) XArray.resize(matrixValues, numRow);
    }
    ParameterValue[] rowValues = matrixValues[row];
    if (rowValues == null) {
        matrixValues[row] = rowValues = new ParameterValue[numCol];
    }
    if (column >= rowValues.length) {
        matrixValues[row] = rowValues = (ParameterValue[]) XArray.resize(rowValues, numCol);
    }
    ParameterValue param = rowValues[column];
    if (param == null) {
        rowValues[column] = param = new ParameterReal(
                ((MatrixParameters) descriptor).getParameter(row, column, numRow, numCol));
    }
    return param;
}
public final void delValue(final int row, final int column){
    delValue(row, column, numRow.intValue(), numCol.intValue());
}
public void delValue( int row, int column, int numRow, int numCol ){
    if( matrixValues == null ){
        return; // nothing to remove
    }
    final ParameterValue[] rowValues = matrixValues[column];
    if( rowValues == null ){
        // nothing there
        return;
    }
    if ( row < rowValues.length) {
        rowValues[row] = null;
    }
}
/**
 * Returns the parameters descriptors in this group. The amount of parameters depends
 * on the value of <code>"num_row"</code> and <code>"num_col"</code> parameters.
 */
public GeneralParameterDescriptor[] getParameters() {
    return ((MatrixParameters) descriptor).getParameters(numRow.intValue(),
                                                         numCol.intValue());
}

/* (non-Javadoc)
 * @see org.geotools.parameter.ParameterGroup#values()
 */
public List values() {
    GeneralParameterValue[] params = getValues();
    List list = new ArrayList();
    for( int i=0; i<params.length; i++ ){
        list.add( params[i] );
    }
    return list;
}
/**
 * Returns the parameters values in this group. The amount of parameters depends
 * on the value of <code>"num_row"</code> and <code>"num_col"</code> parameters.
int k = 0;
parameters[k++] = this.numRow;
parameters[k++] = this.numCol;
if (values != null) {
    final int maxRow = Math.min(numRow, values.length);
    for (int j=0; j<maxRow; j++) {
        final ParameterValue[] rowValues = values[j];
        if (rowValues != null) {
            final int maxCol = Math.min(numCol, rowValues.length);
            for (int i=0; i<maxCol; i++) {
int k = 0;
parameters[k++] = this.numRow;
parameters[k++] = this.numCol;
if (matrixValues != null) {
    final int maxRow = Math.min(numRow, matrixValues.length);
    for (int j=0; j<maxRow; j++) {
        final ParameterValue[] rowValues = matrixValues[j];
        if (rowValues != null) {
            final int maxCol = Math.min(numCol, rowValues.length);
            for (int i=0; i<maxCol; i++) {
final int numRow = this.numRow.intValue();
final int numCol = this.numCol.intValue();
final GeneralMatrix matrix = new GeneralMatrix(numRow, numCol);
if (values != null) {
    for (int j=0; j<numRow; j++) {
        final ParameterValue[] row = values[j];
        if (row != null) {
            for (int i=0; i<numCol; i++) {
                final ParameterValue element = row[i];
final int numRow = this.numRow.intValue();
final int numCol = this.numCol.intValue();
final GeneralMatrix matrix = new GeneralMatrix(numRow, numCol);
if (matrixValues != null) {
    for (int j=0; j<numRow; j++) {
        final ParameterValue[] row = matrixValues[j];
        if (row != null) {
            for (int i=0; i<numCol; i++) {
                final ParameterValue element = row[i];
 * @param matrix The matrix to copy in this group of parameters.
 */
public void setMatrix(final Matrix matrix) {
    final MatrixParameters descriptor = ((MatrixParameters) this.descriptor);
    final int numRow = matrix.getNumRow();
    final int numCol = matrix.getNumCol();
    this.numRow.setValue(numRow);
    this.numCol.setValue(numCol);
    for (int j=0; j<numRow; j++) {
        for (int i=0; i<numCol; i++) {
            final double element = matrix.getElement(j,i);
            final Object def = descriptor.getParameter(j,i, numRow, numCol).getDefaultValue();
            if (!(def instanceof Number) || element != ((Number)def).doubleValue()) {
                getValue(j,i, numRow, numCol).setValue(element);
            } else {
                /*
                 * The matrix element has the default value. Delete the corresponding
                 * entry from this parameter value group.
                 */
                if (values!=null && j<values.length) {
                    final ParameterValue[] rowValues = values[j];
                    if (rowValues!=null && i<rowValues.length) {
                        rowValues[i] = null;
                    }
                }
            }
        }
    }
}

/**
 * Compare this object with the specified one for equality.
 * @param matrix The matrix to copy in this group of parameters.
 */
public void setMatrix(final Matrix matrix) {
    final MatrixParameters matrixDescriptor = ((MatrixParameters) this.descriptor);
    final int numRow = matrix.getNumRow();
    final int numCol = matrix.getNumCol();
    this.numRow.setValue(numRow);
    this.numCol.setValue(numCol);
    for (int row=0; row<numRow; row++) {
        for (int col=0; col<numCol; col++) {
            final double element = matrix.getElement(row,col);
            ParameterDescriptor descriptor = matrixDescriptor.getParameter( row, col );
            final Object defaultValue = descriptor.getDefaultValue();
            if( defaultValue instanceof Number ){
                double value = ((Number) defaultValue ).doubleValue();
                if( element != value ){
                    // ParameterValue paramValue = getValue( row, col );
                    if( matrixValues == null ){
                        matrixValues = new ParameterValue[ numRow ][];
                    }
                    if( matrixValues[ row] == null ){
                        matrixValues[ row ] = new ParameterValue[ numCol ];
                }
                    ParameterValue realValue = new ParameterReal( descriptor, element );
                    matrixValues[ row ][ col ] = realValue;
            }
                else {
                    // remove entry to keep things sparse
                    if( matrixValues != null && matrixValues[row] != null &&
                        matrixValues[row][col] != null ){
                        matrixValues[row][col] = null;
        }
    }
}
            else {
                // remove entry
                if( matrixValues != null && matrixValues[row] != null &&
                    matrixValues[row][col] != null ){
                    matrixValues[row][col] = null;
                }
            }
        }
    }
}

/**
 * Compare this object with the specified one for equality.
 */
public Object clone() {
    final MatrixParameterValues copy = (MatrixParameterValues) super.clone();
    if (copy.values != null) {
        copy.numRow = (ParameterValue)     copy.getValue(0);
        copy.numCol = (ParameterValue)     copy.getValue(1);
        copy.values = (ParameterValue[][]) copy.values.clone();
        for (int j=0; j<copy.values.length; j++) {
            ParameterValue[] array = copy.values[j];
            if (array != null) {
                copy.values[j] = array = (ParameterValue[]) array.clone();
                for (int i=0; i<array.length; i++) {
                    if (array[i] != null) {
                        array[i] = (ParameterValue) array[i].clone();
 */
public Object clone() {
    final MatrixParameterValues copy = (MatrixParameterValues) super.clone();
    if (copy.matrixValues != null) {
        copy.numRow = (ParameterValue)     copy.getValue(0);
        copy.numCol = (ParameterValue)     copy.getValue(1);
        copy.matrixValues = (ParameterValue[][]) copy.matrixValues.clone();
        for (int j=0; j<copy.matrixValues.length; j++) {
            ParameterValue[] array = copy.matrixValues[j];
            if (array != null) {
                copy.matrixValues[j] = array = (ParameterValue[]) array.clone();
                for (int i=0; i<array.length; i++) {
                    if (array[i] != null) {
                        array[i] = (ParameterValue) array[i].clone();