Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportRange.java @ 8976:e541938dd3ab
Range data handled consistently as BigDecimal to minimize the fractional part of a and b
author | mschaefer |
---|---|
date | Tue, 03 Apr 2018 10:43:53 +0200 |
parents | 5e38e2924c07 |
children |
comparison
equal
deleted
inserted
replaced
8975:a0a0a7f912ab | 8976:e541938dd3ab |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.importer; | 9 package org.dive4elements.river.importer; |
10 | 10 |
11 import org.dive4elements.river.model.Range; | |
12 import org.dive4elements.river.model.River; | |
13 | |
14 import java.math.BigDecimal; | 11 import java.math.BigDecimal; |
15 | 12 |
16 import org.apache.log4j.Logger; | 13 import org.apache.log4j.Logger; |
14 import org.dive4elements.river.model.Range; | |
15 import org.dive4elements.river.model.River; | |
17 | 16 |
18 /** A range that is about to be imported. */ | 17 /** A range that is about to be imported. */ |
19 public class ImportRange | 18 public class ImportRange |
20 implements Comparable<ImportRange> | 19 implements Comparable<ImportRange> |
21 { | 20 { |
28 protected Range peer; | 27 protected Range peer; |
29 | 28 |
30 public ImportRange() { | 29 public ImportRange() { |
31 } | 30 } |
32 | 31 |
33 public ImportRange(BigDecimal a) { | 32 public ImportRange(final BigDecimal a) { |
34 this.a = a; | 33 this.a = a; |
35 this.b = null; | 34 this.b = null; |
36 } | 35 } |
37 | 36 |
38 public ImportRange(BigDecimal a, BigDecimal b) { | 37 public ImportRange(BigDecimal a, BigDecimal b) { |
53 this.a = a; | 52 this.a = a; |
54 this.b = null; | 53 this.b = null; |
55 } | 54 } |
56 else { | 55 else { |
57 if (a.compareTo(b) > 0) { | 56 if (a.compareTo(b) > 0) { |
58 BigDecimal t = a; a = b; b = t; | 57 final BigDecimal t = a; a = b; b = t; |
59 } | 58 } |
60 this.a = a; | 59 this.a = a; |
61 this.b = b; | 60 this.b = b; |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 private static final int compare(BigDecimal a, BigDecimal b) { | 64 private static final int compare(final BigDecimal a, final BigDecimal b) { |
66 if (a == null && b == null) { | 65 if (a == null && b == null) { |
67 return 0; | 66 return 0; |
68 } | 67 } |
69 if (a == null && b != null) { | 68 if (a == null && b != null) { |
70 return -1; | 69 return -1; |
73 return +1; | 72 return +1; |
74 } | 73 } |
75 return a.compareTo(b); | 74 return a.compareTo(b); |
76 } | 75 } |
77 | 76 |
78 public int compareTo(ImportRange other) { | 77 @Override |
79 int cmp = compare(a, other.a); | 78 public int compareTo(final ImportRange other) { |
79 final int cmp = compare(this.a, other.a); | |
80 if (cmp != 0) return cmp; | 80 if (cmp != 0) return cmp; |
81 return compare(b, other.b); | 81 return compare(this.b, other.b); |
82 } | 82 } |
83 | 83 |
84 public BigDecimal getA() { | 84 public BigDecimal getA() { |
85 return a; | 85 return this.a; |
86 } | 86 } |
87 | 87 |
88 public void setA(BigDecimal a) { | 88 public void setA(final BigDecimal a) { |
89 if (this.b != null && a.compareTo(b) >= 0) { | 89 if (this.b != null && a.compareTo(this.b) >= 0) { |
90 throw new IllegalArgumentException( | 90 throw new IllegalArgumentException( |
91 "a (" + a + ") must be smaller than b (" + b + ")."); | 91 "a (" + a + ") must be smaller than b (" + this.b + ")."); |
92 } | 92 } |
93 this.a = a; | 93 this.a = a; |
94 } | 94 } |
95 | 95 |
96 public BigDecimal getB() { | 96 public BigDecimal getB() { |
97 return b; | 97 return this.b; |
98 } | 98 } |
99 | 99 |
100 public void setB(BigDecimal b) { | 100 public void setB(final BigDecimal b) { |
101 if (b != null && b.compareTo(a) <= 0) { | 101 if (b != null && b.compareTo(this.a) <= 0) { |
102 throw new IllegalArgumentException( | 102 throw new IllegalArgumentException( |
103 "b (" + b + ") must be greater than a (" + a + ") or null."); | 103 "b (" + b + ") must be greater than a (" + this.a + ") or null."); |
104 } | 104 } |
105 this.b = b; | 105 this.b = b; |
106 } | 106 } |
107 | 107 |
108 public Range getPeer(River river) { | 108 /** |
109 if (peer == null) { | 109 * Difference of a and b |
110 peer = ImporterSession.getInstance().getRange(river, a, b); | 110 * |
111 * @return b - a, or NaN if a or b null | |
112 */ | |
113 public double difference() { | |
114 if ((this.a != null) && (this.b != null)) | |
115 return this.b.subtract(this.a).doubleValue(); | |
116 else | |
117 return Double.NaN; | |
118 } | |
119 | |
120 public Range getPeer(final River river) { | |
121 if (this.peer == null) { | |
122 this.peer = ImporterSession.getInstance().getRange(river, this.a, this.b); | |
111 } | 123 } |
112 return peer; | 124 return this.peer; |
113 } | 125 } |
114 } | 126 } |
115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 127 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |