Skip to content

Commit

Permalink
Fix CollectIterable detect methods. Closes #994
Browse files Browse the repository at this point in the history
Signed-off-by: Sirisha Pratha <sirisha.pratha@bnymellon.com>
  • Loading branch information
prathasirisha committed Sep 10, 2020
1 parent ef83b90 commit ef6d68a
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -112,15 +112,17 @@ public boolean noneSatisfy(Predicate<? super V> predicate)
@Override
public V detect(Predicate<? super V> predicate)
{
T resultItem = Iterate.detect(this.adapted, Predicates.attributePredicate(this.function, predicate));
return resultItem == null ? null : this.function.valueOf(resultItem);
AttributePredicate<T, V> attributePredicate = new AttributePredicate<>(this.function, predicate);
T resultItem = Iterate.detect(this.adapted, attributePredicate);
return resultItem == null ? null : attributePredicate.functionResult();
}

@Override
public Optional<V> detectOptional(Predicate<? super V> predicate)
{
Optional<T> resultItem = Iterate.detectOptional(this.adapted, Predicates.attributePredicate(this.function, predicate));
return resultItem.map(this.function::valueOf);
AttributePredicate<T, V> attributePredicate = new AttributePredicate<>(this.function, predicate);
Optional<T> resultItem = Iterate.detectOptional(this.adapted, attributePredicate);
return resultItem.isPresent() ? Optional.of(attributePredicate.functionResult()) : Optional.empty();
}

@Override
Expand Down Expand Up @@ -172,4 +174,47 @@ public V getLast()
}
return this.function.valueOf(Iterate.getLast(this.adapted));
}

private static final class AttributePredicate<T, V> implements Predicate<T>
{
private static final long serialVersionUID = 1L;
private final Function<? super T, ? extends V> function;
private final Predicate<? super V> predicate;
private transient V functionResult;

private AttributePredicate(
Function<? super T, ? extends V> newFunction,
Predicate<? super V> 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
+ ')';
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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<Integer> integers = Lists.multiReader.withAll(Interval.oneTo(5));
CollectIterable<Integer, Integer> 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<Integer> integers = Lists.multiReader.withAll(Interval.oneTo(5));
CollectIterable<Integer, Integer> 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<Integer> integers = Lists.multiReader.withAll(Interval.oneTo(5));
CollectIterable<Integer, Integer> 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<Integer> integers = Lists.multiReader.withAll(Interval.oneTo(5));
CollectIterable<Integer, Integer> 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<Integer> integers = Lists.multiReader.withAll(Interval.oneTo(5));
CollectIterable<Integer, Integer> 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<Integer> integers = Lists.multiReader.withAll(Interval.oneTo(5));
CollectIterable<Integer, Integer> 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));
}
}

0 comments on commit ef6d68a

Please sign in to comment.