From d6b7985a99e401c870183b15d919155f1f265006 Mon Sep 17 00:00:00 2001 From: "Craig P. Motlin" Date: Mon, 21 Feb 2022 00:13:21 -0500 Subject: [PATCH] Implement missing implementations of toString(), fixing an issue mentioned in #1196. Also optimize some implementations of toString() that were delegating to iterators. Signed-off-by: Craig P. Motlin --- .../bimap/mutable/AbstractMutableBiMap.java | 18 ++- .../impl/bimap/mutable/SynchronizedBiMap.java | 14 -- .../map/AbstractSynchronizedMapIterable.java | 22 ++- .../map/mutable/SynchronizedMutableMap.java | 16 +- .../impl/map/mutable/UnifiedMap.java | 28 +++- .../sorted/mutable/SynchronizedSortedMap.java | 16 +- .../UnifiedMapWithHashingStrategy.java | 28 +++- .../collections/impl/utility/Iterate.java | 7 +- unit-tests-java8/pom.xml | 9 +- .../collections/test/IterableTestCase.java | 4 +- .../MutableSortedNaturalOrderTestCase.java | 40 ++--- .../test/RichIterableTestCase.java | 7 +- .../test/RichIterableUniqueTestCase.java | 17 ++- .../RichIterableWithDuplicatesTestCase.java | 2 +- .../test/SortedNaturalOrderTestCase.java | 24 +-- .../test/UnorderedIterableTestCase.java | 9 +- .../test/bag/UnsortedBagTestCase.java | 11 +- .../MutableSortedBagNoComparatorTestCase.java | 35 ++++- .../sorted/TreeBagNoComparatorTest.java | 38 +---- .../collections/test/bimap/BiMapTestCase.java | 88 ++++++++++- .../test/bimap/UnsortedBiMapTestCase.java | 39 ++--- .../ImmutableHashBiMapInverseTest.java | 27 +++- .../immutable/ImmutableHashBiMapTest.java | 21 ++- .../ImmutableUnsortedBiMapTestCase.java | 8 +- .../bimap/mutable/HashBiMapInverseTest.java | 26 +++- .../mutable/HashBiMapNoIteratorTest.java | 144 ++++++++++++++++++ .../test/bimap/mutable/HashBiMapTest.java | 23 ++- .../mutable/MutableUnsortedBiMapTestCase.java | 21 ++- .../bimap/mutable/UnmodifiableBiMapTest.java | 21 ++- .../collections/test/list/ListTestCase.java | 8 + .../list/mutable/MutableListTestCase.java | 6 + .../test/map/MapIterableTestCase.java | 30 +++- .../test/map/OrderedMapIterableTestCase.java | 10 -- .../test/map/SortedMapIterableTestCase.java | 13 +- .../test/map/UnsortedMapIterableTestCase.java | 43 +++--- .../mutable/MutableMapIterableTestCase.java | 12 ++ .../test/map/mutable/MutableMapTestCase.java | 18 ++- .../collections/test/set/SetTestCase.java | 8 +- .../test/set/UnsortedSetIterableTestCase.java | 20 +-- .../test/set/UnsortedSetLikeTestTrait.java | 11 +- .../test/set/mutable/MutableSetTestCase.java | 8 +- 41 files changed, 666 insertions(+), 284 deletions(-) create mode 100755 unit-tests-java8/src/test/java/org/eclipse/collections/test/bimap/mutable/HashBiMapNoIteratorTest.java diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/AbstractMutableBiMap.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/AbstractMutableBiMap.java index 8d793a375d..5dfa465c0f 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/AbstractMutableBiMap.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/AbstractMutableBiMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs and others. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -728,7 +728,7 @@ public void remove() } } - private class KeySet implements Set, Serializable + protected class KeySet implements Set, Serializable { @Override public boolean equals(Object obj) @@ -847,7 +847,7 @@ public Iterator iterator() @Override public String toString() { - return Iterate.makeString(this, "[", ", ", "]"); + return AbstractMutableBiMap.this.delegate.keySet().toString(); } protected Object writeReplace() @@ -858,7 +858,7 @@ protected Object writeReplace() } } - private class ValuesCollection implements Collection + protected class ValuesCollection implements Collection { @Override public int size() @@ -965,11 +965,11 @@ public Iterator iterator() @Override public String toString() { - return Iterate.makeString(this, "[", ", ", "]"); + return AbstractMutableBiMap.this.delegate.values().toString(); } } - private class EntrySet implements Set> + protected class EntrySet implements Set> { @Override public boolean equals(Object obj) @@ -991,6 +991,12 @@ public int hashCode() return AbstractMutableBiMap.this.hashCode(); } + @Override + public String toString() + { + return AbstractMutableBiMap.this.delegate.entrySet().toString(); + } + @Override public int size() { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/SynchronizedBiMap.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/SynchronizedBiMap.java index c73eed16d0..2aa85b0e0e 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/SynchronizedBiMap.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/bimap/mutable/SynchronizedBiMap.java @@ -15,7 +15,6 @@ import java.util.Map; import java.util.Set; -import org.eclipse.collections.api.RichIterable; import org.eclipse.collections.api.bimap.ImmutableBiMap; import org.eclipse.collections.api.bimap.MutableBiMap; import org.eclipse.collections.api.block.function.Function; @@ -36,7 +35,6 @@ import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable; import org.eclipse.collections.impl.map.mutable.SynchronizedBiMapSerializationProxy; import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet; -import org.eclipse.collections.impl.utility.LazyIterate; public class SynchronizedBiMap extends AbstractSynchronizedMapIterable implements MutableBiMap, Serializable { @@ -269,18 +267,6 @@ public MutableBiMap flipUniqueValues() } } - @Override - public RichIterable keysView() - { - return LazyIterate.adapt(this.keySet()); - } - - @Override - public RichIterable valuesView() - { - return LazyIterate.adapt(this.values()); - } - @Override public ImmutableBiMap toImmutable() { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/AbstractSynchronizedMapIterable.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/AbstractSynchronizedMapIterable.java index 14848c582e..9dc7a5843c 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/AbstractSynchronizedMapIterable.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/AbstractSynchronizedMapIterable.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs and others. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -37,7 +37,6 @@ import org.eclipse.collections.api.tuple.Pair; import org.eclipse.collections.impl.collection.AbstractSynchronizedRichIterable; import org.eclipse.collections.impl.tuple.AbstractImmutableEntry; -import org.eclipse.collections.impl.utility.Iterate; import org.eclipse.collections.impl.utility.LazyIterate; /** @@ -350,15 +349,22 @@ public MutableMapIterable aggregateBy( } } + @Override + public RichIterable keysView() + { + return LazyIterate.adapt(this.keySet()); + } + + @Override + public RichIterable valuesView() + { + return LazyIterate.adapt(this.values()); + } + @Override public RichIterable> keyValuesView() { - synchronized (this.lock) - { - Set> entries = this.getDelegate().entrySet(); - Iterable> pairs = Iterate.collect(entries, AbstractImmutableEntry.getPairFunction()); - return LazyIterate.adapt(pairs); - } + return LazyIterate.adapt(this.entrySet()).collect(AbstractImmutableEntry.getPairFunction()); } @Override diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/SynchronizedMutableMap.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/SynchronizedMutableMap.java index 008c5bcfd4..4b35b128ff 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/SynchronizedMutableMap.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/SynchronizedMutableMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs and others. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -16,7 +16,6 @@ import java.util.Map; import java.util.Set; -import org.eclipse.collections.api.RichIterable; import org.eclipse.collections.api.bag.MutableBag; import org.eclipse.collections.api.bag.primitive.MutableBooleanBag; import org.eclipse.collections.api.bag.primitive.MutableByteBag; @@ -54,7 +53,6 @@ import org.eclipse.collections.impl.list.fixed.ArrayAdapter; import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable; import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet; -import org.eclipse.collections.impl.utility.LazyIterate; /** * A synchronized view of a {@link MutableMap}. It is imperative that the user manually synchronize on the collection when iterating over it using the @@ -489,18 +487,6 @@ public Set> entrySet() } } - @Override - public RichIterable keysView() - { - return LazyIterate.adapt(this.keySet()); - } - - @Override - public RichIterable valuesView() - { - return LazyIterate.adapt(this.values()); - } - @Override public MutableMap asUnmodifiable() { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/UnifiedMap.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/UnifiedMap.java index 91833f8088..9d9b9ebbb3 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/UnifiedMap.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/mutable/UnifiedMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -42,6 +42,7 @@ import org.eclipse.collections.api.tuple.Pair; import org.eclipse.collections.impl.block.factory.Functions; import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.block.procedure.AppendStringProcedure; import org.eclipse.collections.impl.block.procedure.MapCollectProcedure; import org.eclipse.collections.impl.list.mutable.FastList; import org.eclipse.collections.impl.parallel.BatchIterable; @@ -2346,7 +2347,12 @@ public int hashCode() @Override public String toString() { - return Iterate.makeString(this, "[", ", ", "]"); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('['); + Procedure appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", "); + this.forEach(appendStringProcedure); + stringBuilder.append(']'); + return stringBuilder.toString(); } @Override @@ -2897,6 +2903,17 @@ public int hashCode() { return UnifiedMap.this.hashCode(); } + + @Override + public String toString() + { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('['); + Procedure> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", "); + this.forEach(appendStringProcedure); + stringBuilder.append(']'); + return stringBuilder.toString(); + } } protected class EntrySetIterator extends PositionalIterator> @@ -3228,7 +3245,12 @@ private void chainedAddToList(Object[] chain, FastList replace) @Override public String toString() { - return Iterate.makeString(this, "[", ", ", "]"); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('['); + Procedure appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", "); + this.forEach(appendStringProcedure); + stringBuilder.append(']'); + return stringBuilder.toString(); } } diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/sorted/mutable/SynchronizedSortedMap.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/sorted/mutable/SynchronizedSortedMap.java index 35243f9636..3359fc4bc6 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/sorted/mutable/SynchronizedSortedMap.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/sorted/mutable/SynchronizedSortedMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs and others. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -18,7 +18,6 @@ import java.util.SortedMap; import org.eclipse.collections.api.LazyIterable; -import org.eclipse.collections.api.RichIterable; import org.eclipse.collections.api.block.function.Function; import org.eclipse.collections.api.block.function.Function2; import org.eclipse.collections.api.block.function.primitive.BooleanFunction; @@ -63,7 +62,6 @@ import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable; import org.eclipse.collections.impl.map.mutable.SynchronizedMapSerializationProxy; import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet; -import org.eclipse.collections.impl.utility.LazyIterate; /** * A synchronized view of a SortedMap. @@ -507,18 +505,6 @@ public MutableSortedMap collectValues(Function2 keysView() - { - return LazyIterate.adapt(this.keySet()); - } - - @Override - public RichIterable valuesView() - { - return LazyIterate.adapt(this.values()); - } - @Override public MutableSortedMap asUnmodifiable() { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/strategy/mutable/UnifiedMapWithHashingStrategy.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/strategy/mutable/UnifiedMapWithHashingStrategy.java index 076f8b94d0..50bfc34059 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/strategy/mutable/UnifiedMapWithHashingStrategy.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/map/strategy/mutable/UnifiedMapWithHashingStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs and others. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -39,6 +39,7 @@ import org.eclipse.collections.api.tuple.Pair; import org.eclipse.collections.impl.block.factory.Functions; import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.block.procedure.AppendStringProcedure; import org.eclipse.collections.impl.block.procedure.MapCollectProcedure; import org.eclipse.collections.impl.factory.HashingStrategyMaps; import org.eclipse.collections.impl.list.mutable.FastList; @@ -1959,7 +1960,12 @@ public int hashCode() @Override public String toString() { - return Iterate.makeString(this, "[", ", ", "]"); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('['); + Procedure appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", "); + this.forEach(appendStringProcedure); + stringBuilder.append(']'); + return stringBuilder.toString(); } @Override @@ -2513,6 +2519,17 @@ public int hashCode() { return UnifiedMapWithHashingStrategy.this.hashCode(); } + + @Override + public String toString() + { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('['); + Procedure> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", "); + this.forEach(appendStringProcedure); + stringBuilder.append(']'); + return stringBuilder.toString(); + } } protected class EntrySetIterator extends PositionalIterator> @@ -2852,7 +2869,12 @@ private void chainedAddToList(Object[] chain, FastList replace) @Override public String toString() { - return Iterate.makeString(this, "[", ", ", "]"); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append('['); + Procedure appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", "); + this.forEach(appendStringProcedure); + stringBuilder.append(']'); + return stringBuilder.toString(); } } diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/Iterate.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/Iterate.java index 42326f4630..848eb382c6 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/Iterate.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/Iterate.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Goldman Sachs. + * Copyright (c) 2022 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -86,6 +86,7 @@ import org.eclipse.collections.impl.multimap.bag.HashBagMultimap; import org.eclipse.collections.impl.multimap.list.FastListMultimap; import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap; +import org.eclipse.collections.impl.parallel.BatchIterable; import org.eclipse.collections.impl.utility.internal.DefaultSpeciesNewStrategy; import org.eclipse.collections.impl.utility.internal.IterableIterate; import org.eclipse.collections.impl.utility.internal.RandomAccessListIterate; @@ -130,6 +131,10 @@ public static void forEach(Iterable iterable, Procedure proced { ((InternalIterable) iterable).forEach(procedure); } + else if (iterable instanceof BatchIterable) + { + ((BatchIterable) iterable).forEach(procedure); + } else if (iterable instanceof ArrayList) { ArrayListIterate.forEach((ArrayList) iterable, procedure); diff --git a/unit-tests-java8/pom.xml b/unit-tests-java8/pom.xml index e4fda8a670..16013e8b8e 100644 --- a/unit-tests-java8/pom.xml +++ b/unit-tests-java8/pom.xml @@ -1,7 +1,7 @@