github.com/searKing/golang/go@v1.2.74/util/function/predicate/predicate.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 predicate
     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/object"
    11  )
    12  
    13  /**
    14   * Represents a predicate (boolean-valued function) of one argument.
    15   */
    16  type Predicater interface {
    17  	/**
    18  	 * Evaluates this predicate on the given argument.
    19  	 *
    20  	 * @param t the input argument
    21  	 * @return {@code true} if the input argument matches the predicate,
    22  	 * otherwise {@code false}
    23  	 */
    24  	Test(value interface{}) bool
    25  
    26  	/**
    27  	 * Returns a composed predicate that represents a short-circuiting logical
    28  	 * AND of this predicate and another.  When evaluating the composed
    29  	 * predicate, if this predicate is {@code false}, then the {@code other}
    30  	 * predicate is not evaluated.
    31  	 *
    32  	 * <p>Any exceptions thrown during evaluation of either predicate are relayed
    33  	 * to the caller; if evaluation of this predicate throws an exception, the
    34  	 * {@code other} predicate will not be evaluated.
    35  	 *
    36  	 * @param other a predicate that will be logically-ANDed with this
    37  	 *              predicate
    38  	 * @return a composed predicate that represents the short-circuiting logical
    39  	 * AND of this predicate and the {@code other} predicate
    40  	 * @throws NullPointerException if other is null
    41  	 */
    42  	And(other Predicater) Predicater
    43  
    44  	/**
    45  	 * Returns a predicate that represents the logical negation of this
    46  	 * predicate.
    47  	 *
    48  	 * @return a predicate that represents the logical negation of this
    49  	 * predicate
    50  	 */
    51  	Negate() Predicater
    52  
    53  	/**
    54  	 * Returns a composed predicate that represents a short-circuiting logical
    55  	 * OR of this predicate and another.  When evaluating the composed
    56  	 * predicate, if this predicate is {@code true}, then the {@code other}
    57  	 * predicate is not evaluated.
    58  	 *
    59  	 * <p>Any exceptions thrown during evaluation of either predicate are relayed
    60  	 * to the caller; if evaluation of this predicate throws an exception, the
    61  	 * {@code other} predicate will not be evaluated.
    62  	 *
    63  	 * @param other a predicate that will be logically-ORed with this
    64  	 *              predicate
    65  	 * @return a composed predicate that represents the short-circuiting logical
    66  	 * OR of this predicate and the {@code other} predicate
    67  	 * @throws NullPointerException if other is null
    68  	 */
    69  	Or(other Predicater) Predicater
    70  
    71  	/**
    72  	 * Returns a predicate that tests if two arguments are equal according
    73  	 * to {@link Objects#equals(Object, Object)}.
    74  	 *
    75  	 * @param <T> the type of arguments to the predicate
    76  	 * @param targetRef the object reference with which to compare for equality,
    77  	 *               which may be {@code null}
    78  	 * @return a predicate that tests if two arguments are equal according
    79  	 * to {@link Objects#equals(Object, Object)}
    80  	 */
    81  	IsEqual(targetRef interface{}) Predicater
    82  }
    83  
    84  type PredicaterFunc func(value interface{}) bool
    85  
    86  func (f PredicaterFunc) Test(value interface{}) bool {
    87  	return f(value)
    88  }
    89  
    90  func (f PredicaterFunc) And(other Predicater) Predicater {
    91  	object.RequireNonNil(other)
    92  	return PredicaterFunc(func(value interface{}) bool {
    93  		return f.Test(value) && other.Test(value)
    94  	})
    95  }
    96  
    97  func (f PredicaterFunc) Negate() Predicater {
    98  	return PredicaterFunc(func(value interface{}) bool {
    99  		return !f.Test(value)
   100  	})
   101  }
   102  
   103  func (f PredicaterFunc) Or(other Predicater) Predicater {
   104  	object.RequireNonNil(other)
   105  	return PredicaterFunc(func(value interface{}) bool {
   106  		return f.Test(value) || other.Test(value)
   107  	})
   108  }
   109  
   110  func (f PredicaterFunc) IsEqual(targetRef interface{}) Predicater {
   111  	if targetRef == nil {
   112  		return PredicaterFunc(object.IsNil)
   113  	}
   114  	return PredicaterFunc(func(value interface{}) bool {
   115  		return value == targetRef
   116  	})
   117  }
   118  
   119  type AbstractPredicaterClass struct {
   120  	class.Class
   121  }
   122  
   123  func (pred *AbstractPredicaterClass) Test(value interface{}) bool {
   124  	panic(exception.NewIllegalStateException1("called wrong Test method"))
   125  }
   126  
   127  func (pred *AbstractPredicaterClass) And(other Predicater) Predicater {
   128  	return PredicaterFunc(func(value interface{}) bool {
   129  		return pred.GetDerivedElse(pred).(Predicater).Test(value) && other.Test(value)
   130  	})
   131  }
   132  
   133  func (pred *AbstractPredicaterClass) Negate() Predicater {
   134  	return PredicaterFunc(func(value interface{}) bool {
   135  		return !pred.GetDerivedElse(pred).(Predicater).Test(value)
   136  	})
   137  }
   138  
   139  func (pred *AbstractPredicaterClass) Or(other Predicater) Predicater {
   140  	object.RequireNonNil(other)
   141  	return PredicaterFunc(func(value interface{}) bool {
   142  		return pred.GetDerivedElse(pred).(Predicater).Test(value) || other.Test(value)
   143  	})
   144  }
   145  
   146  func (pred *AbstractPredicaterClass) IsEqual(targetRef interface{}) Predicater {
   147  	if targetRef == nil {
   148  		return PredicaterFunc(object.IsNil)
   149  	}
   150  	return PredicaterFunc(func(value interface{}) bool {
   151  		return value == targetRef
   152  	})
   153  }