functionalj.stream.AsStreamPlusWithMatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.stream;
import static functionalj.stream.StreamPlus.streamOf;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import functionalj.function.aggregator.Aggregation;
import functionalj.function.aggregator.AggregationToBoolean;
import functionalj.result.Result;
import functionalj.stream.markers.Sequential;
import functionalj.stream.markers.Terminal;
import lombok.val;
public interface AsStreamPlusWithMatch {
public StreamPlus streamPlus();
//-- Match --
@Terminal
public default boolean anyMatch(Predicate super DATA> predicate) {
val streamPlus = streamPlus();
return streamPlus
.anyMatch(predicate);
}
@Terminal
public default boolean anyMatch(AggregationToBoolean super DATA> aggregation) {
val aggregator = aggregation.newAggregator();
return anyMatch(aggregator::test);
}
@Terminal
public default boolean allMatch(Predicate super DATA> predicate) {
val streamPlus = streamPlus();
return streamPlus
.allMatch(predicate);
}
@Terminal
public default boolean allMatch(AggregationToBoolean super DATA> aggregation) {
val aggregator = aggregation.newAggregator();
return allMatch(aggregator::test);
}
@Terminal
public default boolean noneMatch(Predicate super DATA> predicate) {
val streamPlus = streamPlus();
return streamPlus
.noneMatch(predicate);
}
@Terminal
public default boolean noneMatch(AggregationToBoolean super DATA> aggregation) {
val aggregator = aggregation.newAggregator();
return noneMatch(aggregator::test);
}
@Terminal
public default Optional findFirst() {
val streamPlus = streamPlus();
return streamPlus
.findFirst();
}
@Terminal
public default Optional findAny() {
val streamPlus = streamPlus();
return streamPlus
.findAny();
}
@Sequential
@Terminal
public default Optional findLast() {
val streamPlus = streamPlus();
return streamPlus
.findLast();
}
@Sequential
@Terminal
public default Result firstResult() {
val streamPlus = streamPlus();
return streamPlus
.firstResult();
}
@Sequential
@Terminal
public default Result lastResult() {
val streamPlus = streamPlus();
return streamPlus
.lastResult();
}
/** Return the first element that matches the predicate. */
@Terminal
@Sequential
public default Optional findFirst(
Predicate super DATA> predicate) {
val streamPlus = streamPlus();
return streamPlus
.filter(predicate)
.findFirst();
}
/** Return the first element that matches the predicate. */
@Terminal
@Sequential
public default Optional findFirst(
AggregationToBoolean super DATA> aggregation) {
val aggregator = aggregation.newAggregator();
return findFirst(aggregator::test);
}
/** Return the any element that matches the predicate. */
@Terminal
public default Optional findAny(
Predicate super DATA> predicate) {
val streamPlus = streamPlus();
return streamPlus
.filter(predicate)
.findAny();
}
/** Return the any element that matches the predicate. */
@Terminal
public default Optional findAny(
AggregationToBoolean super DATA> aggregation) {
val aggregator = aggregation.newAggregator();
return findFirst(aggregator::test);
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findFirst(
Function super DATA, T> mapper,
Predicate super T> theCondition) {
val streamPlus = streamPlus();
return streamPlus
.filter(mapper, theCondition)
.findFirst();
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findFirst(
Aggregation super DATA, T> aggregation,
Predicate super T> theCondition) {
val mapper = aggregation.newAggregator();
return findFirst(mapper, theCondition);
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findFirst(
Function super DATA, T> mapper,
AggregationToBoolean super T> theConditionAggregation) {
val theCondition = theConditionAggregation.newAggregator();
return findFirst(mapper, theCondition::test);
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findFirst(
Aggregation super DATA, T> aggregation,
AggregationToBoolean super T> theConditionAggregation) {
val mapper = aggregation.newAggregator();
val theCondition = theConditionAggregation.newAggregator();
return findFirst(mapper, theCondition::test);
}
/** Use the mapper, return the any element that its mapped value matches the predicate. */
@Terminal
public default Optional findAny(
Function super DATA, T> mapper,
Predicate super T> theCondition) {
val streamPlus = streamPlus();
return streamPlus
.filter(mapper, theCondition)
.findAny();
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findAny(
Aggregation super DATA, T> aggregation,
Predicate super T> theCondition) {
val mapper = aggregation.newAggregator();
return findAny(mapper, theCondition);
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findAny(
Function super DATA, T> mapper,
AggregationToBoolean super T> theConditionAggregation) {
val theCondition = theConditionAggregation.newAggregator();
return findAny(mapper, theCondition::test);
}
/** Use the mapper, return the first element that its mapped value matches the predicate. */
@Terminal
@Sequential
public default Optional findAny(
Aggregation super DATA, T> aggregation,
AggregationToBoolean super T> theConditionAggregation) {
val mapper = aggregation.newAggregator();
val theCondition = theConditionAggregation.newAggregator();
return findAny(mapper, theCondition::test);
}
//== Contains ==
/** Check if the list contains all the given values */
@SuppressWarnings("unchecked")
public default boolean containsAllOf(DATA ... values) {
val set = new HashSet(values.length);
for (val value : values) {
set.add(value);
}
val streamPlus = streamPlus();
return streamPlus
.peek(set::remove)
.anyMatch(__ -> set.isEmpty());
}
@SuppressWarnings("unchecked")
public default boolean containsAnyOf(DATA ... values) {
return anyMatch(each -> streamOf(values).anyMatch(o -> Objects.equals(each, o)));
}
@SuppressWarnings("unchecked")
public default boolean containsNoneOf(DATA ... values) {
return noneMatch(each -> streamOf(values).anyMatch(o -> Objects.equals(each, o)));
}
}