Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataValueFilter.java @ 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 | |
children | 72760ca2fc2b |
comparison
equal
deleted
inserted
replaced
8036:17542d100e75 | 8037:1de6256c9786 |
---|---|
1 /* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 package org.dive4elements.river.artifacts.model.minfo; | |
9 | |
10 import java.util.ArrayList; | |
11 import java.util.Calendar; | |
12 import java.util.Date; | |
13 import java.util.List; | |
14 | |
15 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Value; | |
16 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Value.Filter; | |
17 | |
18 public final class SedimentLoadDataValueFilter { | |
19 | |
20 private SedimentLoadDataValueFilter() { | |
21 } | |
22 | |
23 public static abstract class Composite implements Filter { | |
24 protected List<Filter> filters; | |
25 | |
26 public Composite() { | |
27 filters = new ArrayList<Filter>(); | |
28 } | |
29 | |
30 public void add(Filter filter) { | |
31 filters.add(filter); | |
32 } | |
33 } | |
34 | |
35 public static final class And extends Composite { | |
36 | |
37 public And() { | |
38 } | |
39 | |
40 @Override | |
41 public boolean accept(Value value) { | |
42 for (Filter filter: filters) { | |
43 if (!filter.accept(value)) { | |
44 return false; | |
45 } | |
46 } | |
47 return true; | |
48 } | |
49 } // class And | |
50 | |
51 public static final class Or extends Composite { | |
52 | |
53 public Or() { | |
54 } | |
55 | |
56 @Override | |
57 public boolean accept(Value value) { | |
58 for (Filter filter: filters) { | |
59 if (filter.accept(value)) { | |
60 return true; | |
61 } | |
62 } | |
63 return false; | |
64 } | |
65 } // class Or | |
66 | |
67 public static final class Year implements Filter { | |
68 | |
69 private int year; | |
70 | |
71 public Year(int year) { | |
72 this.year = year; | |
73 } | |
74 | |
75 @Override | |
76 public boolean accept(Value value) { | |
77 Calendar cal = Calendar.getInstance(); | |
78 cal.setTime(value.getLoad().getStartTime()); | |
79 return cal.get(Calendar.YEAR) == year; | |
80 } | |
81 } // class Year | |
82 | |
83 public static final class IsEpoch implements Filter { | |
84 | |
85 public static final IsEpoch INSTANCE = new IsEpoch(); | |
86 | |
87 private IsEpoch() { | |
88 } | |
89 | |
90 @Override | |
91 public boolean accept(Value value) { | |
92 return value.getLoad().isEpoch(); | |
93 } | |
94 } // class Year | |
95 | |
96 public static final class TimeRangeIntersects implements Filter { | |
97 | |
98 private Date a; | |
99 private Date b; | |
100 | |
101 public TimeRangeIntersects(Date a, Date b) { | |
102 if (a.after(b)) { | |
103 this.b = a; | |
104 this.a = b; | |
105 } else { | |
106 this.a = a; | |
107 this.b = b; | |
108 } | |
109 } | |
110 @Override | |
111 public boolean accept(Value value) { | |
112 Date c = value.getLoad().getStartTime(); | |
113 Date d = value.getLoad().getStopTime(); | |
114 return d == null | |
115 ? c.compareTo(a) >= 0 && c.compareTo(b) <= 0 | |
116 : !(a.after(d) || c.after(b)); | |
117 } | |
118 } // class TimeRangeIntersects | |
119 } | |
120 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | |
121 |