diff 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
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportRange.java	Tue Apr 03 10:40:57 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportRange.java	Tue Apr 03 10:43:53 2018 +0200
@@ -8,12 +8,11 @@
 
 package org.dive4elements.river.importer;
 
-import org.dive4elements.river.model.Range;
-import org.dive4elements.river.model.River;
-
 import java.math.BigDecimal;
 
 import org.apache.log4j.Logger;
+import org.dive4elements.river.model.Range;
+import org.dive4elements.river.model.River;
 
 /** A range that is about to be imported. */
 public class ImportRange
@@ -30,7 +29,7 @@
     public ImportRange() {
     }
 
-    public ImportRange(BigDecimal a) {
+    public ImportRange(final BigDecimal a) {
         this.a = a;
         this.b = null;
     }
@@ -55,14 +54,14 @@
         }
         else {
             if (a.compareTo(b) > 0) {
-                BigDecimal t = a; a = b; b = t;
+                final BigDecimal t = a; a = b; b = t;
             }
             this.a = a;
             this.b = b;
         }
     }
 
-    private static final int compare(BigDecimal a, BigDecimal b) {
+    private static final int compare(final BigDecimal a, final BigDecimal b) {
         if (a == null && b == null) {
             return 0;
         }
@@ -75,41 +74,54 @@
         return a.compareTo(b);
     }
 
-    public int compareTo(ImportRange other) {
-        int cmp = compare(a, other.a);
+    @Override
+    public int compareTo(final ImportRange other) {
+        final int cmp = compare(this.a, other.a);
         if (cmp != 0) return cmp;
-        return compare(b, other.b);
+        return compare(this.b, other.b);
     }
 
     public BigDecimal getA() {
-        return a;
+        return this.a;
     }
 
-    public void setA(BigDecimal a) {
-        if (this.b != null && a.compareTo(b) >= 0) {
+    public void setA(final BigDecimal a) {
+        if (this.b != null && a.compareTo(this.b) >= 0) {
             throw new IllegalArgumentException(
-                "a (" + a + ") must be smaller than b (" + b + ").");
+                    "a (" + a + ") must be smaller than b (" + this.b + ").");
         }
         this.a = a;
     }
 
     public BigDecimal getB() {
-        return b;
+        return this.b;
     }
 
-    public void setB(BigDecimal b) {
-        if (b != null && b.compareTo(a) <= 0) {
+    public void setB(final BigDecimal b) {
+        if (b != null && b.compareTo(this.a) <= 0) {
             throw new IllegalArgumentException(
-                "b (" + b + ") must be greater than a (" + a + ") or null.");
+                    "b (" + b + ") must be greater than a (" + this.a + ") or null.");
         }
         this.b = b;
     }
 
-    public Range getPeer(River river) {
-        if (peer == null) {
-            peer = ImporterSession.getInstance().getRange(river, a, b);
+    /**
+     * Difference of a and b
+     *
+     * @return b - a, or NaN if a or b null
+     */
+    public double difference() {
+        if ((this.a != null) && (this.b != null))
+            return this.b.subtract(this.a).doubleValue();
+        else
+            return Double.NaN;
+    }
+
+    public Range getPeer(final River river) {
+        if (this.peer == null) {
+            this.peer = ImporterSession.getInstance().getRange(river, this.a, this.b);
         }
-        return peer;
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org