[Dive4elements-commits] [PATCH] Order Qs before Ds and values ascending

Wald Commits scm-commit at wald.intevation.org
Fri Apr 5 09:54:35 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1365148447 -7200
# Node ID 42692d6868e685452ecfe120be63e3c01a5ed002
# Parent  adb6c0f14810b7049ef4240860703b8695358721
Order Qs before Ds and values ascending

diff -r adb6c0f14810 -r 42692d6868e6 flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java
--- 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);
 


More information about the Dive4elements-commits mailing list