Search in sources :

Example 1 with UnspecifiedParameterException

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

the class ObjectListParameter method parseValue.

@SuppressWarnings("unchecked")
@Override
protected List<Class<? extends C>> parseValue(Object obj) throws ParameterException {
    if (obj == null) {
        throw new UnspecifiedParameterException(this);
    }
    if (List.class.isInstance(obj)) {
        List<?> l = (List<?>) obj;
        ArrayList<C> inst = new ArrayList<>(l.size());
        ArrayList<Class<? extends C>> classes = new ArrayList<>(l.size());
        for (Object o : l) {
            // does the given objects class fit?
            if (restrictionClass.isInstance(o)) {
                inst.add((C) o);
                classes.add((Class<? extends C>) o.getClass());
            } else if (o instanceof Class) {
                if (restrictionClass.isAssignableFrom((Class<?>) o)) {
                    inst.add(null);
                    classes.add((Class<? extends C>) o);
                } else {
                    throw new WrongParameterValueException(this, ((Class<?>) o).getName(), "Given class not a subclass / implementation of " + restrictionClass.getName());
                }
            } else {
                throw new WrongParameterValueException(this, o.getClass().getName(), "Given instance not an implementation of " + restrictionClass.getName());
            }
        }
        this.instances = inst;
        return super.parseValue(classes);
    }
    // Did we get a single instance?
    try {
        C inst = restrictionClass.cast(obj);
        this.instances = Arrays.asList(inst);
        return super.parseValue(inst.getClass());
    } catch (ClassCastException e) {
    // Continue
    }
    return super.parseValue(obj);
}
Also used : WrongParameterValueException(de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)

Example 2 with UnspecifiedParameterException

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

the class MiniGUI method reportErrors.

/**
 * Report errors in a single error log record.
 *
 * @param config Parameterization
 */
protected void reportErrors(SerializedParameterization config) {
    StringBuilder buf = new StringBuilder(500).append("Task is not completely configured:").append(NEWLINE).append(NEWLINE);
    for (ParameterException e : config.getErrors()) {
        if (e instanceof UnspecifiedParameterException) {
            // 
            buf.append("The parameter ").append(// 
            ((UnspecifiedParameterException) e).getParameterName()).append(" is required.").append(NEWLINE);
        } else {
            buf.append(e.getMessage()).append(NEWLINE);
        }
    }
    LOG.warning(buf.toString());
    config.clearErrors();
}
Also used : ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)

Example 3 with UnspecifiedParameterException

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

the class ClassListParameter method instantiateClasses.

/**
 * Returns a list of new instances for the value (i.e., the class name) of
 * this class list parameter. The instances have the type of the restriction
 * class of this class list parameter.
 * <p/>
 * If the Class for the class names is not found, the instantiation is tried
 * using the package of the restriction class as package of the class name.
 *
 * @param config Parameterization to use (if Parameterizable))
 * @return a list of new instances for the value of this class list parameter
 */
public List<C> instantiateClasses(Parameterization config) {
    config = config.descend(this);
    List<C> instances = new ArrayList<>();
    if (getValue() == null) {
        config.reportError(new UnspecifiedParameterException(this));
        // empty list.
        return instances;
    }
    for (Class<? extends C> cls : getValue()) {
        // in sync!
        try {
            instances.add(ClassGenericsUtil.tryInstantiate(restrictionClass, cls, config));
        } catch (Exception e) {
            config.reportError(new WrongParameterValueException(this, cls.getName(), e.getMessage(), e));
        }
    }
    return instances;
}
Also used : WrongParameterValueException(de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException) ArrayList(java.util.ArrayList) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) WrongParameterValueException(de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException) ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)

Aggregations

UnspecifiedParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)3 ParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)2 WrongParameterValueException (de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)1