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

io.datarouter.scanner.ScannerStream Maven / Gradle / Ivy

/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * 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 io.datarouter.scanner;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * Wrapper around a Scanner to allow closing it without explicitly closing the Stream.  Some operations do release
 * a native Stream that must still be closed if it isn't fully consumed.
 */
public class ScannerStream implements Stream{

	private final Scanner scanner;

	public ScannerStream(Scanner scanner){
		this.scanner = scanner;
	}

	/*-------------- private -----------------*/

	private Stream nativeStream(){
		return ScannerTool.nativeStream(scanner);
	}

	private  R closeAndReturn(R result){
		close();
		return result;
	}

	/*--------------- Stream ------------------*/

	@Override
	public Iterator iterator(){
		return scanner.iterator();
	}

	@Override
	public Spliterator spliterator(){
		return ScannerTool.spliterator(scanner);
	}

	@Override
	public boolean isParallel(){
		return false;
	}

	@Override
	public Stream sequential(){
		return this;
	}

	@Override
	public Stream parallel(){
		return this;
	}

	@Override
	public Stream unordered(){
		return this;
	}

	@Override
	public Stream onClose(Runnable closeHandler){
		return nativeStream().onClose(closeHandler);
	}

	@Override
	public void close(){
		scanner.close();
	}

	/*------- return native stream that must be closed ----------------*/

	@Override
	public IntStream mapToInt(ToIntFunction mapper){
		return nativeStream().mapToInt(mapper);
	}

	@Override
	public LongStream mapToLong(ToLongFunction mapper){
		return nativeStream().mapToLong(mapper);
	}

	@Override
	public DoubleStream mapToDouble(ToDoubleFunction mapper){
		return nativeStream().mapToDouble(mapper);
	}

	/*-------------- chain to another ScannerStream ---------------*/

	@Override
	public Stream distinct(){
		return scanner.distinct().stream();
	}

	@Override
	public Stream filter(Predicate predicate){
		return scanner.include(predicate).stream();
	}

	@Override
	public  Stream map(Function mapper){
		Scanner mappedScanner = scanner.map(mapper);
		return mappedScanner.stream();
	}

	@Override
	public Stream peek(Consumer action){
		return scanner.each(action).stream();
	}

	@Override
	public Stream limit(long limit){
		return scanner.limit(limit).stream();
	}

	@Override
	public Stream skip(long numToSkip){
		return scanner.skip(numToSkip).stream();
	}

	@Override
	public Stream sorted(){
		return scanner.sort().stream();
	}

	@Override
	public Stream sorted(Comparator comparator){
		return scanner.sort(comparator).stream();
	}

	/*----------- terminate using Scanner ------------*/

	@Override
	public boolean anyMatch(Predicate predicate){
		return scanner.anyMatch(predicate);
	}

	@Override
	public boolean allMatch(Predicate predicate){
		return scanner.allMatch(predicate);
	}

	@Override
	public long count(){
		return scanner.count();
	}

	@Override
	public Optional findAny(){
		return scanner.findFirst();
	}

	@Override
	public Optional findFirst(){
		return scanner.findFirst();
	}

	@Override
	public void forEach(Consumer action){
		scanner.forEach(action);
	}

	@Override
	public void forEachOrdered(Consumer action){
		scanner.forEach(action);
	}

	@Override
	public Optional max(Comparator comparator){
		return scanner.findMax(comparator);
	}

	@Override
	public Optional min(Comparator comparator){
		return scanner.findMin(comparator);
	}

	@Override
	public boolean noneMatch(Predicate predicate){
		return scanner.noneMatch(predicate);
	}

	@Override
	public Object[] toArray(){
		return scanner.toArray();
	}

	/*-------------- closeAndReturn -----------------*/

	@Override
	public  A[] toArray(IntFunction generator){
		return closeAndReturn(nativeStream().toArray(generator));
	}

	@Override
	public T reduce(T identity, BinaryOperator accumulator){
		return closeAndReturn(nativeStream().reduce(identity, accumulator));
	}

	@Override
	public Optional reduce(BinaryOperator accumulator){
		return closeAndReturn(nativeStream().reduce(accumulator));
	}

	@Override
	public  U reduce(U identity, BiFunction accumulator, BinaryOperator combiner){
		return closeAndReturn(nativeStream().reduce(identity, accumulator, combiner));
	}

	@Override
	public  R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner){
		return closeAndReturn(nativeStream().collect(supplier, accumulator, combiner));
	}

	@Override
	public  R collect(Collector collector){
		return closeAndReturn(nativeStream().collect(collector));
	}

	/*-------------- flatMap auto-closes the scanner -----------*/

	@Override
	public  Stream flatMap(Function> mapper){
		return nativeStream().flatMap(mapper);
	}

	@Override
	public IntStream flatMapToInt(Function mapper){
		return nativeStream().flatMapToInt(mapper);
	}

	@Override
	public LongStream flatMapToLong(Function mapper){
		return nativeStream().flatMapToLong(mapper);
	}

	@Override
	public DoubleStream flatMapToDouble(Function mapper){
		return nativeStream().flatMapToDouble(mapper);
	}

}