mirror of
https://github.com/tursom/GoCollections.git
synced 2025-01-31 11:00:11 +08:00
30 lines
588 B
Go
30 lines
588 B
Go
package lang
|
|
|
|
type Comparable[T any] interface {
|
|
Compare(t T) int
|
|
}
|
|
|
|
func Compare[T any, E Comparable[T]](e E, t T) int {
|
|
//if e == nil {
|
|
// panic(exceptions.NewNPE("LessThan compare from an null object", true))
|
|
//}
|
|
|
|
return e.Compare(t)
|
|
}
|
|
|
|
func LessThan[T any, E Comparable[T]](e E, t T) bool {
|
|
return e.Compare(t) < 0
|
|
}
|
|
|
|
func MoreThan[T any, E Comparable[T]](e E, t T) bool {
|
|
return e.Compare(t) > 0
|
|
}
|
|
|
|
func LessThanEqual[T any, E Comparable[T]](e E, t T) bool {
|
|
return e.Compare(t) <= 0
|
|
}
|
|
|
|
func MoreThanEqual[T any, E Comparable[T]](e E, t T) bool {
|
|
return e.Compare(t) >= 0
|
|
}
|