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

org.rapidoid.fluent.Flow Maven / Gradle / Ivy

The newest version!
package org.rapidoid.fluent;

/*
 * #%L
 * rapidoid-fluent
 * %%
 * Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
 * %%
 * 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.
 * #L%
 */

import org.rapidoid.fluent.flow.FlowImpl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * A Flow is a {@link Stream} decorator with extra operations for convenience.
 *
 * @author Nikolche Mihajlovski
 * @since 5.0.0
 */
public interface Flow extends Stream {

	@SafeVarargs
	@SuppressWarnings("unchecked")
	static  FlowImpl of(T... values) {
		return new FlowImpl(Do.streamOf(values));
	}

	static  FlowImpl of(Iterable values) {
		return new FlowImpl(Do.stream(values));
	}

	static  FlowImpl of(Stream values) {
		return new FlowImpl(Do.stream(values));
	}

	static FlowImpl range(long startInclusive, long endExclusive) {
		return new FlowImpl(LongStream.range(startInclusive, endExclusive).boxed());
	}

	static FlowImpl count(long startInclusive, long endInclusive) {
		return new FlowImpl(LongStream.rangeClosed(startInclusive, endInclusive).boxed());
	}

	static FlowImpl chars(char startInclusive, char endInclusive) {
		return count(startInclusive, endInclusive).map(n -> (char) n.intValue());
	}

	/**
	 * Returns the wrapped stream.
	 */
	Stream stream();

	/**
	 * Equivalent to collect(Collectors.toList()).
	 */
	List toList();

	/**
	 * Equivalent to collect(Collectors.toSet()).
	 */
	Set toSet();

	/**
	 * Equivalent to collect(Collectors.toMap(keyTransformation, valueTransformation)).
	 */
	 Map toMap(Function keyTransformation, Function valueTransformation);

	/**
	 * Equivalent to stream.filter(x -> transformation.apply(x) != null)).
	 */
	 Flow withNonNull(Function transformation);

	/**
	 * Equivalent to filter(x -> transformation.apply(x) == null).
	 */
	 Flow withNull(Function transformation);

	/**
	 * Equivalent to collect(Collectors.groupingBy(transformation)).
	 */
	 Map> groupBy(Function transformation);

	/**
	 * Reverses the order of the elements in the flow.
	 */
	Flow reverse();

	/**
	 * Returns an {@link Optional} describing the last element of this stream, or an empty {@code Optional} if the
	 * stream is empty. If the stream has no encounter order, then any element may be returned.
	 */
	Optional findLast();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy