github.com/tursom/GoCollections@v0.3.10/lang/Comparable.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package lang
     8  
     9  type Comparable[T any] interface {
    10  	Compare(t T) int
    11  }
    12  
    13  func Compare[T any, E Comparable[T]](e E, t T) int {
    14  	//if e == nil {
    15  	//	panic(exceptions.NewNPE("LessThan compare from an null object", true))
    16  	//}
    17  
    18  	return e.Compare(t)
    19  }
    20  
    21  func LessThan[T any, E Comparable[T]](e E, t T) bool {
    22  	return e.Compare(t) < 0
    23  }
    24  
    25  func MoreThan[T any, E Comparable[T]](e E, t T) bool {
    26  	return e.Compare(t) > 0
    27  }
    28  
    29  func LessThanEqual[T any, E Comparable[T]](e E, t T) bool {
    30  	return e.Compare(t) <= 0
    31  }
    32  
    33  func MoreThanEqual[T any, E Comparable[T]](e E, t T) bool {
    34  	return e.Compare(t) >= 0
    35  }