[New] Added new checked and throwing functional interfaces
This commit is contained in:
parent
d716d697b1
commit
ed42640e42
|
@ -0,0 +1,6 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CheckedConsumer<T> {
|
||||
void accept(T t) throws Exception;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CheckedFunction<T, R> {
|
||||
R apply(T t) throws Exception;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CheckedPredicate {
|
||||
boolean test() throws Exception;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CheckedSupplier<T> {
|
||||
T get() throws Exception;
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
package li.strolch.utils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a predicate (boolean-valued function) of one argument.
|
||||
*
|
||||
* <p>This is a <a href="package-summary.html">functional interface</a>
|
||||
* whose functional method is {@link #test(Object)}.
|
||||
*
|
||||
* @param <T> the type of the input to the predicate
|
||||
*
|
||||
* @since 1.8
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Predicate<T> {
|
||||
|
||||
/**
|
||||
* Evaluates this predicate on the given argument.
|
||||
*
|
||||
* @param t the input argument
|
||||
* @return {@code true} if the input argument matches the predicate,
|
||||
* otherwise {@code false}
|
||||
*/
|
||||
boolean test(T t);
|
||||
|
||||
/**
|
||||
* Returns a composed predicate that represents a short-circuiting logical
|
||||
* AND of this predicate and another. When evaluating the composed
|
||||
* predicate, if this predicate is {@code false}, then the {@code other}
|
||||
* predicate is not evaluated.
|
||||
*
|
||||
* <p>Any exceptions thrown during evaluation of either predicate are relayed
|
||||
* to the caller; if evaluation of this predicate throws an exception, the
|
||||
* {@code other} predicate will not be evaluated.
|
||||
*
|
||||
* @param other a predicate that will be logically-ANDed with this
|
||||
* predicate
|
||||
* @return a composed predicate that represents the short-circuiting logical
|
||||
* AND of this predicate and the {@code other} predicate
|
||||
* @throws NullPointerException if other is null
|
||||
*/
|
||||
default Predicate<T> and(Predicate<? super T> other) {
|
||||
Objects.requireNonNull(other);
|
||||
return (t) -> test(t) && other.test(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a predicate that represents the logical negation of this
|
||||
* predicate.
|
||||
*
|
||||
* @return a predicate that represents the logical negation of this
|
||||
* predicate
|
||||
*/
|
||||
default Predicate<T> negate() {
|
||||
return (t) -> !test(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a composed predicate that represents a short-circuiting logical
|
||||
* OR of this predicate and another. When evaluating the composed
|
||||
* predicate, if this predicate is {@code true}, then the {@code other}
|
||||
* predicate is not evaluated.
|
||||
*
|
||||
* <p>Any exceptions thrown during evaluation of either predicate are relayed
|
||||
* to the caller; if evaluation of this predicate throws an exception, the
|
||||
* {@code other} predicate will not be evaluated.
|
||||
*
|
||||
* @param other a predicate that will be logically-ORed with this
|
||||
* predicate
|
||||
* @return a composed predicate that represents the short-circuiting logical
|
||||
* OR of this predicate and the {@code other} predicate
|
||||
* @throws NullPointerException if other is null
|
||||
*/
|
||||
default Predicate<T> or(Predicate<? super T> other) {
|
||||
Objects.requireNonNull(other);
|
||||
return (t) -> test(t) || other.test(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a predicate that tests if two arguments are equal according
|
||||
* to {@link Objects#equals(Object, Object)}.
|
||||
*
|
||||
* @param <T> the type of arguments to the predicate
|
||||
* @param targetRef the object reference with which to compare for equality,
|
||||
* which may be {@code null}
|
||||
* @return a predicate that tests if two arguments are equal according
|
||||
* to {@link Objects#equals(Object, Object)}
|
||||
*/
|
||||
static <T> Predicate<T> isEqual(Object targetRef) {
|
||||
return (null == targetRef)
|
||||
? Objects::isNull
|
||||
: object -> targetRef.equals(object);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingConsumer<T> extends Consumer<T> {
|
||||
|
||||
@Override
|
||||
default void accept(final T elem) {
|
||||
try {
|
||||
acceptThrows(elem);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void acceptThrows(T elem) throws Exception;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingFunction<T, R> extends Function<T, R> {
|
||||
|
||||
@Override
|
||||
default R apply(T t) {
|
||||
try {
|
||||
return applyThrows(t);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
R applyThrows(T t) throws Exception;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingPredicate<T> extends Predicate<T> {
|
||||
|
||||
@Override
|
||||
default boolean test(T t) {
|
||||
try {
|
||||
return testThrows(t);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
boolean testThrows(T t) throws Exception;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingSupplier<T> extends Supplier<T> {
|
||||
|
||||
@Override
|
||||
default T get() {
|
||||
try {
|
||||
return getThrows();
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
T getThrows() throws Exception;
|
||||
}
|
Loading…
Reference in New Issue