
pl.touk.throwing.ThrowingFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of throwing-function Show documentation
Show all versions of throwing-function Show documentation
Java 8+ functional interfaces with checked exceptions support
/*
* Copyright 2016 the original author or authors.
*
* 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 pl.touk.throwing;
import pl.touk.throwing.exception.WrappedException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
/**
* Represents a function that accepts one argument and returns a value;
* Function might throw a checked exception instance.
*
* @param the type of the input to the function
* @param the type of the result of the function
* @param the type of the thrown checked exception
*
*/
@FunctionalInterface
public interface ThrowingFunction {
R apply(T arg) throws E;
/**
* @param type
* @param checked exception
* @return a function that accepts one argument and returns it as a value.
*/
static ThrowingFunction identity() {
return t -> t;
}
/**
* @return a Function that returns the result of the given function as an Optional instance.
* In case of a failure, empty Optional is returned
*/
static Function> lifted(ThrowingFunction f) {
Objects.requireNonNull(f);
return f.lift();
}
static Function unchecked(ThrowingFunction f) {
Objects.requireNonNull(f);
return f.uncheck();
}
default ThrowingFunction compose(final ThrowingFunction super V, ? extends T, E> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default ThrowingFunction andThen(final ThrowingFunction super R, ? extends V, E> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* @return a Function that returns the result as an Optional instance. In case of a failure, empty Optional is
* returned
*/
default Function> lift() {
return t -> {
try {
return Optional.of(apply(t));
} catch (Throwable e) {
return Optional.empty();
}
};
}
/**
* @return a new Function instance which wraps thrown checked exception instance into a RuntimeException
*/
default Function uncheck() {
return t -> {
try {
return apply(t);
} catch (final Throwable e) {
throw new WrappedException(e);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy