Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java @ 658:ed7c901ee712
If Artifact.feed() fails do not store invalid values in database.
flys-artifacts/trunk@2062 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 07 Jun 2011 11:43:30 +0000 |
parents | 913b52064449 |
children | 8124ca1ecaaa |
rev | line source |
---|---|
650
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.model; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.List; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import java.util.ArrayList; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 import java.io.Serializable; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import org.apache.log4j.Logger; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 import gnu.trove.TDoubleArrayList; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 |
655
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
12 import de.intevation.flys.utils.DoubleUtil; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
13 |
650
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 public class Segment |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 implements Serializable |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 private static Logger logger = Logger.getLogger(Segment.class); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 protected double from; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 protected double to; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 protected double [] values; |
655
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
22 protected double [] backup; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
23 protected double referencePoint; |
650
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 public Segment() { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
655
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
28 public Segment(double referencePoint) { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
29 this.referencePoint = referencePoint; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
30 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
31 |
650
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 public Segment(double from, double to, double [] values) { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 this.from = from; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 this.to = to; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 this.values = values; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 public boolean isUp() { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 return from < to; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 public String toString() { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 StringBuilder sb = new StringBuilder("Segment: ["); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 sb.append("from: ").append(from).append("; to: ") |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 .append(to).append("; values: ("); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 for (int i = 0; i < values.length; ++i) { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 if (i > 0) sb.append(", "); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 sb.append(values[i]); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 sb.append(")]"); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 return sb.toString(); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 |
655
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
54 public void setFrom(double from) { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
55 this.from = from; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
56 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
57 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
58 public void backup() { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
59 backup = (double [])values.clone(); |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
60 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
61 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
62 public double getFrom() { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
63 return from; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
64 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
65 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
66 public void setTo(double to) { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
67 this.to = to; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
68 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
69 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
70 public double getTo() { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
71 return to; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
72 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
73 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
74 public void setValues(double [] values) { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
75 this.values = values; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
76 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
77 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
78 public double [] getValues() { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
79 return values; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
80 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
81 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
82 public void setReferencePoint(double referencePoint) { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
83 this.referencePoint = referencePoint; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
84 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
85 |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
86 public double getReferencePoint() { |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
87 return referencePoint; |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
88 } |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
89 |
650
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 public static List<Segment> parseSegments(String input) { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 ArrayList<Segment> segments = new ArrayList<Segment>(); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 TDoubleArrayList vs = new TDoubleArrayList(); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 for (String segmentStr: input.split(":")) { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 String [] parts = segmentStr.split(";"); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 if (parts.length < 3) { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 logger.warn("invalid segment: '" + segmentStr + "'"); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 continue; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 try { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 double from = Double.parseDouble(parts[0].trim()); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 double to = Double.parseDouble(parts[1].trim()); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 vs.clear(); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 for (String valueStr: parts[2].split(",")) { |
655
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
109 vs.add(DoubleUtil.round( |
913b52064449
Refactored version of "Berechnung 4"
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
650
diff
changeset
|
110 Double.parseDouble(valueStr.trim()))); |
650
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 double [] values = vs.toNativeArray(); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 segments.add(new Segment(from, to, values)); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 catch (NumberFormatException nfe) { |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 logger.warn("invalid segment: '" + segmentStr + "'"); |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 return segments; |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 } |
6695e03e9b82
Added parser for segments
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |