annotate backend/src/main/java/org/dive4elements/river/importer/ImportRange.java @ 7003:3d83a4cf33ba double-precision

Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
author Tom Gottfried <tom.gottfried@intevation.de>
date Thu, 12 Sep 2013 18:58:19 +0200
parents 4c3ccf2b0304
children 70ab9e8cdefb
rev   line source
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5992
4c3ccf2b0304 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5844
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5992
4c3ccf2b0304 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5844
diff changeset
6 * documentation coming with Dive4Elements River for details.
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
9 package org.dive4elements.river.importer;
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
11 import org.dive4elements.river.model.Range;
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
12 import org.dive4elements.river.model.River;
188
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
13
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
14 import java.math.BigDecimal;
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
15
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
16 import org.apache.log4j.Logger;
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
17
4713
2c99995395f5 Documentation.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 500
diff changeset
18 /** A range that is about to be imported. */
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 public class ImportRange
186
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
20 implements Comparable<ImportRange>
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 {
4713
2c99995395f5 Documentation.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 500
diff changeset
22 /** Private logger. */
188
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
23 private static Logger log = Logger.getLogger(ImportRange.class);
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
24
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
25 protected BigDecimal a;
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
26 protected BigDecimal b;
188
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
27
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
28 protected Range peer;
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 public ImportRange() {
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 }
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
33 public ImportRange(BigDecimal a, BigDecimal b) {
7003
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
34
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
35 // enforce a<b and set only a for zero-length ranges
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
36 if (a != null && b == null) {
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
37 this.a = a;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
38 this.b = null;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
39 }
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
40 else if (a == null && b != null) {
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
41 this.a = b;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
42 this.b = null;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
43 }
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
44 else if (a == null && b == null) {
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
45 throw new IllegalArgumentException("Both a and b are null.");
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
46 }
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
47 else if (a == b) {
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
48 this.a = a;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
49 this.b = null;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
50 }
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
51 else {
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
52 if (a.compareTo(b) == 1) {
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
53 BigDecimal t = a; a = b; b = t;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
54 }
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
55 this.a = a;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
56 this.b = b;
3d83a4cf33ba Range import: enforce a<b not only for WSTs and set only a for zero-length ranges.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5992
diff changeset
57 }
186
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
58 }
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
59
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
60 private static final int compare(BigDecimal a, BigDecimal b) {
186
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
61 if (a == null && b == null) {
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
62 return 0;
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
63 }
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
64 if (a == null && b != null) {
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
65 return -1;
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
66 }
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
67 if (a != null && b == null) {
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
68 return +1;
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
69 }
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
70 return a.compareTo(b);
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
71 }
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
72
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
73 public int compareTo(ImportRange other) {
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
74 int cmp = compare(a, other.a);
186
cf8cbcb6a10d Added parser to read *.KM files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 185
diff changeset
75 if (cmp != 0) return cmp;
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
76 return compare(b, other.b);
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 }
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
79 public BigDecimal getA() {
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
80 return a;
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 }
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
83 public void setA(BigDecimal a) {
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
84 this.a = a;
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
189
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
87 public BigDecimal getB() {
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
88 return b;
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
89 }
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
90
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
91 public void setB(BigDecimal b) {
bc3747a371cc First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 188
diff changeset
92 this.b = b;
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
188
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
94
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
95 public Range getPeer(River river) {
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
96 if (peer == null) {
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
97 peer = ImporterSession.getInstance().getRange(river, a, b);
188
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
98 }
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
99 return peer;
003ac16812dd Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
100 }
185
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 }
a60edcfe5f53 Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org