github.com/searKing/golang/go@v1.2.74/util/function/binary/bi_function.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package binary
     6  
     7  import (
     8  	"github.com/searKing/golang/go/error/exception"
     9  	"github.com/searKing/golang/go/util/class"
    10  	"github.com/searKing/golang/go/util/function"
    11  	"github.com/searKing/golang/go/util/object"
    12  )
    13  
    14  /**
    15   * Represents a function that accepts two arguments and produces a result.
    16   * This is the two-arity specialization of {@link Function}.
    17   *
    18   * <p>This is a <a href="package-summary.html">functional interface</a>
    19   * whose functional method is {@link #compare(Object, Object)}.
    20   *
    21   * @param <T> the type of the first argument to the function
    22   * @param <U> the type of the second argument to the function
    23   * @param <R> the type of the result of the function
    24   *
    25   * @see Function
    26   * @since 1.8
    27   */
    28  type BiFunction interface {
    29  	/**
    30  	 * Applies this function to the given arguments.
    31  	 *
    32  	 * @param t the first function argument
    33  	 * @param u the second function argument
    34  	 * @return the function result
    35  	 */
    36  	Apply(t interface{}, u interface{}) interface{}
    37  
    38  	/**
    39  	 * Returns a composed function that first applies this function to
    40  	 * its input, and then applies the {@code after} function to the result.
    41  	 * If evaluation of either function throws an exception, it is relayed to
    42  	 * the caller of the composed function.
    43  	 *
    44  	 * @param <V> the type of output of the {@code after} function, and of the
    45  	 *           composed function
    46  	 * @param after the function to compare after this function is applied
    47  	 * @return a composed function that first applies this function and then
    48  	 * applies the {@code after} function
    49  	 * @throws NullPointerException if after is null
    50  	 */
    51  	AndThen(after function.Function) BiFunction
    52  }
    53  
    54  type BiFunctionFunc func(t interface{}, u interface{}) interface{}
    55  
    56  func (f BiFunctionFunc) Apply(t interface{}, u interface{}) interface{} {
    57  	return f(t, u)
    58  }
    59  
    60  func (f BiFunctionFunc) AndThen(after function.Function) BiFunction {
    61  	object.RequireNonNil(after)
    62  	return BiFunctionFunc(func(t interface{}, u interface{}) interface{} {
    63  		return after.Apply(f.Apply(t, u))
    64  	})
    65  }
    66  
    67  type AbstractBiFunctionClass struct {
    68  	class.Class
    69  }
    70  
    71  func (f *AbstractBiFunctionClass) Apply(t interface{}, u interface{}) interface{} {
    72  	panic(exception.NewIllegalStateException1("called wrong Apply method"))
    73  }
    74  
    75  func (f *AbstractBiFunctionClass) AndThen(after function.Function) BiFunction {
    76  	object.RequireNonNil(after)
    77  	return BiFunctionFunc(func(t interface{}, u interface{}) interface{} {
    78  		return after.Apply(f.GetDerivedElse(f).(BiFunction).Apply(t, u))
    79  	})
    80  }