changeset 8037:1de6256c9786

Added filters for loaded sediment data values. Year, Epochs (time ranges), And and Or.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 16 Jul 2014 11:55:46 +0200
parents 17542d100e75
children aa622bddfdac
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataValueFilter.java
diffstat 1 files changed, 121 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataValueFilter.java	Wed Jul 16 11:55:46 2014 +0200
@@ -0,0 +1,121 @@
+/* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+package org.dive4elements.river.artifacts.model.minfo;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Value;
+import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Value.Filter;
+
+public final class SedimentLoadDataValueFilter {
+
+    private SedimentLoadDataValueFilter() {
+    }
+
+    public static abstract class Composite implements Filter {
+        protected List<Filter> filters;
+
+        public Composite() {
+            filters = new ArrayList<Filter>();
+        }
+
+        public void add(Filter filter) {
+            filters.add(filter);
+        }
+    }
+
+    public static final class And extends Composite {
+
+        public And() {
+        }
+
+        @Override
+        public boolean accept(Value value) {
+            for (Filter filter: filters) {
+                if (!filter.accept(value)) {
+                    return false;
+                }
+            }
+            return true;
+        }
+    } // class And
+
+    public static final class Or extends Composite {
+
+        public Or() {
+        }
+
+        @Override
+        public boolean accept(Value value) {
+            for (Filter filter: filters) {
+                if (filter.accept(value)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+    } // class Or
+
+    public static final class Year implements Filter {
+
+        private int year;
+
+        public Year(int year) {
+            this.year = year;
+        }
+
+        @Override
+        public boolean accept(Value value) {
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(value.getLoad().getStartTime());
+            return cal.get(Calendar.YEAR) == year;
+        }
+    } // class Year
+
+    public static final class IsEpoch implements Filter {
+
+        public static final IsEpoch INSTANCE = new IsEpoch();
+
+        private IsEpoch() {
+        }
+
+        @Override
+        public boolean accept(Value value) {
+            return value.getLoad().isEpoch();
+        }
+    } // class Year
+
+    public static final class TimeRangeIntersects implements Filter {
+
+        private Date a;
+        private Date b;
+
+        public TimeRangeIntersects(Date a, Date b) {
+            if (a.after(b)) {
+                this.b = a;
+                this.a = b;
+            } else {
+                this.a = a;
+                this.b = b;
+            }
+        }
+        @Override
+        public boolean accept(Value value) {
+            Date c = value.getLoad().getStartTime();
+            Date d = value.getLoad().getStopTime();
+            return d == null
+                ? c.compareTo(a) >= 0 && c.compareTo(b) <= 0
+                : !(a.after(d) || c.after(b));
+        }
+    } // class TimeRangeIntersects
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+

http://dive4elements.wald.intevation.org