All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.florianmichael.rclasses.functional.throwable.TFunction Maven / Gradle / Ivy

Go to download

Random collection of Java classes and utils in different submodules which together form a commons library

There is a newer version: 2.4.1
Show newest version
/*
 * 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 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 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