GoCollections/lang/Object.go

76 lines
1006 B
Go
Raw Normal View History

2022-03-21 11:02:41 +08:00
package lang
2022-03-23 10:15:18 +08:00
import (
"fmt"
"unsafe"
)
type (
AsObject interface {
AsObject() Object
}
Equable interface {
Equals(o Object) bool
}
Object interface {
2022-03-28 18:19:05 +08:00
fmt.Stringer
AsObject
Equable
2022-03-23 10:15:18 +08:00
HashCode() int32
}
Any = Object
BaseObject struct {
}
)
2022-03-21 11:02:41 +08:00
2022-03-28 18:19:05 +08:00
func ToString(obj Object) String {
return NewString(obj.String())
}
2022-03-21 11:02:41 +08:00
func Equals(e Object, t Object) bool {
if e == nil {
return t == nil
}
return e.Equals(t)
}
2022-03-23 10:15:18 +08:00
2022-03-28 18:19:05 +08:00
func HashCode(obj Object) int32 {
if obj == nil {
return 0
} else {
return obj.HashCode()
}
}
2022-03-23 10:15:18 +08:00
func NewBaseObject() BaseObject {
return BaseObject{}
}
func (b *BaseObject) AsObject() Object {
return b
}
func (b *BaseObject) Equals(o Object) bool {
return b == o
}
2022-03-28 18:19:05 +08:00
func (b *BaseObject) GoString() string {
return b.String()
}
func (b *BaseObject) String() string {
return fmt.Sprintf("BaseObject@%p", unsafe.Pointer(b))
}
2022-03-23 10:15:18 +08:00
func (b *BaseObject) ToString() String {
2022-03-28 18:19:05 +08:00
return NewString(b.String())
2022-03-23 10:15:18 +08:00
}
func (b *BaseObject) HashCode() int32 {
return Hash64(b)
}