001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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.apache.juneau.commons.function;
018
019import static org.apache.juneau.commons.utils.AssertionUtils.*;
020import static org.apache.juneau.commons.utils.ThrowableUtils.*;
021
022import java.util.function.*;
023
024/**
025 * A functional interface representing a function that accepts two arguments, produces a result, and may throw a checked exception.
026 *
027 * <p>
028 * This interface extends {@link Function2} to allow the functional method
029 * to throw checked exceptions. The default {@link #apply(Object, Object)} method wraps any checked exceptions in a
030 * {@link RuntimeException}, making it compatible with standard {@link Function2} usage while still allowing
031 * the implementation to throw checked exceptions via {@link #applyThrows(Object, Object)}.
032 *
033 * <h5 class='section'>Features:</h5>
034 * <ul class='spaced-list'>
035 *    <li>Functional interface - can be used with lambda expressions and method references
036 *    <li>Exception support - allows checked exceptions to be thrown via {@link #applyThrows(Object, Object)}
037 *    <li>Compatible with Function2 - implements {@link Function2#apply(Object, Object)} by wrapping exceptions
038 *    <li>Dual interface - can be used as both Function2 and ThrowingFunction2
039 * </ul>
040 *
041 * <h5 class='section'>Use Cases:</h5>
042 * <ul class='spaced-list'>
043 *    <li>Two-argument transformations that may throw I/O exceptions
044 *    <li>Parsing operations that may throw validation exceptions
045 *    <li>Data conversion that may fail with checked exceptions
046 *    <li>Operations that need to propagate checked exceptions
047 * </ul>
048 *
049 * <h5 class='section'>Usage:</h5>
050 * <p class='bjava'>
051 *    <jc>// Lambda with exception</jc>
052 *    ThrowingFunction2&lt;String,String,File&gt; <jv>fileParser</jv> = (<jv>dir</jv>, <jv>name</jv>) -&gt; {
053 *       Path <jv>path</jv> = Paths.get(<jv>dir</jv>, <jv>name</jv>);
054 *       <jk>if</jk> (! Files.exists(<jv>path</jv>)) {
055 *          <jk>throw new</jk> FileNotFoundException(<js>"File not found: "</js> + <jv>path</jv>);
056 *       }
057 *       <jk>return</jk> <jv>path</jv>.toFile();
058 *    };
059 *
060 *    <jc>// Using applyThrows to get checked exception</jc>
061 *    <jk>try</jk> {
062 *       File <jv>file</jv> = <jv>fileParser</jv>.applyThrows(<js>"/tmp"</js>, <js>"data.txt"</js>);
063 *    } <jk>catch</jk> (FileNotFoundException <jv>e</jv>) {
064 *       <jc>// Handle checked exception</jc>
065 *    }
066 *
067 *    <jc>// Using apply wraps exception in RuntimeException</jc>
068 *    File <jv>file</jv> = <jv>fileParser</jv>.apply(<js>"/tmp"</js>, <js>"data.txt"</js>);  <jc>// May throw RuntimeException</jc>
069 * </p>
070 *
071 * <h5 class='section'>Exception Handling:</h5>
072 * <ul class='spaced-list'>
073 *    <li>{@link #applyThrows(Object, Object)} - Throws checked exceptions directly
074 *    <li>{@link #apply(Object, Object)} - Wraps checked exceptions in {@link RuntimeException}
075 *    <li>Use {@code applyThrows} when you need to handle specific checked exceptions
076 *    <li>Use {@code apply} when you want standard Function2 behavior
077 * </ul>
078 *
079 * <h5 class='section'>See Also:</h5><ul>
080 *    <li class='jc'>{@link ThrowingFunction} - Single-argument function that throws exceptions
081 *    <li class='jc'>{@link ThrowingFunction3} - Three-argument function that throws exceptions
082 *    <li class='jc'>{@link ThrowingFunction4} - Four-argument function that throws exceptions
083 *    <li class='jc'>{@link ThrowingFunction5} - Five-argument function that throws exceptions
084 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauCommonsBasics">juneau-commons Basics</a>
085 * </ul>
086 *
087 * @param <A> The type of the first argument to the function.
088 * @param <B> The type of the second argument to the function.
089 * @param <R> The type of the result of the function.
090 */
091@FunctionalInterface
092public interface ThrowingFunction2<A,B,R> extends Function2<A,B,R> {
093
094   /**
095    * Applies this function to the given arguments, wrapping any checked exceptions in a {@link RuntimeException}.
096    *
097    * <p>
098    * This is the default implementation that makes this interface compatible with {@link Function2}.
099    * It calls {@link #applyThrows(Object, Object)} and wraps any checked exceptions.
100    *
101    * @param a The first function argument.
102    * @param b The second function argument.
103    * @return The function result.
104    * @throws RuntimeException if {@link #applyThrows(Object, Object)} throws a checked exception.
105    */
106   @Override
107   default R apply(A a, B b) {
108      try {
109         return applyThrows(a, b);
110      } catch (Exception e) {
111         throw toRex(e);
112      }
113   }
114
115   /**
116    * Returns a composed function that first applies this function to its input, and then applies
117    * the {@code after} function to the result.
118    *
119    * <p>
120    * This method enables function composition, allowing you to chain multiple transformations together.
121    * The composed function will throw exceptions from this function, but the {@code after} function
122    * is a standard {@link Function} that does not throw checked exceptions.
123    *
124    * @param <V> The type of output of the {@code after} function, and of the composed function.
125    * @param after The function to apply after this function is applied. Must not be <jk>null</jk>.
126    * @return A composed {@link ThrowingFunction2} that first applies this function and then applies the {@code after} function.
127    * @throws NullPointerException if {@code after} is <jk>null</jk>.
128    */
129   default <V> ThrowingFunction2<A,B,V> andThen(Function<? super R,? extends V> after) {
130      assertArgNotNull("after", after);
131      return (A a, B b) -> after.apply(applyThrows(a, b));
132   }
133
134   /**
135    * Applies this function to the given arguments, potentially throwing a checked exception.
136    *
137    * <p>
138    * This is the functional method that implementations must provide. It allows checked exceptions
139    * to be thrown directly, unlike the standard {@link Function2#apply(Object, Object)} method.
140    *
141    * <h5 class='section'>Example:</h5>
142    * <p class='bjava'>
143    *    ThrowingFunction2&lt;String,Integer,Integer&gt; <jv>parser</jv> = (<jv>s</jv>, <jv>base</jv>) -&gt; {
144    *       <jk>try</jk> {
145    *          <jk>return</jk> Integer.parseInt(<jv>s</jv>, <jv>base</jv>);
146    *       } <jk>catch</jk> (NumberFormatException <jv>e</jv>) {
147    *          <jk>throw new</jk> ParseException(<js>"Invalid number: "</js> + <jv>s</jv> + <js>" in base "</js> + <jv>base</jv>, 0);
148    *       }
149    *    };
150    *
151    *    <jk>try</jk> {
152    *       <jk>int</jk> <jv>value</jv> = <jv>parser</jv>.applyThrows(<js>"123"</js>, 10);
153    *    } <jk>catch</jk> (ParseException <jv>e</jv>) {
154    *       <jc>// Handle checked exception</jc>
155    *    }
156    * </p>
157    *
158    * @param a The first function argument.
159    * @param b The second function argument.
160    * @return The function result.
161    * @throws Exception If an error occurs during function execution.
162    */
163   R applyThrows(A a, B b) throws Exception;
164}
165