Mercurial > dive4elements > river
annotate backend/src/main/java/org/dive4elements/river/importer/ImportRange.java @ 8046:8c14588d1f78
ImporterSession: Load measurement stations via ranges which hold a reference to the river.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 17 Jul 2014 10:56:38 +0200 |
parents | 4b26fd60105f |
children | 9d2e69f971f5 |
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 |
7376
4b26fd60105f
Replaced more tabs with spaces + minor cosmetics (removed out commented code from double precision branch).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
7221
diff
changeset
|
35 // enforce a<b and set only a for zero-length ranges |
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
|
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 { |
7221
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
52 if (a.compareTo(b) > 0) { |
7376
4b26fd60105f
Replaced more tabs with spaces + minor cosmetics (removed out commented code from double precision branch).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
7221
diff
changeset
|
53 BigDecimal t = a; a = b; b = t; |
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
|
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) { |
7221
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
84 if (this.b != null && a.compareTo(b) >= 0) { |
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
85 throw new IllegalArgumentException("a (" + a + ") must be smaller than b (" + b + ")."); |
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
86 } |
7376
4b26fd60105f
Replaced more tabs with spaces + minor cosmetics (removed out commented code from double precision branch).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
7221
diff
changeset
|
87 this.a = a; |
185
a60edcfe5f53
Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
a60edcfe5f53
Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
189
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
188
diff
changeset
|
90 public BigDecimal getB() { |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
188
diff
changeset
|
91 return b; |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
188
diff
changeset
|
92 } |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
188
diff
changeset
|
93 |
bc3747a371cc
First part of parsing main values.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
188
diff
changeset
|
94 public void setB(BigDecimal b) { |
7221
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
95 if (b != null && b.compareTo(a) <= 0) { |
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
96 throw new IllegalArgumentException("b (" + b + ") must be greater than a (" + a + ") or null."); |
70ab9e8cdefb
Range importer: Do not allow to set invalid a or b.
Tom Gottfried <tom@intevation.de>
parents:
7003
diff
changeset
|
97 } |
7376
4b26fd60105f
Replaced more tabs with spaces + minor cosmetics (removed out commented code from double precision branch).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
7221
diff
changeset
|
98 this.b = b; |
185
a60edcfe5f53
Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 } |
188
003ac16812dd
Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
100 |
003ac16812dd
Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
101 public Range getPeer(River river) { |
003ac16812dd
Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
102 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
|
103 peer = ImporterSession.getInstance().getRange(river, a, b); |
188
003ac16812dd
Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
104 } |
003ac16812dd
Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
105 return peer; |
003ac16812dd
Store annotations in backend.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
106 } |
185
a60edcfe5f53
Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 } |
a60edcfe5f53
Added new helper models for import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |