001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.spongepowered.configurate.util;
018
019import static java.util.Objects.requireNonNull;
020
021import org.checkerframework.checker.nullness.qual.NonNull;
022
023import java.util.function.Function;
024
025/**
026 * A function with one input and one output which may throw a checked exception.
027 *
028 * @param <I> the input parameter type
029 * @param <O> the output parameter type
030 * @param <E> the type thrown
031 * @since 4.0.0
032 */
033@FunctionalInterface
034public interface CheckedFunction<I, O, E extends Exception> {
035
036    /**
037     * Perform the action.
038     *
039     * @param one parameter
040     * @return return value
041     * @throws E thrown when defined by types accepting this function
042     * @since 4.0.0
043     */
044    O apply(I one) throws E;
045
046    /**
047     * Convert a JDK {@link Function} into its checked variant.
048     *
049     * @param func the function
050     * @param <I> parameter type
051     * @param <O> return type
052     * @return the function as a checked function
053     * @since 4.0.0
054     */
055    static <I, O> CheckedFunction<I, O, RuntimeException> from(Function<I, @NonNull O> func) {
056        return requireNonNull(func, "func")::apply;
057    }
058
059}