From ef6d68a149908ed9c738f9d4267f443db916717e Mon Sep 17 00:00:00 2001 From: Sirisha Pratha Date: Thu, 10 Sep 2020 16:06:54 -0400 Subject: [PATCH] Fix CollectIterable detect methods. Closes #994 Signed-off-by: Sirisha Pratha --- .../impl/lazy/CollectIterable.java | 55 +++++++++++-- .../impl/lazy/CollectIterableTest.java | 77 ++++++++++++++++++- 2 files changed, 126 insertions(+), 6 deletions(-) diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/CollectIterable.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/CollectIterable.java index ed181ba668..fdc6174a67 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/CollectIterable.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/lazy/CollectIterable.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Goldman Sachs. + * Copyright (c) 2020 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. @@ -112,15 +112,17 @@ public boolean noneSatisfy(Predicate predicate) @Override public V detect(Predicate predicate) { - T resultItem = Iterate.detect(this.adapted, Predicates.attributePredicate(this.function, predicate)); - return resultItem == null ? null : this.function.valueOf(resultItem); + AttributePredicate attributePredicate = new AttributePredicate<>(this.function, predicate); + T resultItem = Iterate.detect(this.adapted, attributePredicate); + return resultItem == null ? null : attributePredicate.functionResult(); } @Override public Optional detectOptional(Predicate predicate) { - Optional resultItem = Iterate.detectOptional(this.adapted, Predicates.attributePredicate(this.function, predicate)); - return resultItem.map(this.function::valueOf); + AttributePredicate attributePredicate = new AttributePredicate<>(this.function, predicate); + Optional resultItem = Iterate.detectOptional(this.adapted, attributePredicate); + return resultItem.isPresent() ? Optional.of(attributePredicate.functionResult()) : Optional.empty(); } @Override @@ -172,4 +174,47 @@ public V getLast() } return this.function.valueOf(Iterate.getLast(this.adapted)); } + + private static final class AttributePredicate implements Predicate + { + private static final long serialVersionUID = 1L; + private final Function function; + private final Predicate predicate; + private transient V functionResult; + + private AttributePredicate( + Function newFunction, + Predicate newPredicate) + { + this.function = newFunction; + this.predicate = newPredicate; + } + + @Override + public boolean accept(T anObject) + { + V functionValue = this.function.valueOf(anObject); + boolean result = this.predicate.accept(functionValue); + if (result) + { + this.functionResult = functionValue; + } + return result; + } + + public V functionResult() + { + return this.functionResult; + } + + @Override + public String toString() + { + return "AttributePredicate(" + + this.function + + ", " + + this.predicate + + ')'; + } + } } diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/lazy/CollectIterableTest.java b/unit-tests/src/test/java/org/eclipse/collections/impl/lazy/CollectIterableTest.java index ede2bc39c1..d0a3a09b27 100644 --- a/unit-tests/src/test/java/org/eclipse/collections/impl/lazy/CollectIterableTest.java +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/lazy/CollectIterableTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Goldman Sachs. + * Copyright (c) 2020 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. @@ -10,9 +10,12 @@ package org.eclipse.collections.impl.lazy; +import java.util.concurrent.atomic.AtomicInteger; + import org.eclipse.collections.api.InternalIterable; import org.eclipse.collections.api.LazyIterable; import org.eclipse.collections.api.block.procedure.Procedure; +import org.eclipse.collections.api.list.MultiReaderList; import org.eclipse.collections.impl.block.factory.Functions; import org.eclipse.collections.impl.block.factory.Procedures; import org.eclipse.collections.impl.factory.Lists; @@ -139,5 +142,77 @@ public void toArray() stringNums.toArray(); Assert.assertEquals(Lists.immutable.of("0", "1", "2", "3"), Lists.immutable.ofAll(stringNums)); } + + @Override + @Test + public void detect() + { + super.detect(); + AtomicInteger functionCount = new AtomicInteger(0); + MultiReaderList integers = Lists.multiReader.withAll(Interval.oneTo(5)); + CollectIterable collect = new CollectIterable<>(integers, functionCount::addAndGet); + Assert.assertEquals(3L, collect.detect(each -> each.equals(3)).longValue()); + Assert.assertNull(collect.detect(each -> each.equals(100))); + } + + @Override + @Test + public void detectIfNone() + { + super.detectIfNone(); + AtomicInteger functionCount = new AtomicInteger(0); + MultiReaderList integers = Lists.multiReader.withAll(Interval.oneTo(5)); + CollectIterable collect = new CollectIterable<>(integers, functionCount::addAndGet); + Assert.assertEquals(3L, collect.detectIfNone(each -> each.equals(3), () -> Integer.valueOf(0)).longValue()); + Assert.assertNull(collect.detectIfNone(each -> each.equals(100), () -> null)); + } + + @Override + @Test + public void detectWith() + { + super.detectWith(); + AtomicInteger functionCount = new AtomicInteger(0); + MultiReaderList integers = Lists.multiReader.withAll(Interval.oneTo(5)); + CollectIterable collect = new CollectIterable<>(integers, functionCount::addAndGet); + Assert.assertEquals(3L, collect.detectWith((each, ignore) -> each.equals(3), null).longValue()); + Assert.assertNull(collect.detectWith((each, ignore) -> each.equals(100), null)); + } + + @Override + @Test + public void detectWithIfNone() + { + super.detectWithIfNone(); + AtomicInteger functionCount = new AtomicInteger(0); + MultiReaderList integers = Lists.multiReader.withAll(Interval.oneTo(5)); + CollectIterable collect = new CollectIterable<>(integers, functionCount::addAndGet); + Assert.assertEquals(3L, collect.detectWithIfNone((each, ignore) -> each.equals(3), null, () -> Integer.valueOf(0)).longValue()); + Assert.assertNull(collect.detectWithIfNone((each, ignore) -> each.equals(100), null, () -> null)); + } + + @Override + @Test + public void detectOptional() + { + super.detectOptional(); + AtomicInteger functionCount = new AtomicInteger(0); + MultiReaderList integers = Lists.multiReader.withAll(Interval.oneTo(5)); + CollectIterable collect = new CollectIterable<>(integers, functionCount::addAndGet); + Assert.assertEquals(3L, collect.detectOptional(each -> each.equals(3)).get().longValue()); + Assert.assertNull(collect.detectOptional(each -> each.equals(100)).orElse(null)); + } + + @Override + @Test + public void detectWithOptional() + { + super.detectWithOptional(); + AtomicInteger functionCount = new AtomicInteger(0); + MultiReaderList integers = Lists.multiReader.withAll(Interval.oneTo(5)); + CollectIterable collect = new CollectIterable<>(integers, functionCount::addAndGet); + Assert.assertEquals(3L, collect.detectWithOptional((each, ignore) -> each.equals(3), null).get().longValue()); + Assert.assertNull(collect.detectWithOptional((each, ignore) -> each.equals(100), null).orElse(null)); + } }