Skip to content

Commit

Permalink
Optimize DropIterable to not delegate to an iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
motlin committed Mar 10, 2016
1 parent a170425 commit 826a007
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2016 Goldman Sachs.
* 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 @@ -13,9 +13,11 @@
import java.util.Iterator;

import net.jcip.annotations.Immutable;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.predicate.DropIterablePredicate;
import org.eclipse.collections.impl.block.procedure.IfObjectIntProcedure;
import org.eclipse.collections.impl.block.procedure.IfProcedure;
Expand Down Expand Up @@ -60,6 +62,30 @@ public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parame
Iterate.forEachWith(this.adapted, new IfProcedureWith<T, P>(new DropIterablePredicate<T>(this.count), procedure), parameter);
}

@Override
public boolean anySatisfy(Predicate<? super T> predicate)
{
return Iterate.anySatisfy(this.adapted, Predicates.and(new DropIterablePredicate<T>(this.count), predicate));
}

@Override
public boolean allSatisfy(Predicate<? super T> predicate)
{
return Iterate.allSatisfy(this.adapted, Predicates.or(Predicates.not(new DropIterablePredicate<T>(this.count)), predicate));
}

@Override
public boolean noneSatisfy(Predicate<? super T> predicate)
{
return Iterate.noneSatisfy(this.adapted, Predicates.and(new DropIterablePredicate<T>(this.count), predicate));
}

@Override
public T detect(Predicate<? super T> predicate)
{
return Iterate.detect(this.adapted, Predicates.and(new DropIterablePredicate<T>(this.count), predicate));
}

public Iterator<T> iterator()
{
return new DropIterator<T>(this.adapted, this.count);
Expand Down
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2016 Goldman Sachs.
* 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.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections.test.lazy;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.lazy.DropIterable;
import org.eclipse.collections.test.IterableTestCase;
import org.eclipse.collections.test.LazyNoIteratorTestCase;
import org.eclipse.collections.test.list.mutable.FastListNoIterator;
import org.eclipse.junit.runners.Java8Runner;
import org.junit.runner.RunWith;

@RunWith(Java8Runner.class)
public class DropIterableTestNoIteratorTest implements LazyNoIteratorTestCase
{
@Override
public <T> LazyIterable<T> newWith(T... elements)
{
MutableList<T> list = new FastListNoIterator<>();
IterableTestCase.addAllTo(elements, list);
return new DropIterable<>(list, 0);
}
}

0 comments on commit 826a007

Please sign in to comment.