org.spongepowered.api.util.Functional Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spongeapi Show documentation
Show all versions of spongeapi Show documentation
A plugin API for Minecraft: Java Edition
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered
* Copyright (c) contributors
*
* 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 org.spongepowered.api.util;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
/**
* Utility methods to help with function work.
*/
public class Functional {
private Functional() {
}
/**
* Perform an AND using an array of predicates.
*
* @param predicates The predicates to AND
* @param The type to accept
* @return The combined predicate
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static Predicate predicateAnd(Predicate... predicates) {
return Functional.predicateAnd(Arrays.asList(predicates));
}
/**
* Perform an AND using an iterable of predicates.
*
* @param predicates The predicates to and
* @param The type to accept
* @return The combined predicate
*/
public static Predicate predicateAnd(Iterable> predicates) {
return e -> {
for (Predicate pred : predicates) {
if (!pred.test(e)) {
return false;
}
}
return true;
};
}
/**
* Perform an AND using an array of bi-predicates.
*
* @param predicates The bi-predicates to AND
* @param The left type to accept
* @param The right type to accept
* @return The combined bi-predicate
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static BiPredicate biPredicateAnd(BiPredicate... predicates) {
return Functional.biPredicateAnd(Arrays.asList(predicates));
}
/**
* Perform an AND using an iterable of bi-predicates.
*
* @param predicates The bi-predicates to and
* @param The left type to accept
* @param The right type to accept
* @return The combined bi-predicate
*/
public static BiPredicate biPredicateAnd(Iterable> predicates) {
return (l, r) -> {
for (BiPredicate pred : predicates) {
if (!pred.test(l, r)) {
return false;
}
}
return true;
};
}
/**
* Creates a new {@link Predicate} defining whether an {@link Object}
* is contained within the provided {@link Collection}.
*
* @param collection The collection
* @param The type of object
* @return The predicate
*/
public static Predicate predicateIn(Collection collection) {
return collection::contains;
}
/**
* Creates a {@link com.google.common.base.Predicate} based on the provided {@link Predicate}, used
* to transform between Java 8 specific code to those from the guava
* library.
*
* @param predicate The predicate
* @param The type of object
* @return The guava predicate
*/
public static com.google.common.base.Predicate java8ToGuava(Predicate predicate) {
return predicate::test;
}
/**
* Gets the value of an {@link Optional} as either a zero- or one-element immutable set.
*
* @param value The value to get as a set
* @param The type
* @return The immutable set containing any value the optional has
*/
public static Set optionalAsSet(Optional value) {
return value.isPresent() ? ImmutableSet.of(value.get()) : ImmutableSet.of();
}
/**
* Execute a callable on the current thread, capturing the result or any exceptions that may be thrown into a {@link
* CompletableFuture}.
*
* @param call The callable to execute
* @param The type of value returned
* @return The future holding the result
*/
public static CompletableFuture failableFuture(Callable call) {
final CompletableFuture ret = new CompletableFuture<>();
try {
ret.complete(call.call());
} catch (Throwable e) {
ret.completeExceptionally(e);
}
return ret;
}
/**
* Execute a callable on the provided executor, capturing the result or any exceptions that may be thrown into a {@link
* CompletableFuture}.
*
* @param call The callable to execute
* @param exec The executor to execute this task on
* @param The type of value returned
* @return The future holding the result
*/
public static CompletableFuture asyncFailableFuture(Callable call, Executor exec) {
final CompletableFuture ret = new CompletableFuture<>();
exec.execute(() -> {
try {
ret.complete(call.call());
} catch (Throwable e) {
ret.completeExceptionally(e);
}
});
return ret;
}
}