changeset 8036:17542d100e75

Throw out old visitor model and use grain fraction filters instead. Fixed neighborhood wirinng of measument stations.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 16 Jul 2014 11:05:59 +0200
parents f2dc7992b8a3
children 1de6256c9786
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java
diffstat 1 files changed, 47 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Tue Jul 15 21:31:11 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Wed Jul 16 11:05:59 2014 +0200
@@ -10,6 +10,7 @@
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.TreeMap;
@@ -38,8 +39,16 @@
         return -1;
     }
 
+    public interface Visitor {
+        void visit(List<Station> stations);
+    }
+
     public static class Value implements Serializable {
 
+        public interface Filter {
+            boolean accept(Value value);
+        }
+
         private double value;
 
         private Load load;
@@ -107,12 +116,6 @@
         public static final int BED_LOAD  = 0;
         public static final int SUSPENDED = 1;
 
-        public interface Visitor {
-            boolean accept(Station station);
-            boolean accept(int grainFraction);
-            boolean visit(Value value, int grainFraction);
-        }
-
         private double station;
 
         private int type;
@@ -129,7 +132,7 @@
         public Station(int type, double station) {
             grainFractions = new ArrayList<List<Value>>(GF_MAX+1);
             for (int i = 0; i < GF_MAX+1; ++i) {
-                grainFractions.add(new ArrayList<Value>());
+                grainFractions.add(null);
             }
             this.type = type;
             this.station = station;
@@ -159,8 +162,21 @@
             return prev;
         }
 
+        public Station getNext(boolean isKMUp) {
+            return isKMUp ? getNext() : getPrev();
+        }
+
+        public Station getPrev(boolean isKMUp) {
+            return isKMUp ? getPrev() : getNext();
+        }
+
         public void addValue(int grainFraction, Value value) {
-            grainFractions.get(grainFraction).add(value);
+            List<Value> values = grainFractions.get(grainFraction);
+            if (values == null) {
+                values = new ArrayList<Value>();
+                grainFractions.set(grainFraction, values);
+            }
+            values.add(value);
         }
 
         public boolean hasGrainFraction(String grainFraction) {
@@ -169,29 +185,21 @@
 
         public boolean hasGrainFraction(int grainFraction) {
             List<Value> values = grainFractions.get(grainFraction);
-            return !values.isEmpty();
+            return values != null && !values.isEmpty();
         }
 
-        public void visit(Visitor visitor) {
-            if (!visitor.accept(this)) {
-                return;
+        public List<Value> filterGrainFraction(int grainFraction, Value.Filter filter) {
+            List<Value> values = grainFractions.get(grainFraction);
+            if (values == null || values.isEmpty()) {
+                return Collections.<Value>emptyList();
             }
-
-            for (int i = 0; i < GF_MAX+1; ++i) {
-                List<Value> values = grainFractions.get(i);
-                if (values.isEmpty() || !visitor.accept(i)) {
-                    continue;
-                }
-                for (Value value: values) {
-                    if (!visitor.visit(value, i)) {
-                        break;
-                    }
+            List<Value> result = new ArrayList<Value>(values.size());
+            for (Value value: values) {
+                if (filter.accept(value)) {
+                    result.add(value);
                 }
             }
-        }
-
-        public boolean inside(double a, double b) {
-            return station >= a && station <= b;
+            return result;
         }
 
         public double findValueByLoadId(int id) {
@@ -208,6 +216,9 @@
             List<Value> values,
             int         id
         ) {
+            if (values == null) {
+                return Double.NaN;
+            }
             // List is ordered by station id -> binary search.
             int lo = 0, hi = values.size()-1;
             while (lo <= hi) {
@@ -221,6 +232,15 @@
 
             return Double.NaN;
         }
+
+        public static Station firstOfType(List<Station> stations, int type) {
+            for (Station station: stations) {
+                if (station.getType() == type) {
+                    return station;
+                }
+            }
+            return null;
+        }
     } // class Station
 
 
@@ -250,7 +270,7 @@
     }
 
     private void wireNeighbors() {
-        for (int i = 0, N = stations.size()-1; i < N; ++i) {
+        for (int i = 0, N = stations.size(); i < N-1; ++i) {
             for (Station current: stations.get(i)) {
                 int type = current.getType();
                 NEXT: for (int j = i+1; j < N; ++j) {
@@ -266,10 +286,6 @@
         }
     }
 
-    public interface Visitor {
-        void visit(List<Station> stations);
-    }
-
     private void recursiveFindStations(
         double a, double b,
         int lo, int hi,

http://dive4elements.wald.intevation.org