ingo@322: package de.intevation.flys.artifacts.states; ingo@322: ingo@322: import org.apache.log4j.Logger; ingo@322: ingo@921: import de.intevation.artifacts.Artifact; ingo@921: ingo@921: import de.intevation.artifactdatabase.data.StateData; ingo@921: ingo@921: import de.intevation.flys.artifacts.FLYSArtifact; ingo@921: ingo@322: ingo@322: /** ingo@322: * @author Ingo Weinzierl ingo@322: */ ingo@322: public abstract class RangeState extends DefaultState { ingo@322: ingo@322: /** The logger that is used in this class.*/ ingo@921: private Logger logger = Logger.getLogger(RangeState.class); ingo@322: ingo@322: ingo@322: public RangeState() { ingo@322: } ingo@322: ingo@322: ingo@921: protected abstract String getLowerField(); ingo@921: protected abstract String getUpperField(); ingo@921: protected abstract String getStepField(); ingo@921: protected abstract double[] getMinMax(Artifact artifact); ingo@921: ingo@921: ingo@1630: protected boolean validateBounds( ingo@1630: double fromValid, double toValid, ingo@1630: double from, double to) ingo@1630: throws IllegalArgumentException ingo@1630: { ingo@1630: if (from < fromValid) { ingo@1630: logger.error( ingo@1630: "Invalid 'from'. " + from + " is smaller than " + fromValid); ingo@1630: throw new IllegalArgumentException("error_feed_from_out_of_range"); ingo@1630: } ingo@1630: else if (to > toValid) { ingo@1630: logger.error( ingo@1630: "Invalid 'to'. " + to + " is bigger than " + toValid); ingo@1630: throw new IllegalArgumentException("error_feed_to_out_of_range"); ingo@1630: } ingo@1630: ingo@1630: return true; ingo@1630: } ingo@1630: ingo@1630: ingo@322: /** ingo@322: * Validates a given range with a given valid range. ingo@322: * ingo@322: * @param fromValid Valid lower value of the range. ingo@322: * @param toValid Valid upper value of the range. ingo@322: * @param from The lower value. ingo@322: * @param to The upper value. ingo@322: * @param step The step width. ingo@322: * ingo@322: * @return true, if everything was fine, otherwise an exception is thrown. ingo@322: */ ingo@379: protected boolean validateBounds( ingo@322: double fromValid, double toValid, ingo@322: double from, double to, double step) ingo@322: throws IllegalArgumentException ingo@322: { ingo@322: logger.debug("RangeState.validateRange"); ingo@322: ingo@1630: // XXX The step width is not validated at the moment! ingo@1630: return validateBounds(fromValid, toValid, from, to); ingo@322: } ingo@921: ingo@921: ingo@921: @Override sascha@1050: public boolean validate(Artifact artifact) ingo@921: throws IllegalArgumentException ingo@921: { ingo@921: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@921: ingo@921: StateData dFrom = getData(flys, getLowerField()); ingo@921: StateData dTo = getData(flys, getUpperField()); ingo@921: StateData dStep = getData(flys, getStepField()); ingo@921: ingo@921: String fromStr = dFrom != null ? (String) dFrom.getValue() : null; ingo@921: String toStr = dTo != null ? (String) dTo.getValue() : null; ingo@921: String stepStr = dStep != null ? (String) dStep.getValue() : null; ingo@921: ingo@921: if (fromStr == null || toStr == null || stepStr == null) { ingo@921: throw new IllegalArgumentException("error_empty_state"); ingo@921: } ingo@921: ingo@921: try { ingo@921: double from = Double.parseDouble(fromStr); ingo@921: double to = Double.parseDouble(toStr); ingo@921: double step = Double.parseDouble(stepStr); ingo@921: ingo@921: double[] minmax = getMinMax(flys); ingo@921: ingo@921: return validateBounds(minmax[0], minmax[1], from, to, step); ingo@921: } ingo@921: catch (NumberFormatException nfe) { ingo@921: throw new IllegalArgumentException("error_invalid_double_value"); ingo@921: } ingo@921: } ingo@322: } ingo@322: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :