com.metamx.common.guava.FunctionalIterator Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011,2012 Metamarkets Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metamx.common.guava;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterators;
import com.metamx.common.guava.nary.BinaryFn;
import com.metamx.common.guava.nary.BinaryTransformIterator;
import com.metamx.common.guava.nary.TrinaryFn;
import com.metamx.common.guava.nary.TrinaryTransformIterator;
import java.util.Iterator;
/**
*/
public class FunctionalIterator implements Iterator
{
private final Iterator delegate;
public static FunctionalIterator create(Iterator delegate)
{
return new FunctionalIterator<>(delegate);
}
public static FunctionalIterator fromConcatenation(Iterator... toConcat)
{
return new FunctionalIterator<>(Iterators.concat(toConcat));
}
public static FunctionalIterator fromConcatenation(Iterator> toConcat)
{
return new FunctionalIterator<>(Iterators.concat(toConcat));
}
public FunctionalIterator(
Iterator delegate
)
{
this.delegate = delegate;
}
public boolean hasNext()
{
return delegate.hasNext();
}
public T next()
{
return delegate.next();
}
public void remove()
{
delegate.remove();
}
public FunctionalIterator transform(Function fn)
{
return new FunctionalIterator<>(Iterators.transform(delegate, fn));
}
public FunctionalIterator transformCat(Function> fn)
{
return new FunctionalIterator<>(Iterators.concat(Iterators.transform(delegate, fn)));
}
public FunctionalIterator keep(Function fn)
{
return new FunctionalIterator<>(Iterators.filter(Iterators.transform(delegate, fn), Predicates.notNull()));
}
public FunctionalIterator filter(Predicate pred)
{
return new FunctionalIterator<>(Iterators.filter(delegate, pred));
}
public FunctionalIterator drop(int numToDrop)
{
return new FunctionalIterator<>(new DroppingIterator<>(delegate, numToDrop));
}
public FunctionalIterator limit(int limit)
{
return new FunctionalIterator<>(Iterators.limit(delegate, limit));
}
public FunctionalIterator concat(Iterator... toConcat)
{
if (toConcat.length == 1) {
return new FunctionalIterator<>(Iterators.concat(delegate, toConcat[0]));
}
return new FunctionalIterator<>(Iterators.concat(delegate, Iterators.concat(toConcat)));
}
public FunctionalIterator concat(Iterator> toConcat)
{
return new FunctionalIterator<>(Iterators.concat(delegate, Iterators.concat(toConcat)));
}
public FunctionalIterator binaryTransform(
final Iterator otherIterator, final BinaryFn binaryFn
)
{
return new FunctionalIterator<>(BinaryTransformIterator.create(delegate, otherIterator, binaryFn));
}
public FunctionalIterator trinaryTransform(
final Iterator iterator1,
final Iterator iterator2,
final TrinaryFn trinaryFn
)
{
return new FunctionalIterator<>(TrinaryTransformIterator.create(delegate, iterator1, iterator2, trinaryFn));
}
}