From 2c4907a873b71c92f87b066b95f76f0c32e6cf7e Mon Sep 17 00:00:00 2001 From: "Taher A. Alkhateeb" Date: Thu, 26 Jul 2018 10:02:06 +0000 Subject: [PATCH] Improved: refactor the MapContext to use a Deque instead of List interface (OFBIZ-10485) The MapContext and MapStack classes are abstracting away the underlying LinkedList implementation behind a List. The problem is that the push and pop methods are implemented awkwardly by using index zero or using size() to find the ends of the collection. Thus this improvement maintains the same code signature but replaces the List interface with the Deque to utilize the getFirst(), addFirst() and addLast() functions making the code cleaner and more declaractive. Thanks: Mathieu Lirzin for the patch and explanation of your work. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1836710 13f79535-47bb-0310-9956-ffa450edef68 --- .../base/util/collections/MapContext.java | 26 +++++++------------ .../ofbiz/base/util/collections/MapStack.java | 2 +- 2 files changed, 11 insertions(+), 17 deletions(-) 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 581a7c8b8c..128b2215e6 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 @@ -22,6 +22,7 @@ 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; @@ -50,12 +51,11 @@ public class MapContext implements Map, LocalizedMap { super(); } - protected List> stackList = new LinkedList<>(); + protected Deque> stackList = new LinkedList<>(); /** Puts a new Map on the top of the stack */ public void push() { - Map newMap = new HashMap<>(); - this.stackList.add(0,newMap); + stackList.addFirst(new HashMap()); } /** Puts an existing Map on the top of the stack (top meaning will override lower layers on the stack) */ @@ -63,7 +63,7 @@ public class MapContext implements Map, LocalizedMap { if (existingMap == null) { throw new IllegalArgumentException("Error: cannot push null existing Map onto a MapContext"); } - this.stackList.add(0, existingMap); + stackList.addFirst(existingMap); } /** Puts an existing Map on the BOTTOM of the stack (bottom meaning will be overriden by lower layers on the stack, ie everything else already there) */ @@ -71,16 +71,13 @@ public class MapContext implements Map, LocalizedMap { if (existingMap == null) { throw new IllegalArgumentException("Error: cannot add null existing Map to bottom of a MapContext"); } - this.stackList.add(existingMap); + stackList.addLast(existingMap); } /** Remove and returns the Map from the top of the stack; if there is only one Map on the stack it returns null and does not remove it */ public Map pop() { // always leave at least one Map in the List, ie never pop off the last Map - if (this.stackList.size() > 1) { - return stackList.remove(0); - } - return null; + return stackList.size() > 1 ? stackList.removeFirst() : null; } /* (non-Javadoc) @@ -188,8 +185,7 @@ public class MapContext implements Map, LocalizedMap { @Override public V put(K key, V value) { // all write operations are local: only put in the Map on the top of the stack - Map currentMap = this.stackList.get(0); - return currentMap.put(key, value); + return stackList.getFirst().put(key, value); } /* (non-Javadoc) @@ -198,8 +194,7 @@ public class MapContext implements Map, LocalizedMap { @Override public V remove(Object key) { // all write operations are local: only remove from the Map on the top of the stack - Map currentMap = this.stackList.get(0); - return currentMap.remove(key); + return stackList.getFirst().remove(key); } /* (non-Javadoc) @@ -208,8 +203,7 @@ public class MapContext implements Map, LocalizedMap { @Override public void putAll(Map arg0) { // all write operations are local: only put in the Map on the top of the stack - Map currentMap = this.stackList.get(0); - currentMap.putAll(arg0); + stackList.getFirst().putAll(arg0); } /* (non-Javadoc) @@ -218,7 +212,7 @@ public class MapContext implements Map, LocalizedMap { @Override public void clear() { // all write operations are local: only clear the Map on the top of the stack - this.stackList.get(0).clear(); + stackList.getFirst().clear(); } /* (non-Javadoc) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java index 74f1d0091e..58ade5a43f 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java @@ -43,7 +43,7 @@ public class MapStack extends MapContext { if (baseMap instanceof MapStack) { newValue.stackList.addAll(((MapStack) baseMap).stackList); } else { - newValue.stackList.add(0, baseMap); + newValue.stackList.addFirst(baseMap); } return newValue; }