comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/RangeState.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/RangeState.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.states;
2
3 import org.dive4elements.artifacts.Artifact;
4
5 import org.dive4elements.river.artifacts.FLYSArtifact;
6
7 import org.dive4elements.river.artifacts.access.RangeAccess;
8
9 import org.apache.log4j.Logger;
10
11
12 /**
13 * State in which km range is set.
14 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
15 */
16 public abstract class RangeState extends DefaultState {
17
18 /** The logger that is used in this class. */
19 private Logger logger = Logger.getLogger(RangeState.class);
20
21
22 public RangeState() {
23 }
24
25 protected abstract double[] getMinMax(Artifact artifact);
26
27
28 protected boolean validateBounds(
29 double fromValid, double toValid,
30 double from, double to)
31 throws IllegalArgumentException
32 {
33 if (from < fromValid) {
34 logger.error(
35 "Invalid 'from'. " + from + " is smaller than " + fromValid);
36 throw new IllegalArgumentException("error_feed_from_out_of_range");
37 }
38 else if (to > toValid) {
39 logger.error(
40 "Invalid 'to'. " + to + " is bigger than " + toValid);
41 throw new IllegalArgumentException("error_feed_to_out_of_range");
42 }
43
44 return true;
45 }
46
47
48 /**
49 * Validates a given range with a given valid range.
50 *
51 * @param fromValid Valid lower value of the range.
52 * @param toValid Valid upper value of the range.
53 * @param from The lower value.
54 * @param to The upper value.
55 * @param step The step width.
56 *
57 * @return true, if everything was fine, otherwise an exception is thrown.
58 */
59 protected boolean validateBounds(
60 double fromValid, double toValid,
61 double from, double to, double step)
62 throws IllegalArgumentException
63 {
64 logger.debug("RangeState.validateRange");
65
66 // XXX The step width is not validated at the moment!
67 return validateBounds(fromValid, toValid, from, to);
68 }
69
70
71 @Override
72 public boolean validate(Artifact artifact)
73 throws IllegalArgumentException
74 {
75 FLYSArtifact flys = (FLYSArtifact) artifact;
76
77 try {
78 RangeAccess rangeAccess = new RangeAccess(flys, null);
79 double from = rangeAccess.getFrom();
80 double to = rangeAccess.getTo();
81 double step = rangeAccess.getStep();
82
83 double[] minmax = getMinMax(flys);
84
85 return validateBounds(minmax[0], minmax[1], from, to, step);
86 }
87 catch (NumberFormatException nfe) {
88 throw new IllegalArgumentException("error_invalid_double_value");
89 }
90 catch (NullPointerException npe) {
91 throw new IllegalArgumentException("error_empty_state");
92 }
93 }
94 }
95 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org