diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapContext.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapContext.java index 128b2215e6..2443ef9046 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapContext.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapContext.java @@ -18,19 +18,17 @@ *******************************************************************************/ package org.apache.ofbiz.base.util.collections; -import java.util.AbstractSet; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.ofbiz.base.util.UtilGenerics; @@ -43,14 +41,6 @@ public class MapContext implements Map, LocalizedMap { public static final String module = MapContext.class.getName(); - public static final MapContext getMapContext() { - return new MapContext<>(); - } - - protected MapContext() { - super(); - } - protected Deque> stackList = new LinkedList<>(); /** Puts a new Map on the top of the stack */ @@ -96,13 +86,7 @@ public class MapContext implements Map, LocalizedMap { */ @Override public boolean isEmpty() { - // walk the stackList and if any is not empty, return false; otherwise return true - for (Map curMap: this.stackList) { - if (!curMap.isEmpty()) { - return false; - } - } - return true; + return stackList.stream().allMatch(Map::isEmpty); } /* (non-Javadoc) @@ -110,13 +94,7 @@ public class MapContext implements Map, LocalizedMap { */ @Override public boolean containsKey(Object key) { - // walk the stackList and for the first place it is found return true; otherwise refurn false - for (Map curMap: this.stackList) { - if (curMap.containsKey(key)) { - return true; - } - } - return false; + return stackList.stream().anyMatch(m -> m.containsKey(key)); } /* (non-Javadoc) @@ -220,12 +198,10 @@ public class MapContext implements Map, LocalizedMap { */ @Override public Set keySet() { - // walk the stackList and aggregate all keys - Set resultSet = new HashSet<>(); - for (Map curMap: this.stackList) { - resultSet.addAll(curMap.keySet()); - } - return Collections.unmodifiableSet(resultSet); + Set keys = stackList.stream() + .flatMap(m -> m.keySet().stream()) + .collect(Collectors.toSet()); + return Collections.unmodifiableSet(keys); } /* (non-Javadoc) @@ -254,7 +230,7 @@ public class MapContext implements Map, LocalizedMap { public Set> entrySet() { // walk the stackList and the entries for each Map and if nothing is in for the current key, put it in Set resultKeySet = new HashSet<>(); - Set> resultEntrySet = new ListSet<>(); + Set> resultEntrySet = new HashSet<>(); for (Map curMap: this.stackList) { for (Map.Entry curEntry: curMap.entrySet()) { if (!resultKeySet.contains(curEntry.getKey())) { @@ -290,49 +266,4 @@ public class MapContext implements Map, LocalizedMap { } return fullMapString.toString(); } - - private static final class ListSet extends AbstractSet { - - private final List listImpl; - - public ListSet() { - this.listImpl = new ArrayList<>(); - } - - public int size() { - return this.listImpl.size(); - } - - public Iterator iterator() { - return this.listImpl.iterator(); - } - - public boolean add(final E obj) { - boolean added = false; - - if (!this.listImpl.contains(obj)) { - added = this.listImpl.add(obj); - } - - return added; - } - - public boolean isEmpty() { - return this.listImpl.isEmpty(); - } - - public boolean contains(final Object obj) { - return this.listImpl.contains(obj); - } - - public boolean remove(final Object obj) { - return this.listImpl.remove(obj); - } - - public void clear() { - this.listImpl.clear(); - } - - } - } diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java index a14eace3c2..6900c376ec 100644 --- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java +++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ConfigXMLReader.java @@ -214,7 +214,7 @@ public class ConfigXMLReader { } private Map pushIncludes(Function> f) throws WebAppConfigurationException { - MapContext res = MapContext.getMapContext(); + MapContext res = new MapContext<>(); for (URL include : includes) { res.push(getControllerConfig(include).pushIncludes(f)); } @@ -277,7 +277,7 @@ public class ConfigXMLReader { } public Map getRequestMapMap() throws WebAppConfigurationException { - MapContext result = MapContext.getMapContext(); + MapContext result = new MapContext<>(); for (URL includeLocation : includes) { ControllerConfig controllerConfig = getControllerConfig(includeLocation); result.push(controllerConfig.getRequestMapMap());