de.florianmichael.rclasses.functional.throwable.TFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functional Show documentation
Show all versions of functional Show documentation
Random collection of Java classes and utils in different submodules which together form a commons library
/*
* This file is part of RClasses - https://github.com/FlorianMichael/RClasses
* Copyright (C) 2023 FlorianMichael/EnZaXD 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.
*/
package de.florianmichael.rclasses.functional.throwable;
import java.util.Objects;
/**
* This is a functional interface that can be used to replace Function and throw exceptions.
*
* @param The argument type
* @param The return type
*/
@FunctionalInterface
public interface TFunction {
/**
* Applies this function to the given argument and throws an exception.
*
* @param t The argument
* @return The return value
* @throws Throwable The exception
*/
R apply(T t) throws Throwable;
/**
* @param before The operation to perform before this operation
* @param The argument type of the {@code before} function, and of the composed function
* @return A composed {@code TFunction} that performs in sequence the {@code before} operation followed by this operation
*/
default TFunction compose(TFunction super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* @param after The operation to perform after this operation
* @param The return type of the {@code after} function, and of the composed function
* @return A composed {@code TFunction} that performs in sequence this operation followed by the {@code after}
*/
default TFunction andThen(TFunction super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* @param The argument type
* @return A function that always returns its input argument.
*/
static TFunction identity() {
return t -> t;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy