This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new b2eb2c2a81 Extend EL's + operator to Maps, Sets and Lists
b2eb2c2a81 is described below

commit b2eb2c2a81402dd16f3aa6a4194937b64c8cb88d
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Aug 20 16:46:40 2024 +0100

    Extend EL's + operator to Maps, Sets and Lists
---
 java/org/apache/el/parser/AstPlus.java     |  25 ++++++
 test/org/apache/el/parser/TestAstPlus.java | 124 +++++++++++++++++++++++++++++
 webapps/docs/changelog.xml                 |   5 ++
 3 files changed, 154 insertions(+)

diff --git a/java/org/apache/el/parser/AstPlus.java 
b/java/org/apache/el/parser/AstPlus.java
index 7c5a60a181..fb59c06b4c 100644
--- a/java/org/apache/el/parser/AstPlus.java
+++ b/java/org/apache/el/parser/AstPlus.java
@@ -17,6 +17,11 @@
 /* Generated By:JJTree: Do not edit this line. AstPlus.java */
 package org.apache.el.parser;
 
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import jakarta.el.ELException;
 
 import org.apache.el.lang.ELArithmetic;
@@ -27,14 +32,34 @@ import org.apache.el.lang.EvaluationContext;
  * @author Jacob Hookom [ja...@hookom.net]
  */
 public final class AstPlus extends ArithmeticNode {
+
     public AstPlus(int id) {
         super(id);
     }
 
+
+    @SuppressWarnings({ "unchecked", "rawtypes" })
     @Override
     public Object getValue(EvaluationContext ctx) throws ELException {
         Object obj0 = this.children[0].getValue(ctx);
         Object obj1 = this.children[1].getValue(ctx);
+
+        if (obj0 != null) {
+            if (obj0 instanceof Map && obj1 instanceof Map) {
+                // Maps are merged
+                ((Map) obj0).putAll((Map) obj1);
+                return obj0;
+            } else if (obj0 instanceof Set && obj1 instanceof Collection) {
+                // If obj0 is a Set then merge
+                ((Set<?>) obj0).addAll((Collection) obj1);
+                return obj0;
+            } else if (obj0 instanceof List && obj1 instanceof Collection) {
+                // If obj0 is a List then concatenate
+                ((List) obj0).addAll((Collection) obj1);
+                return obj0;
+            }
+        }
+
         return ELArithmetic.add(obj0, obj1);
     }
 }
diff --git a/test/org/apache/el/parser/TestAstPlus.java 
b/test/org/apache/el/parser/TestAstPlus.java
new file mode 100644
index 0000000000..b31c572611
--- /dev/null
+++ b/test/org/apache/el/parser/TestAstPlus.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.el.parser;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import jakarta.el.ELProcessor;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAstPlus {
+
+    private static final List<String> simpleList = new ArrayList<>();
+    private static final Map<String,String> simpleMap = new HashMap<>();
+    private static final Set<String> simpleSet = new HashSet<>();
+
+    static {
+        simpleList.add("a");
+        simpleList.add("b");
+        simpleList.add("c");
+        simpleList.add("b");
+        simpleList.add("c");
+
+        simpleMap.put("a", "1");
+        simpleMap.put("b", "2");
+        simpleMap.put("c", "3");
+
+        simpleSet.add("a");
+        simpleSet.add("b");
+        simpleSet.add("c");
+    }
+
+
+    @Test
+    public void testNullAddNull() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("null + null", Integer.class);
+        Assert.assertEquals(Integer.valueOf(0), result);
+    }
+
+
+    @Test
+    public void testMapAddMapNoConflicts() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("{'a':'1','b':'2'} + {'c':'3'}", 
Map.class);
+        Assert.assertEquals(simpleMap, result);
+    }
+
+
+    @Test
+    public void testMapAddMapConflicts() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("{'a':'1','b':'3'} + 
{'b':'2','c':'3'}", Map.class);
+        Assert.assertEquals(simpleMap, result);
+    }
+
+
+    @Test
+    public void testSetAddSetNoConflicts() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("{'a','b'} + {'c'}", Set.class);
+        Assert.assertEquals(simpleSet, result);
+    }
+
+
+    @Test
+    public void testSetAddSetConflicts() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("{'a','b'} + {'b','c'}", Set.class);
+        Assert.assertEquals(simpleSet, result);
+    }
+
+
+    @Test
+    public void testSetAddListNoConflicts() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("{'a','b'} + ['c']", Set.class);
+        Assert.assertEquals(simpleSet, result);
+    }
+
+
+    @Test
+    public void testSetAddListConflicts() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("{'a','b'} + ['b','c','c']", 
Set.class);
+        Assert.assertEquals(simpleSet, result);
+    }
+
+
+    @Test
+    public void testListAddList() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("['a','b','c'] + ['b','c']", 
List.class);
+        Assert.assertEquals(simpleList, result);
+    }
+
+
+    @Test
+    public void testListAddSet() {
+        ELProcessor processor = new ELProcessor();
+        Object result = processor.getValue("['a','b','c'] + {'b','c'}", 
List.class);
+        Assert.assertEquals(simpleList, result);
+    }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 6128f5a718..fa227e7f13 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -136,6 +136,11 @@
         Add support for the new Elvis operator (<code>:?</code>) in Jakarta
         Expression language. (markt)
       </add>
+      <add>
+        Extend the existing <code>+</code> operator in Jakarta Expression
+        Language to support mergin <code>Map</code>Map and <code>Set</code>Set
+        and concatenating <code>List</code>s. (markt)
+      </add>
     </changelog>
   </subsection>
 </section>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to