forked from gomem/gomem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparable.go
105 lines (96 loc) · 3.87 KB
/
comparable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package object
import "errors"
type Comparable interface {
// Compare methods
// Eq returns true if the left Object is equal to the right Object.
Eq(Object) (Boolean, error)
// Neq returns true if the left Object is not equal to the right Object.
Neq(Object) (Boolean, error)
// Less returns true if the left Object is less than the right Object.
Less(Object) (Boolean, error)
// LessEq returns true if the left Object is less than or equal to the right Object.
LessEq(Object) (Boolean, error)
// Greater returns true if the left Object is greter than the right Object.
Greater(Object) (Boolean, error)
// GreaterEq returns true if the left Object is greter than or equal to the right Object.
GreaterEq(Object) (Boolean, error)
// ToBoolean returns true when the value of this Object is not false.
ToBoolean() (value Boolean)
// IsEmpty returns true when the value of this object is equal to the
// value of this type of Object in it's default state.
// IsEmpty() bool
}
// These functions should be used when the left Object could be nil.
// They check if either is nil before calling the respective comparator method on the Object.
// Eq returns true if the left Object is equal to the right Object.
// If either is nil Eq returns false.
// Comparing different types is also an error.
func Eq(left Object, right Object) (Boolean, error) {
leftIsNil := IsNil(left)
rightIsNil := IsNil(right)
// If both are nil they are equal.
if leftIsNil && rightIsNil {
return Boolean(true), nil
}
// If one is nil and the other isn't they are not equal
if leftIsNil != rightIsNil {
return Boolean(false), nil
}
return left.Eq(right)
}
// Neq returns true if the left Object is not equal to the right Object.
// If either is nil Neq returns true.
// Comparing different types is also an error.
func Neq(left Object, right Object) (Boolean, error) {
leftIsNil := IsNil(left)
rightIsNil := IsNil(right)
// If both are nil they are equal.
if leftIsNil && rightIsNil {
return Boolean(false), nil
}
// If one is nil and the other isn't they are not equal
if leftIsNil != rightIsNil {
return Boolean(true), nil
}
return left.Neq(right)
}
// Less returns true if the left Object is less than the right Object.
// If either is nil Less returns false and an error.
// Comparing different types is also an error.
func Less(left Object, right Object) (Boolean, error) {
// If one is nil and the other isn't they cannot be compared.
if IsNil(left) || IsNil(right) {
return Boolean(false), errors.New("less than not defined on nil")
}
return left.Less(right)
}
// LessEq returns true if the left Object is less than or equal to the right Object.
// If either is nil LessEq returns false and an error.
// Comparing different types is also an error.
func LessEq(left Object, right Object) (Boolean, error) {
// If one is nil and the other isn't they cannot be compared.
if IsNil(left) || IsNil(right) {
return Boolean(false), errors.New("less than or equal to not defined on nil")
}
return left.LessEq(right)
}
// Greater returns true if the left Object is greter than the right Object.
// If either is nil Greater returns false and an error.
// Comparing different types is also an error.
func Greater(left Object, right Object) (Boolean, error) {
// If one is nil and the other isn't they cannot be compared.
if IsNil(left) || IsNil(right) {
return Boolean(false), errors.New("greater than not defined on nil")
}
return left.Greater(right)
}
// GreaterEq returns true if the left Object is greter than or equal to the right Object.
// If either is nil GreaterEq returns false and an error.
// Comparing different types is also an error.
func GreaterEq(left Object, right Object) (Boolean, error) {
// If one is nil and the other isn't they cannot be compared.
if IsNil(left) || IsNil(right) {
return Boolean(false), errors.New("greater than or equal to not defined on nil")
}
return left.GreaterEq(right)
}