Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportRange.java @ 7730:e1b831fe435a slt-simplify-cross-sections
Merged default into slt-simplify-cross-sections branch and updated package and class names.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Mon, 20 Jan 2014 14:04:20 +0100 |
parents | 4b26fd60105f |
children | 9d2e69f971f5 |
comparison
equal
deleted
inserted
replaced
5084:ca45dd039b54 | 7730:e1b831fe435a |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.importer; | |
10 | |
11 import org.dive4elements.river.model.Range; | |
12 import org.dive4elements.river.model.River; | |
13 | |
14 import java.math.BigDecimal; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 /** A range that is about to be imported. */ | |
19 public class ImportRange | |
20 implements Comparable<ImportRange> | |
21 { | |
22 /** Private logger. */ | |
23 private static Logger log = Logger.getLogger(ImportRange.class); | |
24 | |
25 protected BigDecimal a; | |
26 protected BigDecimal b; | |
27 | |
28 protected Range peer; | |
29 | |
30 public ImportRange() { | |
31 } | |
32 | |
33 public ImportRange(BigDecimal a, BigDecimal b) { | |
34 | |
35 // enforce a<b and set only a for zero-length ranges | |
36 if (a != null && b == null) { | |
37 this.a = a; | |
38 this.b = null; | |
39 } | |
40 else if (a == null && b != null) { | |
41 this.a = b; | |
42 this.b = null; | |
43 } | |
44 else if (a == null && b == null) { | |
45 throw new IllegalArgumentException("Both a and b are null."); | |
46 } | |
47 else if (a == b) { | |
48 this.a = a; | |
49 this.b = null; | |
50 } | |
51 else { | |
52 if (a.compareTo(b) > 0) { | |
53 BigDecimal t = a; a = b; b = t; | |
54 } | |
55 this.a = a; | |
56 this.b = b; | |
57 } | |
58 } | |
59 | |
60 private static final int compare(BigDecimal a, BigDecimal b) { | |
61 if (a == null && b == null) { | |
62 return 0; | |
63 } | |
64 if (a == null && b != null) { | |
65 return -1; | |
66 } | |
67 if (a != null && b == null) { | |
68 return +1; | |
69 } | |
70 return a.compareTo(b); | |
71 } | |
72 | |
73 public int compareTo(ImportRange other) { | |
74 int cmp = compare(a, other.a); | |
75 if (cmp != 0) return cmp; | |
76 return compare(b, other.b); | |
77 } | |
78 | |
79 public BigDecimal getA() { | |
80 return a; | |
81 } | |
82 | |
83 public void setA(BigDecimal a) { | |
84 if (this.b != null && a.compareTo(b) >= 0) { | |
85 throw new IllegalArgumentException("a (" + a + ") must be smaller than b (" + b + ")."); | |
86 } | |
87 this.a = a; | |
88 } | |
89 | |
90 public BigDecimal getB() { | |
91 return b; | |
92 } | |
93 | |
94 public void setB(BigDecimal b) { | |
95 if (b != null && b.compareTo(a) <= 0) { | |
96 throw new IllegalArgumentException("b (" + b + ") must be greater than a (" + a + ") or null."); | |
97 } | |
98 this.b = b; | |
99 } | |
100 | |
101 public Range getPeer(River river) { | |
102 if (peer == null) { | |
103 peer = ImporterSession.getInstance().getRange(river, a, b); | |
104 } | |
105 return peer; | |
106 } | |
107 } | |
108 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |