All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax0.jamal.tools.param.EnumerationParameter Maven / Gradle / Ivy

package javax0.jamal.tools.param;

import javax0.jamal.api.BadSyntax;
import javax0.jamal.tools.Params;

public class EnumerationParameter extends AbstractTypedParameter {

    public EnumerationParameter(Params.Param param, Class enumClass) {
        super(param);
        this.enumClass = enumClass;
    }

    private final Class enumClass;
    private Object enumDefault = null;

    public EnumerationParameter required() {
        checkDone(DoneAction.REQUIRED);
        return this;
    }

    public EnumerationParameter optional() {
        checkDone(DoneAction.OPTIONAL);
        enumDefault = enumClass.getEnumConstants()[0];
        return this;
    }

    public  EnumerationParameter defaultValue(K enumDefault) {
        checkDone(DoneAction.DEFAULT);
        if (enumDefault != null && !enumDefault.getClass().isAssignableFrom(enumClass)) {
            throw new IllegalArgumentException(String.format("The parameter '%s%%%s' is not '%s'. This is a coding error in the macro.", enumDefault, enumDefault.getClass(), enumClass.getName()));
        }
        this.enumDefault = enumDefault;
        return this;
    }

    /**
     * Get the value of the parameter as an optional enumeration object.
     * If the parameter was not specified, then the returned optional will contain the default value if it was
     * specified, or it will be empty.
     *
     * @param klass the enumeration type. Has to be the same time specified in the parameter of the method
     *              {@link javax0.jamal.tools.Scanner.ScannerObject#enumeration(Class) scanner.enumeration()}.
     *              If the type is different, a runtime exception is thrown.
     * @param    the type of the enumeration
     * @return the value of the parameter as an optional enumeration object.
     * @throws BadSyntax if the parameter was specified multiple times.
     */
    public  K get(Class klass) throws BadSyntax {
        BadSyntax.when(enumClass == null, "The parameter '%s' is not an enumeration", param.name());
        final var ignore = param.get();
        if (enumDefault == null) {
            throw new IllegalArgumentException("The parameter '" + param.name() + "' has no default value.");
        }
        if (!enumClass.isAssignableFrom(klass)) {
            throw new IllegalArgumentException(String.format("The parameter '%s' is not '%s' type", param.name(), klass.getName()));
        }
        if (param.isPresent()) {
            try {
                final var enumValue = Enum.valueOf((Class) enumClass, param.name());
                return (K) enumValue;
            } catch (IllegalArgumentException e) {
                throw new BadSyntax("The value '" + param.get() + "' is not a valid value for the enumeration " + enumClass.getName());
            }
        } else {
            return (K) enumDefault;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy