Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 8124ca1ecaaa |
children | da7cf0e3ccaa |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import java.util.List; | |
4 import java.util.ArrayList; | |
5 | |
6 import java.io.Serializable; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import gnu.trove.TDoubleArrayList; | |
11 | |
12 import de.intevation.flys.utils.DoubleUtil; | |
13 | |
14 public class Segment | |
15 implements Serializable | |
16 { | |
17 private static Logger logger = Logger.getLogger(Segment.class); | |
18 | |
19 protected double from; | |
20 protected double to; | |
21 protected double [] values; | |
22 protected double [] backup; | |
23 protected double referencePoint; | |
24 | |
25 public Segment() { | |
26 } | |
27 | |
28 public Segment(double referencePoint) { | |
29 this.referencePoint = referencePoint; | |
30 } | |
31 | |
32 public Segment(double from, double to, double [] values) { | |
33 this.from = from; | |
34 this.to = to; | |
35 this.values = values; | |
36 } | |
37 | |
38 public boolean isUp() { | |
39 return from < to; | |
40 } | |
41 | |
42 public String toString() { | |
43 StringBuilder sb = new StringBuilder("Segment: ["); | |
44 sb.append("from: ").append(from).append("; to: ") | |
45 .append(to) | |
46 .append("; ref: ").append(referencePoint) | |
47 .append("; values: ("); | |
48 for (int i = 0; i < values.length; ++i) { | |
49 if (i > 0) sb.append(", "); | |
50 sb.append(values[i]); | |
51 } | |
52 sb.append(")]"); | |
53 return sb.toString(); | |
54 } | |
55 | |
56 public void setFrom(double from) { | |
57 this.from = from; | |
58 } | |
59 | |
60 public void backup() { | |
61 backup = (double [])values.clone(); | |
62 } | |
63 | |
64 public double getFrom() { | |
65 return from; | |
66 } | |
67 | |
68 public void setTo(double to) { | |
69 this.to = to; | |
70 } | |
71 | |
72 public double getTo() { | |
73 return to; | |
74 } | |
75 | |
76 public void setValues(double [] values) { | |
77 this.values = values; | |
78 } | |
79 | |
80 public double [] getValues() { | |
81 return values; | |
82 } | |
83 | |
84 public void setReferencePoint(double referencePoint) { | |
85 this.referencePoint = referencePoint; | |
86 } | |
87 | |
88 public double getReferencePoint() { | |
89 return referencePoint; | |
90 } | |
91 | |
92 public static List<Segment> parseSegments(String input) { | |
93 | |
94 ArrayList<Segment> segments = new ArrayList<Segment>(); | |
95 | |
96 TDoubleArrayList vs = new TDoubleArrayList(); | |
97 | |
98 for (String segmentStr: input.split(":")) { | |
99 String [] parts = segmentStr.split(";"); | |
100 if (parts.length < 3) { | |
101 logger.warn("invalid segment: '" + segmentStr + "'"); | |
102 continue; | |
103 } | |
104 try { | |
105 double from = Double.parseDouble(parts[0].trim()); | |
106 double to = Double.parseDouble(parts[1].trim()); | |
107 | |
108 vs.clear(); | |
109 | |
110 for (String valueStr: parts[2].split(",")) { | |
111 vs.add(DoubleUtil.round( | |
112 Double.parseDouble(valueStr.trim()))); | |
113 } | |
114 | |
115 double [] values = vs.toNativeArray(); | |
116 segments.add(new Segment(from, to, values)); | |
117 } | |
118 catch (NumberFormatException nfe) { | |
119 logger.warn("invalid segment: '" + segmentStr + "'"); | |
120 } | |
121 } | |
122 | |
123 return segments; | |
124 } | |
125 } | |
126 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |