From 6fddda899a1a0aeaa71477af086cee318895a763 Mon Sep 17 00:00:00 2001 From: des Date: Tue, 4 Jan 2022 17:47:03 +0000 Subject: [PATCH] Add fused method for collect + makeString --- .../eclipse/collections/api/ParallelIterable.java | 5 +++++ .../org/eclipse/collections/api/RichIterable.java | 11 +++++++++++ .../AbstractSynchronizedRichIterable.java | 9 +++++++++ .../AbstractMultiReaderMutableCollection.java | 9 +++++++++ .../AbstractMultiReaderParallelIterable.java | 14 ++++++++++++++ .../collections/test/RichIterableTestCase.java | 10 ++++++++++ .../test/map/mutable/UnifiedMapNoIteratorTest.java | 6 ++++++ ...nifiedMapWithHashingStrategyNoIteratorTest.java | 6 ++++++ .../impl/AbstractRichIterableTestCase.java | 9 +++++++++ 9 files changed, 79 insertions(+) diff --git a/eclipse-collections-api/src/main/java/org/eclipse/collections/api/ParallelIterable.java b/eclipse-collections-api/src/main/java/org/eclipse/collections/api/ParallelIterable.java index 650ef9ba38..842a435b95 100644 --- a/eclipse-collections-api/src/main/java/org/eclipse/collections/api/ParallelIterable.java +++ b/eclipse-collections-api/src/main/java/org/eclipse/collections/api/ParallelIterable.java @@ -248,6 +248,11 @@ default String makeString(String start, String separator, String end) return stringBuilder.toString(); } + default String makeString(Function function, String start, String separator, String end) + { + return this.collect(function).makeString(start, separator, end); + } + default void appendString(Appendable appendable) { this.appendString(appendable, ", "); diff --git a/eclipse-collections-api/src/main/java/org/eclipse/collections/api/RichIterable.java b/eclipse-collections-api/src/main/java/org/eclipse/collections/api/RichIterable.java index 3f3862ee4c..fd0c259753 100644 --- a/eclipse-collections-api/src/main/java/org/eclipse/collections/api/RichIterable.java +++ b/eclipse-collections-api/src/main/java/org/eclipse/collections/api/RichIterable.java @@ -2298,6 +2298,17 @@ default String makeString(String start, String separator, String end) return stringBuilder.toString(); } + /** + * Returns a string representation of the collection, created by applying the function supplied to each element, + * with the elements separated by the specified separator and enclosed between the start and end strings. + * + * @return a string representation of the mapped collection + */ + default String makeString(Function function, String start, String separator, String end) + { + return this.asLazy().collect(function).makeString(start, separator, end); + } + /** * Prints a string representation of this collection onto the given {@code Appendable}. Prints the string returned * by {@link #makeString()}. diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/AbstractSynchronizedRichIterable.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/AbstractSynchronizedRichIterable.java index 581f491c95..0f1c7ad825 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/AbstractSynchronizedRichIterable.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/AbstractSynchronizedRichIterable.java @@ -1231,6 +1231,15 @@ public String makeString(String start, String separator, String end) } } + @Override + public String makeString(Function function, String start, String separator, String end) + { + synchronized (this.lock) + { + return this.delegate.makeString(function, start, separator, end); + } + } + @Override public void appendString(Appendable appendable) { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/mutable/AbstractMultiReaderMutableCollection.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/mutable/AbstractMultiReaderMutableCollection.java index bf840d1040..73b5cac64a 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/mutable/AbstractMultiReaderMutableCollection.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/collection/mutable/AbstractMultiReaderMutableCollection.java @@ -1371,6 +1371,15 @@ public String makeString(String start, String separator, String end) } } + @Override + public String makeString(Function function, String start, String separator, String end) + { + try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) + { + return this.getDelegate().makeString(function, start, separator, end); + } + } + @Override public void appendString(Appendable appendable) { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/parallel/AbstractMultiReaderParallelIterable.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/parallel/AbstractMultiReaderParallelIterable.java index e953526135..9ba7315843 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/parallel/AbstractMultiReaderParallelIterable.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/parallel/AbstractMultiReaderParallelIterable.java @@ -680,6 +680,20 @@ public String makeString(String start, String separator, String end) } } + @Override + public String makeString(Function function, String start, String separator, String end) + { + this.lock.readLock().lock(); + try + { + return this.delegate.makeString(function, start, separator, end); + } + finally + { + this.lock.readLock().unlock(); + } + } + @Override public void appendString(Appendable appendable) { diff --git a/unit-tests-java8/src/test/java/org/eclipse/collections/test/RichIterableTestCase.java b/unit-tests-java8/src/test/java/org/eclipse/collections/test/RichIterableTestCase.java index af3c2d1f43..52dc24b719 100644 --- a/unit-tests-java8/src/test/java/org/eclipse/collections/test/RichIterableTestCase.java +++ b/unit-tests-java8/src/test/java/org/eclipse/collections/test/RichIterableTestCase.java @@ -1997,6 +1997,16 @@ default void RichIterable_injectInto_primitive() Assert.assertEquals(30.0f, iterable.injectInto(0, AddFunction.INTEGER_TO_FLOAT), 0.001f); } + @Test + default void RichIterable_fused_collectMakeString() + { + RichIterable iterable = this.newWith(0, 1, 8); + + assertEquals( + iterable.asLazy().collect(Integer::toUnsignedString).makeString("[", ",", "]"), + iterable.makeString(Integer::toUnsignedString, "[", ",", "]")); + } + @Test default void RichIterable_makeString_appendString() { diff --git a/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapNoIteratorTest.java b/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapNoIteratorTest.java index dcaddcd1b5..0cc8629fbb 100644 --- a/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapNoIteratorTest.java +++ b/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapNoIteratorTest.java @@ -94,6 +94,12 @@ public void RichIterable_getLast_empty_null() // Not applicable } + @Override + public void RichIterable_fused_collectMakeString() + { + // Not applicable + } + @Override public void MutableMapIterable_updateValue() { diff --git a/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapWithHashingStrategyNoIteratorTest.java b/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapWithHashingStrategyNoIteratorTest.java index f133e3647d..85a0874323 100644 --- a/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapWithHashingStrategyNoIteratorTest.java +++ b/unit-tests-java8/src/test/java/org/eclipse/collections/test/map/mutable/UnifiedMapWithHashingStrategyNoIteratorTest.java @@ -95,6 +95,12 @@ public void RichIterable_getLast_empty_null() // Not applicable } + @Override + public void RichIterable_fused_collectMakeString() + { + // Not applicable + } + @Override public void RichIterable_anySatisfy_allSatisfy_noneSatisfy() { diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/AbstractRichIterableTestCase.java b/unit-tests/src/test/java/org/eclipse/collections/impl/AbstractRichIterableTestCase.java index 21a8e1b148..1805611cd7 100644 --- a/unit-tests/src/test/java/org/eclipse/collections/impl/AbstractRichIterableTestCase.java +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/AbstractRichIterableTestCase.java @@ -1841,6 +1841,15 @@ public void makeStringWithSeparatorAndStartAndEnd() Assert.assertEquals(collection.toString(), collection.makeString("[", ", ", "]")); } + @Test + public void fusedCollectMakeString() + { + RichIterable collection = this.newWith(1, 2, 3); + Assert.assertEquals( + collection.asLazy().collect(Integer::toUnsignedString).makeString("[", ", ", "]"), + collection.makeString(Integer::toUnsignedString, "[", ", ", "]")); + } + @Test public void appendString() {