All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.d2ab.sequence.EquivalentSizeSequence Maven / Gradle / Ivy

The newest version!
package org.d2ab.sequence;

import org.d2ab.collection.SizedIterable;

import java.util.Iterator;
import java.util.function.Function;

/**
 * A {@link Sequence} created from another sequence through a mapping function, which has the same size as the original
 * {@link Sequence}.
 */
public class EquivalentSizeSequence implements Sequence {
	private final SizedIterable original;
	private final Function, Iterator> converter;

	public EquivalentSizeSequence(SizedIterable original, Function, Iterator> converter) {
		this.original = original;
		this.converter = converter;
	}

	@Override
	public Iterator iterator() {
		return converter.apply(original.iterator());
	}

	@Override
	public int size() {
		return original.size();
	}

	@Override
	public SizeType sizeType() {
		return original.sizeType();
	}

	@Override
	public boolean isEmpty() {
		return original.isEmpty();
	}
}