comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java @ 1190:f514894ec2fd

merged flys-artifacts/2.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:17 +0200
parents 9a77a9adbb36
children cafd8af6734a
comparison
equal deleted inserted replaced
917:b48c36076e17 1190:f514894ec2fd
1 package de.intevation.flys.artifacts.states;
2
3 import org.apache.log4j.Logger;
4
5 import de.intevation.artifacts.Artifact;
6
7 import de.intevation.artifactdatabase.data.StateData;
8
9 import de.intevation.flys.artifacts.FLYSArtifact;
10
11
12 /**
13 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
14 */
15 public abstract class RangeState extends DefaultState {
16
17 /** The logger that is used in this class.*/
18 private Logger logger = Logger.getLogger(RangeState.class);
19
20
21 public RangeState() {
22 }
23
24
25 protected abstract String getLowerField();
26 protected abstract String getUpperField();
27 protected abstract String getStepField();
28 protected abstract double[] getMinMax(Artifact artifact);
29
30
31 /**
32 * Validates a given range with a given valid range.
33 *
34 * @param fromValid Valid lower value of the range.
35 * @param toValid Valid upper value of the range.
36 * @param from The lower value.
37 * @param to The upper value.
38 * @param step The step width.
39 *
40 * @return true, if everything was fine, otherwise an exception is thrown.
41 */
42 protected boolean validateBounds(
43 double fromValid, double toValid,
44 double from, double to, double step)
45 throws IllegalArgumentException
46 {
47 logger.debug("RangeState.validateRange");
48
49 if (from < fromValid) {
50 logger.error(
51 "Invalid 'from'. " + from + " is smaller than " + fromValid);
52 throw new IllegalArgumentException("error_feed_from_out_of_range");
53 }
54 else if (to > toValid) {
55 logger.error(
56 "Invalid 'to'. " + to + " is bigger than " + toValid);
57 throw new IllegalArgumentException("error_feed_to_out_of_range");
58 }
59
60 return true;
61 }
62
63
64 @Override
65 public boolean validate(Artifact artifact)
66 throws IllegalArgumentException
67 {
68 FLYSArtifact flys = (FLYSArtifact) artifact;
69
70 StateData dFrom = getData(flys, getLowerField());
71 StateData dTo = getData(flys, getUpperField());
72 StateData dStep = getData(flys, getStepField());
73
74 String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
75 String toStr = dTo != null ? (String) dTo.getValue() : null;
76 String stepStr = dStep != null ? (String) dStep.getValue() : null;
77
78 if (fromStr == null || toStr == null || stepStr == null) {
79 throw new IllegalArgumentException("error_empty_state");
80 }
81
82 try {
83 double from = Double.parseDouble(fromStr);
84 double to = Double.parseDouble(toStr);
85 double step = Double.parseDouble(stepStr);
86
87 double[] minmax = getMinMax(flys);
88
89 return validateBounds(minmax[0], minmax[1], from, to, step);
90 }
91 catch (NumberFormatException nfe) {
92 throw new IllegalArgumentException("error_invalid_double_value");
93 }
94 }
95 }
96 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org