changeset 5567:42692d6868e6

Order Qs before Ds and values ascending
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 05 Apr 2013 09:54:07 +0200
parents adb6c0f14810
children 2a058dff9666
files flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java
diffstat 1 files changed, 36 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java	Fri Apr 05 09:10:10 2013 +0200
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java	Fri Apr 05 09:54:07 2013 +0200
@@ -1,6 +1,8 @@
 package de.intevation.flys.client.server;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
 import java.util.List;
 
 import javax.xml.xpath.XPathConstants;
@@ -84,7 +86,7 @@
             logger.debug("Extract wq info objects now.");
             WQInfoObject[] objects = extractWQInfoObjects(result);
 
-            if (objects != null && objects.length > 0) {
+            if (objects.length > 0) {
                 return objects;
             }
         }
@@ -118,8 +120,12 @@
             throw new ServerException(ERROR_NO_WQINFO_FOUND);
         }
 
+        boolean debug = logger.isDebugEnabled();
+
         int num = list.getLength();
-        logger.debug("Response contains " + num + " objects.");
+        if (debug) {
+            logger.debug("Response contains " + num + " objects.");
+        }
 
         List<WQInfoObject> objects =
             new ArrayList<WQInfoObject>(num);
@@ -132,12 +138,35 @@
             }
         }
 
-        logger.debug("Retrieved " + objects.size() + " wq values");
+        if (debug) {
+            logger.debug("Retrieved " + objects.size() + " wq values");
+        }
 
-        return (WQInfoObject[])
-            objects.toArray(new WQInfoObject[num]);
+        WQInfoObject [] array = (WQInfoObject[])
+            objects.toArray(new WQInfoObject[objects.size()]);
+
+        Arrays.sort(array, WQ_INFO_OBJECT_CMP);
+
+        return array;
     }
 
+    public static final Comparator<WQInfoObject> WQ_INFO_OBJECT_CMP =
+        new Comparator<WQInfoObject>() {
+            @Override
+            public int compare(WQInfoObject a, WQInfoObject b) {
+
+                // Descending by type: Qs before Ds
+                int cmp = a.getType().compareTo(b.getType());
+                if (cmp < 0) return +1;
+                if (cmp > 0) return -1;
+
+                // Ascending by value
+                double diff = a.getValue() - b.getValue();
+                if (diff < 0d) return -1;
+                if (diff > 0d) return +1;
+                return 0;
+            }
+        };
 
     /**
      * Extracts information for a single wq info object and intializes an
@@ -148,6 +177,8 @@
      * @return a valid WQInfoObject.
      */
     protected WQInfoObject buildWQInfoObject(Node node) {
+
+        // TODO: Replace this expensive XPaths with simpler use of DOM.
         String name = XMLUtils.xpathString(
             node, "@name", ArtifactNamespaceContext.INSTANCE);
 

http://dive4elements.wald.intevation.org