[PATCH] Added dc:coalesce(a, b). Returns first element which is not empty or not zero

Wald Commits scm-commit at wald.intevation.org
Thu Jun 26 12:54:35 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1403780071 -7200
# Node ID 09c9920e6f24d49b1cf3d94d12aec920934beb97
# Parent  6d5cbd69511acb3deb90c15d301e155a279475e7
Added dc:coalesce(a, b). Returns first element which is not empty or not zero.

diff -r 6d5cbd69511a -r 09c9920e6f24 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	Thu Jun 26 12:44:40 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Thu Jun 26 12:54:31 2014 +0200
@@ -86,6 +86,13 @@
 
         functions = new HashMap<String, Entry>();
 
+        addFunction("coalesce", 2, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                return coalesce(args);
+            }
+        });
+
         addFunction("lowercase", 1, new XPathFunction() {
             @Override
             public Object evaluate(List args) throws XPathFunctionException {
@@ -546,5 +553,17 @@
             ? (Number)list
             : Double.valueOf(-Double.MAX_VALUE);
     }
+
+    public static Object coalesce(List list) {
+        for (Object x: list) {
+            if (x instanceof String && ((String)x).length() != 0) {
+                return x;
+            }
+            if (x instanceof Number && ((Number)x).doubleValue() != 0.0) {
+                return x;
+            }
+        }
+        return StackFrames.NULL;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4Elements-commits mailing list