[PATCH] Datacage: Added dc:min-number(list) and dc:max-number(list) to extract the numerical

Wald Commits scm-commit at wald.intevation.org
Fri Oct 18 18:36:23 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1382114178 -7200
# Node ID 36a194156f15f88f77fdbf12a5b95fe1bc170282
# Parent  420eb5a5fde41e1a42fe7957a633f4ae59770581
Datacage: Added dc:min-number(list) and dc:max-number(list) to extract the numerical
minimum/maximum from a list of numbers.

Example:

  dc:min-number(dc:find-all('\d{4}', '1919 1291 1299 3029'))

returns

  1291

diff -r 420eb5a5fde4 -r 36a194156f15 artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Fri Oct 18 18:15:33 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Fri Oct 18 18:36:18 2013 +0200
@@ -185,6 +185,20 @@
                     : Collections.<String>emptyList();
             }
         });
+
+        addFunction("max-number", 1, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                return maxNumber(args.get(0));
+            }
+        });
+
+        addFunction("min-number", 1, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                return minNumber(args.get(0));
+            }
+        });
     }
 
     /**
@@ -437,5 +451,85 @@
         }
         return result;
     }
+
+    public Number maxNumber(Object list) {
+        if (list instanceof Collection) {
+            Collection collection = (Collection)list;
+            double max = -Double.MAX_VALUE;
+            for (Object x: collection) {
+                Number n;
+                if (x instanceof Number) {
+                    n = (Number)x;
+                }
+                else if (x instanceof String) {
+                    try {
+                        n = Double.valueOf((String)x);
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn("'" + x + "' is not a number.");
+                        continue;
+                    }
+                }
+                else {
+                    log.warn("'" + x + "' is not a number.");
+                    continue;
+                }
+
+                double v = n.doubleValue();
+
+                if (v > max) {
+                    max = v;
+                }
+            }
+
+            return Double.valueOf(max == -Double.MAX_VALUE
+                ? Double.MAX_VALUE
+                : max);
+        }
+
+        return list instanceof Number
+            ? (Number)list
+            : Double.valueOf(Double.MAX_VALUE);
+    }
+
+    public Number minNumber(Object list) {
+        if (list instanceof Collection) {
+            Collection collection = (Collection)list;
+            double min = Double.MAX_VALUE;
+            for (Object x: collection) {
+                Number n;
+                if (x instanceof Number) {
+                    n = (Number)x;
+                }
+                else if (x instanceof String) {
+                    try {
+                        n = Double.valueOf((String)x);
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn("'" + x + "' is not a number.");
+                        continue;
+                    }
+                }
+                else {
+                    log.warn("'" + x + "' is not a number.");
+                    continue;
+                }
+
+                double v = n.doubleValue();
+
+                if (v < min) {
+                    min = v;
+                }
+            }
+
+            return Double.valueOf(min == Double.MAX_VALUE
+                ? -Double.MAX_VALUE
+                : min);
+        }
+
+        return list instanceof Number
+            ? (Number)list
+            : Double.valueOf(-Double.MAX_VALUE);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list