Search in sources :

Example 6 with WrongParameterValueException

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException in project elki by elki-project.

the class DoubleArrayListParameter method parseValue.

@SuppressWarnings("unchecked")
@Override
protected List<double[]> parseValue(Object obj) throws ParameterException {
    try {
        List<?> l = List.class.cast(obj);
        // do extra validation:
        for (Object o : l) {
            if (!(o instanceof double[])) {
                throw new WrongParameterValueException("Wrong parameter format for parameter \"" + getOptionID().getName() + "\". Given list contains objects of different type!");
            }
        }
        // TODO: Should we copy the list and vectors?
        return (List<double[]>) l;
    } catch (ClassCastException e) {
    // continue with other attempts.
    }
    if (obj instanceof String) {
        String[] vectors = VECTOR_SPLIT.split((String) obj);
        if (vectors.length == 0) {
            throw new WrongParameterValueException("Wrong parameter format! Given list of vectors for parameter \"" + getOptionID().getName() + "\" is empty!");
        }
        ArrayList<double[]> vecs = new ArrayList<>();
        double[] buf = new double[11];
        int used = 0;
        for (String vector : vectors) {
            used = 0;
            String[] coordinates = SPLIT.split(vector);
            for (String coordinate : coordinates) {
                try {
                    if (used == buf.length) {
                        buf = Arrays.copyOf(buf, buf.length << 1);
                    }
                    buf[used++] = ParseUtil.parseDouble(coordinate);
                } catch (NumberFormatException e) {
                    throw new WrongParameterValueException("Wrong parameter format! Coordinates of vector \"" + vector + "\" are not valid!");
                }
            }
            vecs.add(Arrays.copyOf(buf, used));
        }
        return vecs;
    }
    throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getOptionID().getName() + "\" requires a list of double values!");
}
Also used : WrongParameterValueException(de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ParameterConstraint(de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint)

Aggregations

WrongParameterValueException (de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException)6 ArrayList (java.util.ArrayList)5 UnspecifiedParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)3 List (java.util.List)3 ParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)2 ParameterConstraint (de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint)1 Parameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization)1 Flag (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag)1 StringParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter)1 File (java.io.File)1