001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import static org.apache.juneau.common.internal.ThrowableUtils.*;
016
017import java.util.function.*;
018
019/**
020 * A subclass of {@link Function} that allows for thrown exceptions.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 * </ul>
024 *
025 * @param <T> the type of the input to the function.
026 * @param <R> the type of the result of the function.
027 */
028@FunctionalInterface
029public interface ThrowingFunction<T,R> extends Function<T,R> {
030
031   @Override
032   default R apply(T t) {
033      try {
034         return applyThrows(t);
035      } catch (Exception e) {
036         throw asRuntimeException(e);
037      }
038   }
039
040   /**
041    * The functional method to implement.
042    *
043    * @param t The type of the input to the function.
044    * @return The type of the result of the function.
045    * @throws Exception Any exception.
046    */
047   R applyThrows(T t) throws Exception;
048}