You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2022/08/22 16:50:11 UTC

[commons-jexl] branch master updated: JEXL-380: use the more elegant map.merge() to handle promotion of pragma values;

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

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new a6199de4 JEXL-380: use the more elegant map.merge() to handle promotion of pragma values;
a6199de4 is described below

commit a6199de400ed3b6225cb337dd5419af5861f0f33
Author: henrib <he...@apache.org>
AuthorDate: Mon Aug 22 18:50:06 2022 +0200

    JEXL-380: use the more elegant map.merge() to handle promotion of pragma values;
---
 .../org/apache/commons/jexl3/parser/JexlParser.java  | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
index f46f87e0..bae8e971 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
@@ -518,22 +518,18 @@ public abstract class JexlParser extends StringParser {
                 namespaces.add(nsname);
             }
         }
-        Object previous = pragmas.put(key, value);
-        if (previous != null) {
-            Set<Object> values;
+        // merge new value into a set created on the fly if key is already mapped
+        pragmas.merge(key, value, (previous, newValue)->{
             if (previous instanceof Set<?>) {
-                // reinsert previous value which was the set
-                values = (Set<Object>) previous;
-                pragmas.put(key, values);
+                ((Set<Object>) previous).add(newValue);
+                return previous;
             } else {
-                // create a new set as value
-                values = new LinkedHashSet<Object>();
-                pragmas.put(key, values);
+                Set<Object> values = new LinkedHashSet<>();
                 values.add(previous);
+                values.add(newValue);
+                return values;
             }
-            // add the new value to the set of values
-            values.add(value);
-        }
+        });
     }
 
     /**