changeset 7383:36a194156f15

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
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 18 Oct 2013 18:36:18 +0200
parents 420eb5a5fde4
children ef310e272fb5
files artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java
diffstat 1 files changed, 94 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org