teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; ingo@322: teichmann@5831: import org.dive4elements.artifacts.Artifact; ingo@921: teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; ingo@921: teichmann@5831: import org.dive4elements.river.artifacts.access.RangeAccess; teichmann@4949: teichmann@4949: import org.apache.log4j.Logger; ingo@921: ingo@322: ingo@322: /** felix@4865: * State in which km range is set. ingo@322: * @author Ingo Weinzierl ingo@322: */ ingo@322: public abstract class RangeState extends DefaultState { ingo@322: felix@1685: /** 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@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: { teichmann@5867: D4EArtifact flys = (D4EArtifact) artifact; ingo@921: ingo@921: try { felix@4865: RangeAccess rangeAccess = new RangeAccess(flys, null); felix@4865: double from = rangeAccess.getFrom(); felix@4865: double to = rangeAccess.getTo(); felix@4865: double step = rangeAccess.getStep(); 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: } felix@4865: catch (NullPointerException npe) { felix@4865: throw new IllegalArgumentException("error_empty_state"); felix@4865: } ingo@921: } ingo@322: } ingo@322: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :