Improved: General refactoring and code improvements, package

org.apache.ofbiz.base.util.collections.
(OFBIZ-9860)

Thanks Dennis Balkir for reporting and providing the patch.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1817743 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-11 06:34:02 +00:00
parent bf4bfeaf17
commit 7cc0b229a6
14 changed files with 130 additions and 52 deletions
@@ -52,7 +52,9 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
}
public final void clear() {
if (isEmpty()) return;
if (isEmpty()) {
return;
}
incrementModCount();
clearInternal();
}
@@ -65,11 +67,19 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
@Override
public boolean equals(Object o) {
if (!(o instanceof Map<?, ?>)) return false;
if (this == o) return true;
if (!(o instanceof Map<?, ?>)) {
return false;
}
if (this == o) {
return true;
}
Map<?, ?> map = (Map<?, ?>) o;
if (size() != map.size()) return false;
if (o instanceof GenericMap<?, ?>) return equalsGenericMap((GenericMap<?, ?>) o);
if (size() != map.size()) {
return false;
}
if (o instanceof GenericMap<?, ?>) {
return equalsGenericMap((GenericMap<?, ?>) o);
}
return equalsMap(map);
}
@@ -80,11 +90,17 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
K key = entry.getKey();
V value = entry.getValue();
if (value != null) {
if (!value.equals(map.get(key, false))) return false;
if (!value.equals(map.get(key, false))) {
return false;
}
} else {
Object otherValue = map.get(key, false);
if (otherValue != null) return false;
if (!map.containsKey(key)) return false;
if (otherValue != null) {
return false;
}
if (!map.containsKey(key)) {
return false;
}
}
}
return true;
@@ -97,11 +113,17 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
K key = entry.getKey();
V value = entry.getValue();
if (value != null) {
if (!value.equals(map.get(key))) return false;
if (!value.equals(map.get(key))) {
return false;
}
} else {
Object otherValue = map.get(key);
if (otherValue != null) return false;
if (!map.containsKey(key)) return false;
if (otherValue != null) {
return false;
}
if (!map.containsKey(key)) {
return false;
}
}
}
return true;
@@ -121,7 +143,9 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
}
protected boolean isValid(Map.Entry<K, V> src) {
if (currentModCount != getModCount()) throw new ConcurrentModificationException();
if (currentModCount != getModCount()) {
throw new ConcurrentModificationException();
}
return true;
}
}
@@ -214,7 +238,9 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
}
private <KE extends K, VE extends V> void putAllInternal(Map<KE, VE> map) {
if (map.isEmpty()) return;
if (map.isEmpty()) {
return;
}
incrementModCount();
Iterator<Map.Entry<KE, VE>> it;
if (map instanceof GenericMap<?, ?>) {
@@ -239,7 +265,9 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
while (it.hasNext()) {
Map.Entry<K, V> entry = it.next();
sb.append(entry.getKey()).append("=").append(entry.getValue());
if (it.hasNext()) sb.append(",");
if (it.hasNext()) {
sb.append(",");
}
}
return sb.append("}");
}
@@ -45,7 +45,9 @@ public abstract class GenericMapCollection<K, V, M extends Map<K, V>, I> impleme
public boolean containsAll(Collection<?> collection) {
for (Object item: collection) {
if (!contains(item)) return false;
if (!contains(item)) {
return false;
}
}
return true;
}
@@ -63,7 +65,9 @@ public abstract class GenericMapCollection<K, V, M extends Map<K, V>, I> impleme
public boolean removeAll(Collection<?> collection) {
int count = 0;
for (Object item: collection) {
if (remove(item)) count++;
if (remove(item)) {
count++;
}
}
return count > 0;
}
@@ -86,7 +90,7 @@ public abstract class GenericMapCollection<K, V, M extends Map<K, V>, I> impleme
}
public Object[] toArray() {
List<I> list = new LinkedList<I>();
List<I> list = new LinkedList<>();
Iterator<I> it = iterator(false);
while (it.hasNext()) {
list.add(it.next());
@@ -95,7 +99,7 @@ public abstract class GenericMapCollection<K, V, M extends Map<K, V>, I> impleme
}
public <T> T[] toArray(T[] array) {
List<Object> list = new LinkedList<Object>();
List<Object> list = new LinkedList<>();
Iterator<I> it = iterator(false);
while (it.hasNext()) {
list.add(it.next());
@@ -113,7 +117,9 @@ public abstract class GenericMapCollection<K, V, M extends Map<K, V>, I> impleme
Iterator<I> it = iterator(false);
while (it.hasNext()) {
sb.append(it.next());
if (it.hasNext()) sb.append(", ");
if (it.hasNext()) {
sb.append(", ");
}
}
return sb.append("]");
}
@@ -47,8 +47,12 @@ public class GenericMapEntry<K, V> implements Map.Entry<K, V> {
@Override
public boolean equals(Object o) {
if (!(o instanceof Map.Entry<?, ?>)) return false;
if (this == o) return true;
if (!(o instanceof Map.Entry<?, ?>)) {
return false;
}
if (this == o) {
return true;
}
Map.Entry<?, ?> other = (Map.Entry<?, ?>) o;
return UtilObject.equalsHelper(getKey(), other.getKey()) && UtilObject.equalsHelper(getValue(), other.getValue());
}
@@ -26,8 +26,12 @@ public abstract class GenericMapEntrySet<K, V, M extends GenericMap<K, V>> exten
}
public boolean contains(Object item) {
if (item == null) return false;
if (!(item instanceof Map.Entry<?, ?>)) return false;
if (item == null) {
return false;
}
if (!(item instanceof Map.Entry<?, ?>)) {
return false;
}
Map.Entry<?, ?> other = (Map.Entry<?, ?>) item;
return contains(other.getKey(), other.getValue());
}
@@ -35,11 +39,17 @@ public abstract class GenericMapEntrySet<K, V, M extends GenericMap<K, V>> exten
protected abstract boolean contains(Object key, Object value);
public boolean remove(Object item) {
if (item == null) return false;
if (!(item instanceof Map.Entry<?, ?>)) return false;
if (item == null) {
return false;
}
if (!(item instanceof Map.Entry<?, ?>)) {
return false;
}
Map.Entry<?, ?> other = (Map.Entry<?, ?>) item;
Object key = other.getKey();
if (!source.containsKey(key)) return false;
if (!source.containsKey(key)) {
return false;
}
source.remove(key);
return true;
}
@@ -28,11 +28,17 @@ public abstract class GenericMapSet<K, V, M extends Map<K, V>, I> extends Generi
@Override
public final boolean equals(Object o) {
if (!(o instanceof Set<?>)) return false;
if (!(o instanceof Set<?>)) {
return false;
}
Set<?> other = (Set<?>) o;
if (source.size() != other.size()) return false;
if (source.size() != other.size()) {
return false;
}
for (I item: this) {
if (!other.contains(item)) return false;
if (!other.contains(item)) {
return false;
}
}
return true;
}
@@ -41,7 +47,9 @@ public abstract class GenericMapSet<K, V, M extends Map<K, V>, I> extends Generi
public final int hashCode() {
int h = 0;
for (I item: this) {
if (item == null) continue;
if (item == null) {
continue;
}
h += item.hashCode();
}
return h;
@@ -34,21 +34,31 @@ public abstract class GenericMapValues<K, V, M extends Map<K, V>> extends Generi
public boolean contains(Object item) {
Iterator<V> it = iterator(false);
while (it.hasNext()) {
if (UtilObject.equalsHelper(item, it.next())) return true;
if (UtilObject.equalsHelper(item, it.next())) {
return true;
}
}
return false;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Collection<?>)) return false;
if (o instanceof List<?> || o instanceof Set<?>) return false;
if (!(o instanceof Collection<?>)) {
return false;
}
if (o instanceof List<?> || o instanceof Set<?>) {
return false;
}
Collection<?> other = (Collection<?>) o;
if (source.size() != other.size()) return false;
if (source.size() != other.size()) {
return false;
}
Iterator<V> it = iterator(false);
while (it.hasNext()) {
V item = it.next();
if (!other.contains(item)) return false;
if (!other.contains(item)) {
return false;
}
}
return true;
}
@@ -59,7 +69,9 @@ public abstract class GenericMapValues<K, V, M extends Map<K, V>> extends Generi
Iterator<V> it = iterator(false);
while (it.hasNext()) {
V item = it.next();
if (item == null) continue;
if (item == null) {
continue;
}
h += item.hashCode();
}
return h;
@@ -32,8 +32,12 @@ public abstract class IteratorWrapper<DEST, SRC> implements Iterator<DEST> {
}
public boolean hasNext() {
if (nextCalled) return true;
if (!it.hasNext()) return false;
if (nextCalled) {
return true;
}
if (!it.hasNext()) {
return false;
}
do {
lastSrc = it.next();
DEST nextDest = convert(lastSrc);
@@ -48,7 +52,9 @@ public abstract class IteratorWrapper<DEST, SRC> implements Iterator<DEST> {
public DEST next() {
if (!nextCalled) {
if (!hasNext()) throw new NoSuchElementException();
if (!hasNext()) {
throw new NoSuchElementException();
}
}
nextCalled = false;
return lastDest;
@@ -79,8 +79,9 @@ public class LifoSet<V> extends AbstractSet<V> implements Serializable {
if (index == -1) {
backedList.addFirst(obj);
while (size() > maxCapacity)
while (size() > maxCapacity) {
backedList.removeLast();
}
} else {
backedList.remove(index);
backedList.addFirst(obj);
@@ -53,7 +53,7 @@ public class MapComparator implements Comparator<Map<Object, Object>> {
}
return obj.equals(this);
}
public int hashCode() {
return super.hashCode();
}
@@ -53,11 +53,10 @@ public class MapContext<K, V> implements Map<K, V>, LocalizedMap<V> {
return newValue;
}
@SuppressWarnings("unchecked")
public static <K, V> MapContext<K, V> createMapContext(Map<K, V> baseMap) {
MapContext<K, V> newValue = MapContext.getMapContext();
if (baseMap instanceof MapContext) {
newValue.stackList.addAll(((MapContext) baseMap).stackList);
newValue.stackList.addAll(((MapContext<K, V>) baseMap).stackList);
} else {
newValue.stackList.add(0, baseMap);
}
@@ -32,7 +32,7 @@ public class MapStack<K> extends MapContext<K, Object> {
public static final String module = MapStack.class.getName();
public static <K> MapStack<K> create() {
MapStack<K> newValue = new MapStack<K>();
MapStack<K> newValue = new MapStack<>();
// initialize with a single entry
newValue.push();
return newValue;
@@ -40,9 +40,9 @@ public class MapStack<K> extends MapContext<K, Object> {
@SuppressWarnings("unchecked")
public static <K> MapStack<K> create(Map<K, Object> baseMap) {
MapStack<K> newValue = new MapStack<K>();
MapStack<K> newValue = new MapStack<>();
if (baseMap instanceof MapStack) {
newValue.stackList.addAll(((MapStack) baseMap).stackList);
newValue.stackList.addAll(((MapStack<K>) baseMap).stackList);
} else {
newValue.stackList.add(0, baseMap);
}
@@ -51,7 +51,7 @@ public class MapStack<K> extends MapContext<K, Object> {
/** Does a shallow copy of the internal stack of the passed MapStack; enables simultaneous stacks that share common parent Maps */
public static <K> MapStack<K> create(MapStack<K> source) {
MapStack<K> newValue = new MapStack<K>();
MapStack<K> newValue = new MapStack<>();
newValue.stackList.addAll(source.stackList);
return newValue;
}
@@ -60,7 +60,7 @@ public class PagedList<E> implements Iterable<E> {
*/
public static <E> PagedList<E> empty(int viewIndex, int viewSize) {
List<E> emptyList = Collections.emptyList();
return new PagedList<E>(0, 0, 0, viewIndex, viewSize, emptyList);
return new PagedList<>(0, 0, 0, viewIndex, viewSize, emptyList);
}
/**
@@ -78,7 +78,7 @@ public class PagedList<E> implements Iterable<E> {
}
/**
* @return the size of the full list, this can be the
* @return the size of the full list, this can be the
* result of <code>EntityListIterator.getResultsSizeAfterPartialList()</code>
*/
public int getSize() {
@@ -48,8 +48,12 @@ public class ReadOnlyMapEntry<K, V> implements Map.Entry<K, V> {
@Override
public boolean equals(Object o) {
if (!(o instanceof Map.Entry<?, ?>)) return false;
if (this == o) return true;
if (!(o instanceof Map.Entry<?, ?>)) {
return false;
}
if (this == o) {
return true;
}
Map.Entry<?, ?> other = (Map.Entry<?, ?>) o;
return UtilObject.equalsHelper(getKey(), other.getKey()) && UtilObject.equalsHelper(getValue(), other.getValue());
}
@@ -221,7 +221,7 @@ public class ResourceBundleMapWrapper implements Map<String, Object>, Serializab
}
} else {
try {
//the following will just be executed to check if arg0 is null,
//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;