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 019 020import java.util.function.*; 021import static org.apache.juneau.commons.utils.Shorts.*; 022 023/** 024 * A functional interface representing a function that accepts two arguments and produces a result. 025 * 026 * <p> 027 * This interface extends the standard Java {@link Function} pattern to support 028 * two-argument functions. It's useful when you need to pass functions with two parameters to methods 029 * that expect functional interfaces, such as in stream operations, builders, or callback patterns. 030 * 031 * <h5 class='section'>Features:</h5> 032 * <ul class='spaced-list'> 033 * <li>Functional interface - can be used with lambda expressions and method references 034 * <li>Composition support - provides {@link #andThen(Function)} for function chaining 035 * <li>Type-safe - generic type parameters ensure compile-time type safety 036 * </ul> 037 * 038 * <h5 class='section'>Use Cases:</h5> 039 * <ul class='spaced-list'> 040 * <li>Two-argument transformations in functional programming patterns 041 * <li>Builder methods that accept two-parameter functions 042 * <li>Stream operations requiring two-argument functions 043 * <li>Callback patterns with two parameters 044 * </ul> 045 * 046 * <h5 class='section'>Usage:</h5> 047 * <p class='bjava'> 048 * <jc>// Lambda expression</jc> 049 * Function2<String,Integer,String> <jv>concat</jv> = (<jv>s</jv>, <jv>i</jv>) -> <jv>s</jv> + <js>"-"</js> + <jv>i</jv>; 050 * String <jv>result</jv> = <jv>concat</jv>.apply(<js>"prefix"</js>, 42); <jc>// Returns "prefix-42"</jc> 051 * 052 * <jc>// Method reference</jc> 053 * Function2<String,String,Boolean> <jv>equals</jv> = String::equals; 054 * <jk>boolean</jk> <jv>match</jv> = <jv>equals</jv>.apply(<js>"hello"</js>, <js>"hello"</js>); <jc>// Returns true</jc> 055 * 056 * <jc>// Function composition</jc> 057 * Function2<Integer,Integer,Integer> <jv>add</jv> = (<jv>a</jv>, <jv>b</jv>) -> <jv>a</jv> + <jv>b</jv>; 058 * Function2<Integer,Integer,String> <jv>addAndFormat</jv> = <jv>add</jv>.andThen(Object::toString); 059 * String <jv>sum</jv> = <jv>addAndFormat</jv>.apply(5, 3); <jc>// Returns "8"</jc> 060 * </p> 061 * 062 * <h5 class='section'>See Also:</h5><ul> 063 * <li class='jc'>{@link Function3} - Three-argument function 064 * <li class='jc'>{@link Function4} - Four-argument function 065 * <li class='jc'>{@link Function5} - Five-argument function 066 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauCommons">juneau-commons</a> 067 * </ul> 068 * 069 * @param <A> The type of the first argument to the function. 070 * @param <B> The type of the second argument to the function. 071 * @param <R> The type of the result of the function. 072 */ 073@FunctionalInterface 074@SuppressWarnings({ 075 "java:S1192" // Duplicated literals (argument/property names) read more clearly inline than as constants 076}) 077public interface Function2<A,B,R> { 078 079 /** 080 * Returns a composed function that first applies this function to its input, and then applies 081 * the {@code after} function to the result. 082 * 083 * <p> 084 * This method enables function composition, allowing you to chain multiple transformations together. 085 * 086 * <h5 class='section'>Example:</h5> 087 * <p class='bjava'> 088 * Function2<Integer,Integer,Integer> <jv>multiply</jv> = (<jv>a</jv>, <jv>b</jv>) -> <jv>a</jv> * <jv>b</jv>; 089 * Function2<Integer,Integer,String> <jv>multiplyAndFormat</jv> = <jv>multiply</jv>.andThen(<jv>n</jv> -> <js>"Result: "</js> + <jv>n</jv>); 090 * String <jv>result</jv> = <jv>multiplyAndFormat</jv>.apply(6, 7); <jc>// Returns "Result: 42"</jc> 091 * </p> 092 * 093 * @param <V> The type of output of the {@code after} function, and of the composed function. 094 * @param after The function to apply after this function is applied. Must not be <jk>null</jk>. 095 * @return A composed function that first applies this function and then applies the {@code after} function. 096 * @throws NullPointerException if {@code after} is <jk>null</jk>. 097 */ 098 default <V> Function2<A,B,V> andThen(Function<? super R,? extends V> after) { 099 reqnn("after", after); 100 return (A a, B b) -> after.apply(apply(a, b)); 101 } 102 103 /** 104 * Applies this function to the given arguments. 105 * 106 * <h5 class='section'>Example:</h5> 107 * <p class='bjava'> 108 * Function2<String,Integer,String> <jv>repeat</jv> = (<jv>s</jv>, <jv>n</jv>) -> <jv>s</jv>.repeat(<jv>n</jv>); 109 * String <jv>result</jv> = <jv>repeat</jv>.apply(<js>"ha"</js>, 3); <jc>// Returns "hahaha"</jc> 110 * </p> 111 * 112 * @param a The first function argument. 113 * @param b The second function argument. 114 * @return The function result. 115 */ 116 R apply(A a, B b); 117}