github.com/searKing/golang/go@v1.2.74/util/object/compare.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 object 6 7 import "reflect" 8 9 type Comparator interface { 10 Compare(a, b interface{}) int 11 } 12 13 // Equals Returns {@code true} if the arguments are equal to each other 14 // and {@code false} otherwise. 15 func Equals(a, b interface{}) bool { 16 if a == b { 17 return true 18 } 19 if a == nil || b == nil { 20 return a == b 21 } 22 v1 := reflect.ValueOf(a) 23 v2 := reflect.ValueOf(b) 24 if v1.Type() != v2.Type() { 25 return false 26 } 27 return a == b 28 } 29 30 // DeepEquals Returns {@code true} if the arguments are deeply equal to each other 31 // and {@code false} otherwise. 32 func DeepEquals(a, b interface{}) bool { 33 return reflect.DeepEqual(a, b) 34 } 35 36 // Returns 0 if the arguments are identical and {@code 37 // c.compare(a, b)} otherwise. 38 // Consequently, if both arguments are {@code null} 0 39 // is returned. 40 func Compare(a, b interface{}, compare Comparator) int { 41 if a == b { 42 return 0 43 } 44 return compare.Compare(a, b) 45 }