[PATCH] Give ordered list of measurement stations at river

Wald Commits scm-commit at wald.intevation.org
Thu Oct 16 16:46:13 CEST 2014


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1413470753 -7200
# Node ID 536a1c0c8a81192ddbf180e406c5a8ed364a62ae
# Parent  71144e25a6c99b0929b447266da32ca1f47164c4
Give ordered list of measurement stations at river.

diff -r 71144e25a6c9 -r 536a1c0c8a81 backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java
--- a/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Thu Oct 16 13:39:22 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/MeasurementStation.java	Thu Oct 16 16:45:53 2014 +0200
@@ -11,6 +11,8 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 
 import java.math.BigDecimal;
 
@@ -29,6 +31,8 @@
 
 import org.dive4elements.river.backend.SessionHolder;
 
+import static org.dive4elements.river.backend.utils.EpsilonComparator.CMP;
+
 @Entity
 @Table(name = "measurement_station")
 public class MeasurementStation {
@@ -48,6 +52,42 @@
 
     private TimeInterval observationTimerange;
 
+    public static final class MeasurementStationComparator
+        implements Comparator<MeasurementStation> {
+
+        public MeasurementStationComparator() {
+        }
+
+        @Override
+        public int compare(MeasurementStation m1, MeasurementStation m2)
+            throws IllegalArgumentException {
+
+            if (m1.getRange().getRiver() != m2.getRange().getRiver()) {
+                throw new IllegalArgumentException(
+                    "Stations not at same river");
+            }
+
+            boolean kmUp = m1.getRange().getRiver().getKmUp();
+            double s1 = kmUp && m1.getRange().getB() != null
+                ? m1.getRange().getB().doubleValue()
+                : m1.getRange().getA().doubleValue();
+            double s2 = kmUp && m2.getRange().getB() != null
+                ? m2.getRange().getB().doubleValue()
+                : m2.getRange().getA().doubleValue();
+
+            int cmpStations = CMP.compare(s1, s2);
+            if (cmpStations == 0) {
+                if (m1.getMeasurementType().equals(m2.getMeasurementType())) {
+                    throw new IllegalArgumentException(
+                        "Two stations of same type at same km");
+                }
+
+                return m1.getMeasurementType().equals("Geschiebe") ? -1 : +1;
+            }
+            return cmpStations;
+        }
+    }
+
     public MeasurementStation() {
     }
 
@@ -169,10 +209,11 @@
         Query query = session.createQuery(
             "from MeasurementStation " +
             "where range.river = :river");
-
         query.setParameter("river", river);
 
-        return query.list();
+        List<MeasurementStation> result = query.list();
+        Collections.sort(result, new MeasurementStationComparator());
+        return result;
     }
 
     public static List<MeasurementStation> getStationsAtKM(


More information about the Dive4Elements-commits mailing list