changeset 204:764835268e2b

Added methods to find out if two ranges intersects. flys-backend/trunk@1579 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Mar 2011 09:06:30 +0000
parents bfee0e05b4e7
children 697d1faa8217
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/model/Range.java
diffstat 2 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Mar 24 16:47:05 2011 +0000
+++ b/flys-backend/ChangeLog	Mon Mar 28 09:06:30 2011 +0000
@@ -1,3 +1,8 @@
+2011-03-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/model/Range.java:
+	  Added methods to find out if two ranges intersects.
+
 2011-03-24	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/model/Gauge.java:
--- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java	Thu Mar 24 16:47:05 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java	Mon Mar 28 09:06:30 2011 +0000
@@ -28,6 +28,10 @@
     public Range() {
     }
 
+    public Range(double a, double b, River river) {
+        this(new BigDecimal(a), new BigDecimal(b), river);
+    }
+
     public Range(BigDecimal a, BigDecimal b, River river) {
         this.a     = a;
         this.b     = b;
@@ -78,5 +82,63 @@
     public void setRiver(River river) {
         this.river = river;
     }
+
+    public int code() {
+        int code = 0;
+        if (a != null) code  = 1;
+        if (b != null) code |= 2;
+        return code;
+    }
+
+    public boolean intersects(BigDecimal c) {
+        return !(a.compareTo(c) > 0 || b.compareTo(c) < 0);
+    }
+
+    public boolean intersects(Range other) {
+
+        int code  = code();
+        int ocode = other.code();
+
+        if (code == 0 || ocode == 0) {
+            return false;
+        }
+
+        switch (code) {
+            case 1: // has a
+                switch (ocode) {
+                    case 1: // has a
+                        return a.compareTo(other.a) == 0;
+                    case 2: // has b
+                        return a.compareTo(other.b) == 0;
+                    case 3: // has range
+                        return other.intersects(a);
+                }
+                break;
+            case 2: // has b
+                switch (ocode) {
+                    case 1: // has a
+                        return b.compareTo(other.a) == 0;
+                    case 2: // has b
+                        return a.compareTo(other.b) == 0;
+                    case 3: // has range
+                        return other.intersects(b);
+                }
+                break;
+            case 3: // has range
+                switch (ocode) {
+                    case 1: // has a
+                        return intersects(other.a);
+                    case 2: // has b
+                        return intersects(other.b);
+                    case 3: // has range
+                        return !(other.b.compareTo(a) < 0
+                               ||other.a.compareTo(b) > 0);
+                }
+                break;
+
+        }
+
+        return false;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org