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 ninja.leaping.configurate.util;
018
019import org.checkerframework.checker.nullness.qual.NonNull;
020
021import java.util.function.Function;
022
023import static java.util.Objects.requireNonNull;
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 */
032@FunctionalInterface
033public interface CheckedFunction<I, O, E extends Exception> {
034    /**
035     * Perform the action.
036     *
037     * @param one parameter
038     * @return return value
039     * @throws E Thrown when defined by types accepting this function
040     */
041    O apply(I one) throws E;
042
043    /**
044     * Convert a JDK {@link Function} into its checked variant
045     *
046     * @param func The function
047     * @param <I> Parameter type
048     * @param <O> return type
049     * @return The function as a checked function
050     */
051    static <I, O> CheckedFunction<I, O, RuntimeException> fromFunction(Function<I, @NonNull O> func) {
052        return requireNonNull(func, "func")::apply;
053    }
054}