diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/FlexibleServletAccessor.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/FlexibleServletAccessor.java index 51d038d648..616d5fb764 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/FlexibleServletAccessor.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/FlexibleServletAccessor.java @@ -66,7 +66,7 @@ public class FlexibleServletAccessor implements Serializable { } else { empty = false; int openPos = name.indexOf("${"); - if (openPos != -1 && name.indexOf("}", openPos) != -1) { + if (openPos != -1 && name.indexOf('}', openPos) != -1) { fma = null; attributeName = null; needsExpand = true; @@ -95,7 +95,7 @@ public class FlexibleServletAccessor implements Serializable { * @return the object corresponding to this getter class */ public T get(ServletRequest request, Map expandContext) { - AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); + AttributeAccessor aa = new AttributeAccessor<>(name, expandContext, this.attributeName, this.fma, this.needsExpand); return aa.get(request); } @@ -105,7 +105,7 @@ public class FlexibleServletAccessor implements Serializable { * @return the found value */ public T get(HttpSession session, Map expandContext) { - AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); + AttributeAccessor aa = new AttributeAccessor<>(name, expandContext, this.attributeName, this.fma, this.needsExpand); return aa.get(session); } @@ -119,7 +119,7 @@ public class FlexibleServletAccessor implements Serializable { * @param expandContext */ public void put(ServletRequest request, T value, Map expandContext) { - AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); + AttributeAccessor aa = new AttributeAccessor<>(name, expandContext, this.attributeName, this.fma, this.needsExpand); aa.put(request, value); } @@ -133,7 +133,7 @@ public class FlexibleServletAccessor implements Serializable { * @param expandContext */ public void put(HttpSession session, T value, Map expandContext) { - AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); + AttributeAccessor aa = new AttributeAccessor<>(name, expandContext, this.attributeName, this.fma, this.needsExpand); aa.put(session, value); } @@ -143,7 +143,7 @@ public class FlexibleServletAccessor implements Serializable { * @return the removed value */ public T remove(ServletRequest request, Map expandContext) { - AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); + AttributeAccessor aa = new AttributeAccessor<>(name, expandContext, this.attributeName, this.fma, this.needsExpand); return aa.remove(request); } @@ -153,11 +153,11 @@ public class FlexibleServletAccessor implements Serializable { * @return the removed value */ public T remove(HttpSession session, Map expandContext) { - AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand); + AttributeAccessor aa = new AttributeAccessor<>(name, expandContext, this.attributeName, this.fma, this.needsExpand); return aa.remove(session); } - /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key * + /** The equals and hashCode methods are implemented just case this object is ever accidently used as a Map key * * @return the hashcode */ @Override @@ -165,7 +165,7 @@ public class FlexibleServletAccessor implements Serializable { return this.name.hashCode(); } - /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key + /** The equals and hashCode methods are implemented just case this object is ever accidently used as a Map key * @param obj * @return whether this object is equal to the passed object */ @@ -177,13 +177,14 @@ public class FlexibleServletAccessor implements Serializable { return flexibleServletAccessor.name == null; } return this.name.equals(flexibleServletAccessor.name); - } else { - String str = (String) obj; - if (this.name == null) { - return str == null; - } - return this.name.equals(str); } + if (this.name == null) { + return obj == null; + } + if (!(obj instanceof String)) { + return false; + } + return this.name.equals(obj); } /** To be used for a string representation of the accessor, returns the original name. @@ -258,9 +259,8 @@ public class FlexibleServletAccessor implements Serializable { if (fma != null) { return fma.get(UtilGenerics.checkMap(theValue)); - } else { - return UtilGenerics.cast(theValue); } + return UtilGenerics.cast(theValue); } public T get(HttpSession session) { @@ -274,9 +274,8 @@ public class FlexibleServletAccessor implements Serializable { if (fma != null) { return fma.get(UtilGenerics.checkMap(theValue)); - } else { - return UtilGenerics.cast(theValue); } + return UtilGenerics.cast(theValue); } protected void putInList(List lst, T value) { @@ -336,19 +335,16 @@ public class FlexibleServletAccessor implements Serializable { if (isListReference) { List lst = UtilGenerics.checkList(theObj); return fma.remove(UtilGenerics.checkMap(lst.get(listIndex), String.class, Object.class)); - } else { - return fma.remove(UtilGenerics.checkMap(theObj, String.class, Object.class)); - } - } else { - if (isListReference) { - List lst = UtilGenerics.checkList(request.getAttribute(attributeName)); - return UtilGenerics.cast(lst.remove(listIndex)); - } else { - Object theValue = request.getAttribute(attributeName); - request.removeAttribute(attributeName); - return UtilGenerics.cast(theValue); } + return fma.remove(UtilGenerics.checkMap(theObj, String.class, Object.class)); } + if (isListReference) { + List lst = UtilGenerics.checkList(request.getAttribute(attributeName)); + return UtilGenerics.cast(lst.remove(listIndex)); + } + Object theValue = request.getAttribute(attributeName); + request.removeAttribute(attributeName); + return UtilGenerics.cast(theValue); } public T remove(HttpSession session) { @@ -357,19 +353,16 @@ public class FlexibleServletAccessor implements Serializable { if (isListReference) { List lst = UtilGenerics.checkList(theObj); return fma.remove(UtilGenerics.checkMap(lst.get(listIndex), String.class, Object.class)); - } else { - return fma.remove(UtilGenerics.checkMap(theObj, String.class, Object.class)); - } - } else { - if (isListReference) { - List lst = UtilGenerics.checkList(session.getAttribute(attributeName)); - return UtilGenerics.cast(lst.remove(listIndex)); - } else { - Object theValue = session.getAttribute(attributeName); - session.removeAttribute(attributeName); - return UtilGenerics.cast(theValue); } + return fma.remove(UtilGenerics.checkMap(theObj, String.class, Object.class)); } + if (isListReference) { + List lst = UtilGenerics.checkList(session.getAttribute(attributeName)); + return UtilGenerics.cast(lst.remove(listIndex)); + } + Object theValue = session.getAttribute(attributeName); + session.removeAttribute(attributeName); + return UtilGenerics.cast(theValue); } } } diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/LifoSet.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/LifoSet.java index e5509a250b..6525680b3b 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/LifoSet.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/LifoSet.java @@ -1,4 +1,5 @@ /******************************************************************************* + * 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 @@ -32,7 +33,7 @@ import java.util.LinkedList; public class LifoSet extends AbstractSet implements Serializable { // This set's back LinkedList - private LinkedList backedList = new LinkedList(); + private LinkedList backedList = new LinkedList<>(); private int maxCapacity = 10; /** diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapComparator.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapComparator.java index c09efcd95e..a12b741e85 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapComparator.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapComparator.java @@ -48,8 +48,15 @@ public class MapComparator implements Comparator> { */ @Override public boolean equals(Object obj) { + if (obj==null) { + return false; + } return obj.equals(this); } + + public int hashCode() { + return super.hashCode(); + } /** * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) @@ -117,9 +124,8 @@ public class MapComparator implements Comparator> { if (compareResult != 0) { if (ascending) { return compareResult; - } else { - return -compareResult; } + return -compareResult; } } 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 6ab47e2759..e8b9b2e0a9 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 @@ -43,7 +43,7 @@ public class MapContext implements Map, LocalizedMap { public static final String module = MapContext.class.getName(); public static final MapContext getMapContext() { - return new MapContext(); + return new MapContext<>(); } public static MapContext createMapContext() { @@ -75,15 +75,15 @@ public class MapContext implements Map, LocalizedMap { super(); } - protected List> stackList = new LinkedList>(); + protected List> stackList = new LinkedList<>(); public void reset() { - stackList = new LinkedList>(); + stackList = new LinkedList<>(); } /** Puts a new Map on the top of the stack */ public void push() { - Map newMap = new HashMap(); + Map newMap = new HashMap<>(); this.stackList.add(0,newMap); } @@ -108,9 +108,8 @@ public class MapContext implements Map, LocalizedMap { // 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); - } else { - return null; } + return null; } /** @@ -177,7 +176,7 @@ public class MapContext implements Map, LocalizedMap { */ public boolean containsValue(Object value) { // walk the stackList and the entries for each Map and if nothing is in for the current key, consider it an option, otherwise ignore - Set resultKeySet = new HashSet(); + Set resultKeySet = new HashSet<>(); for (Map curMap: this.stackList) { for (Map.Entry curEntry: curMap.entrySet()) { if (!resultKeySet.contains(curEntry.getKey())) { @@ -222,9 +221,8 @@ public class MapContext implements Map, LocalizedMap { if (curMap instanceof LocalizedMap) { LocalizedMap lmap = UtilGenerics.cast(curMap); return lmap.get(name, locale); - } else { - return curMap.get(name); } + return curMap.get(name); } } return null; @@ -270,7 +268,7 @@ public class MapContext implements Map, LocalizedMap { */ public Set keySet() { // walk the stackList and aggregate all keys - Set resultSet = new HashSet(); + Set resultSet = new HashSet<>(); for (Map curMap: this.stackList) { resultSet.addAll(curMap.keySet()); } @@ -282,8 +280,8 @@ public class MapContext implements Map, LocalizedMap { */ public Collection values() { // 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(); - List resultValues = new LinkedList(); + Set resultKeySet = new HashSet<>(); + List resultValues = new LinkedList<>(); for (Map curMap: this.stackList) { for (Map.Entry curEntry: curMap.entrySet()) { if (!resultKeySet.contains(curEntry.getKey())) { @@ -300,8 +298,8 @@ 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 resultKeySet = new HashSet<>(); + Set> resultEntrySet = new ListSet<>(); for (Map curMap: this.stackList) { for (Map.Entry curEntry: curMap.entrySet()) { if (!resultKeySet.contains(curEntry.getKey())) { @@ -338,12 +336,12 @@ public class MapContext implements Map, LocalizedMap { return fullMapString.toString(); } - private static final class ListSet extends AbstractSet implements Set { + private static final class ListSet extends AbstractSet { - protected final List listImpl; + private final List listImpl; public ListSet() { - this.listImpl = new ArrayList(); + this.listImpl = new ArrayList<>(); } public int size() { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/ResourceBundleMapWrapper.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/ResourceBundleMapWrapper.java index 6abf0ce0ff..4186c30ed2 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/ResourceBundleMapWrapper.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/collections/ResourceBundleMapWrapper.java @@ -174,13 +174,13 @@ public class ResourceBundleMapWrapper implements Map, Serializab // when the main Map doesn't have a certain value if (resourceBundle != null) { Set set = resourceBundle.keySet(); - topLevelMap = new HashMap(set.size()); + topLevelMap = new HashMap<>(set.size()); for (String key : set) { Object value = resourceBundle.getObject(key); topLevelMap.put(key, value); } } else { - topLevelMap = new HashMap(1); + topLevelMap = new HashMap<>(1); } topLevelMap.put("_RESOURCE_BUNDLE_", resourceBundle); isMapInitialized = true; @@ -190,13 +190,12 @@ public class ResourceBundleMapWrapper implements Map, Serializab /* (non-Javadoc) * @see java.util.Map#size() */ - public int size() { + public int size() { if(isMapInitialized) { // this is an approximate size, won't include elements from parent bundles return topLevelMap.size() -1; - } else { - return resourceBundle.keySet().size(); } + return resourceBundle.keySet().size(); } /* (non-Javadoc) @@ -205,9 +204,8 @@ public class ResourceBundleMapWrapper implements Map, Serializab public boolean isEmpty() { if (isMapInitialized) { return topLevelMap.isEmpty(); - } else { - return resourceBundle.keySet().size() == 0; } + return resourceBundle.keySet().size() == 0; } /* (non-Javadoc) @@ -220,9 +218,10 @@ public class ResourceBundleMapWrapper implements Map, Serializab } } else { try { - if (this.resourceBundle.getObject((String) arg0) != null) { - return true; - } + //the following will just be executed to check if arg0 is null, + //if so the thrown exception will be caught, if not true will be returned + this.resourceBundle.getObject((String) arg0); + return true; } catch (NullPointerException e) { // happens when arg0 is null } catch (MissingResourceException e) {