comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RangeState.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 022f62c75878
children 1358d0c8481c
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
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 protected boolean validateBounds(
32 double fromValid, double toValid,
33 double from, double to)
34 throws IllegalArgumentException
35 {
36 if (from < fromValid) {
37 logger.error(
38 "Invalid 'from'. " + from + " is smaller than " + fromValid);
39 throw new IllegalArgumentException("error_feed_from_out_of_range");
40 }
41 else if (to > toValid) {
42 logger.error(
43 "Invalid 'to'. " + to + " is bigger than " + toValid);
44 throw new IllegalArgumentException("error_feed_to_out_of_range");
45 }
46
47 return true;
48 }
49
50
51 /**
52 * Validates a given range with a given valid range.
53 *
54 * @param fromValid Valid lower value of the range.
55 * @param toValid Valid upper value of the range.
56 * @param from The lower value.
57 * @param to The upper value.
58 * @param step The step width.
59 *
60 * @return true, if everything was fine, otherwise an exception is thrown.
61 */
62 protected boolean validateBounds(
63 double fromValid, double toValid,
64 double from, double to, double step)
65 throws IllegalArgumentException
66 {
67 logger.debug("RangeState.validateRange");
68
69 // XXX The step width is not validated at the moment!
70 return validateBounds(fromValid, toValid, from, to);
71 }
72
73
74 @Override
75 public boolean validate(Artifact artifact)
76 throws IllegalArgumentException
77 {
78 FLYSArtifact flys = (FLYSArtifact) artifact;
79
80 StateData dFrom = getData(flys, getLowerField());
81 StateData dTo = getData(flys, getUpperField());
82 StateData dStep = getData(flys, getStepField());
83
84 String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
85 String toStr = dTo != null ? (String) dTo.getValue() : null;
86 String stepStr = dStep != null ? (String) dStep.getValue() : null;
87
88 if (fromStr == null || toStr == null || stepStr == null) {
89 throw new IllegalArgumentException("error_empty_state");
90 }
91
92 try {
93 double from = Double.parseDouble(fromStr);
94 double to = Double.parseDouble(toStr);
95 double step = Double.parseDouble(stepStr);
96
97 double[] minmax = getMinMax(flys);
98
99 return validateBounds(minmax[0], minmax[1], from, to, step);
100 }
101 catch (NumberFormatException nfe) {
102 throw new IllegalArgumentException("error_invalid_double_value");
103 }
104 }
105 }
106 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org