annotate gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/DistinctValuesNaviChartStepper.java @ 9263:abf14917be32

Moved stepping behaviour of NaviOutputChart into an exchangeable strategy. Allows for distinct values stepping of sinfo flood duration.
author gernotbelger
date Tue, 17 Jul 2018 19:48:18 +0200
parents
children
rev   line source
9263
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
2 * Software engineering by
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
5 *
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
9 */
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
10 package org.dive4elements.river.client.client.ui.chart;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
11
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
12 import java.util.Set;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
13 import java.util.SortedSet;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
14 import java.util.TreeSet;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
15
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
16 /**
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
17 * Allows stepping through a list of distinct (known) values.
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
18 *
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
19 * @author Gernot Belger
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
20 */
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
21 public class DistinctValuesNaviChartStepper implements INaviChartStepper {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
22
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
23 private final TreeSet<Double> validSteps = new TreeSet<Double>();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
24
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
25 private double currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
26
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
27 public DistinctValuesNaviChartStepper(final Set<Double> validKms) {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
28 this.validSteps.addAll(validKms);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
29
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
30 if (this.validSteps.isEmpty())
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
31 this.validSteps.add(-1d);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
32
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
33 this.currentKm = this.validSteps.first();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
34 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
35
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
36 @Override
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
37 public double getCurrentKm() {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
38 return this.currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
39 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
40
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
41 @Override
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
42 public double stepForward() {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
43 this.currentKm = calculateStepFormward();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
44 return this.currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
45 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
46
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
47 private double calculateStepFormward() {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
48 // REMARK: can't use higher due to gwt bug
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
49 // final Double next = this.validSteps.higher(this.currentKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
50
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
51 // REMAREK: add a bit, because tailSet is inclusive
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
52 final SortedSet<Double> tailSet = this.validSteps.tailSet(this.currentKm + 1e-6);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
53 final Double next = tailSet.isEmpty() ? null : tailSet.first();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
54
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
55 if (next != null)
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
56 return next;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
57
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
58 return this.validSteps.last();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
59 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
60
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
61 @Override
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
62 public double stepBackward() {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
63 this.currentKm = calculateStepBackward();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
64 return this.currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
65 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
66
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
67 private double calculateStepBackward() {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
68 // REMARK: can't use lower due to gwt bug
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
69 // final Double prev = this.validSteps.lower(this.currentKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
70
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
71 final SortedSet<Double> headSet = this.validSteps.headSet(this.currentKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
72 final Double prev = headSet.isEmpty() ? null : headSet.last();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
73
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
74 if (prev != null)
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
75 return prev;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
76
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
77 return this.validSteps.first();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
78 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
79
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
80 @Override
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
81 public double setValidCurrentKm(final double currentKm) {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
82 this.currentKm = calculateValidCurrentKm(currentKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
83 return this.currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
84 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
85
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
86 private double calculateValidCurrentKm(final double newKm) {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
87
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
88 if (this.validSteps.contains(newKm)) {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
89 /* special case, and because headSet() is not inclusive */
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
90 return newKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
91 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
92
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
93 final SortedSet<Double> headSet = this.validSteps.headSet(newKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
94 final SortedSet<Double> tailSet = this.validSteps.tailSet(newKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
95
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
96 // REMARK: can't use floor/ceiling because of gwt bug
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
97 // final Double floor = this.validSteps.floor(currentKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
98 // final Double ceiling = this.validSteps.ceiling(currentKm);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
99 final Double floor = headSet.isEmpty() ? null : headSet.last();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
100 final Double ceiling = tailSet.isEmpty() ? null : tailSet.first();
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
101
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
102 if (floor != null && ceiling == null)
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
103 return floor;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
104
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
105 if (floor == null && ceiling != null)
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
106 return ceiling;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
107
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
108 if (floor == null && ceiling == null) {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
109 /* should never happen as validKms is never empty */
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
110 return this.currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
111 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
112
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
113 if (floor == null || ceiling == null) {
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
114 /* will never happen, but makes the NullPointer access checker happy, else we get warnings in the folowing code */
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
115 return this.currentKm;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
116 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
117
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
118 /* both not null; find nearest */
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
119 final double floorDiff = Math.abs(newKm - floor);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
120 final double ceilingDiff = Math.abs(newKm - ceiling);
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
121 if (floorDiff < ceilingDiff)
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
122 return floor;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
123
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
124 return ceiling;
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
125 }
abf14917be32 Moved stepping behaviour of NaviOutputChart into an exchangeable strategy.
gernotbelger
parents:
diff changeset
126 }

http://dive4elements.wald.intevation.org