github.com/searKing/golang/go@v1.2.74/util/function/binary/binary_operator.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/util" 9 "github.com/searKing/golang/go/util/object" 10 ) 11 12 /** 13 * Returns a {@link BinaryOperator} which returns the lesser of two elements 14 * according to the specified {@code Comparator}. 15 * 16 * @param <T> the type of the input arguments of the comparator 17 * @param comparator a {@code Comparator} for comparing the two values 18 * @return a {@code BinaryOperator} which returns the lesser of its operands, 19 * according to the supplied {@code Comparator} 20 * @throws NullPointerException if the argument is null 21 */ 22 func MinBy(comparator util.Comparator) BiFunction { 23 object.RequireNonNil(comparator) 24 return BiFunctionFunc(func(t interface{}, u interface{}) interface{} { 25 c := comparator.Compare(t, u) 26 if c <= 0 { 27 return t 28 } 29 30 return u 31 }) 32 } 33 34 /** 35 * Returns a {@link BinaryOperator} which returns the greater of two elements 36 * according to the specified {@code Comparator}. 37 * 38 * @param <T> the type of the input arguments of the comparator 39 * @param comparator a {@code Comparator} for comparing the two values 40 * @return a {@code BinaryOperator} which returns the greater of its operands, 41 * according to the supplied {@code Comparator} 42 * @throws NullPointerException if the argument is null 43 */ 44 func MaxBy(comparator util.Comparator) BiFunction { 45 object.RequireNonNil(comparator) 46 return BiFunctionFunc(func(t interface{}, u interface{}) interface{} { 47 c := comparator.Compare(t, u) 48 if c >= 0 { 49 return t 50 } 51 52 return u 53 }) 54 }