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

aQute.bnd.stream.DropWhile Maven / Gradle / Ivy

The newest version!
package aQute.bnd.stream;

import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public final class DropWhile extends AbstractWhile {
	public static  Stream dropWhile(Stream stream, Predicate predicate) {
		return StreamSupport.stream(new DropWhile<>(stream.spliterator(), predicate), stream.isParallel())
			.onClose(stream::close);
	}

	private boolean drop = true;

	private DropWhile(Spliterator spliterator, Predicate predicate) {
		super(spliterator, predicate);
	}

	@Override
	public void forEachRemaining(Consumer action) {
		if (drop) {
			drop = false;
			while (spliterator.tryAdvance(this)) {
				if (!predicate.test(item)) {
					action.accept(item);
					break;
				}
			}
		}
		spliterator.forEachRemaining(action);
	}

	@Override
	public boolean tryAdvance(Consumer action) {
		if (drop) {
			drop = false;
			while (spliterator.tryAdvance(this)) {
				if (!predicate.test(item)) {
					action.accept(item);
					return true;
				}
			}
			return false;
		}
		return spliterator.tryAdvance(action);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy