github.com/tursom/GoCollections@v0.3.10/lang/Object.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  import (
    10  	"fmt"
    11  	"unsafe"
    12  )
    13  
    14  type (
    15  	AsObject interface {
    16  		AsObject() Object
    17  	}
    18  
    19  	Equable interface {
    20  		Equals(o Object) bool
    21  	}
    22  
    23  	Object interface {
    24  		fmt.Stringer
    25  		AsObject
    26  		Equable
    27  		HashCode() int32
    28  	}
    29  
    30  	Any = Object
    31  
    32  	IsBaseObject interface {
    33  		AsBaseObject() *BaseObject
    34  	}
    35  
    36  	BaseObject struct {
    37  	}
    38  )
    39  
    40  func ToString(obj Object) String {
    41  	return NewString(obj.String())
    42  }
    43  
    44  func Equals(e Object, t Object) bool {
    45  	if e == nil {
    46  		return t == nil
    47  	}
    48  	return e.Equals(t)
    49  }
    50  
    51  func HashCode(obj Object) int32 {
    52  	if obj == nil {
    53  		return 0
    54  	} else {
    55  		return obj.HashCode()
    56  	}
    57  }
    58  
    59  func NewBaseObject() BaseObject {
    60  	return BaseObject{}
    61  }
    62  
    63  func (b *BaseObject) AsObject() Object {
    64  	return b
    65  }
    66  
    67  func (b *BaseObject) AsBaseObject() *BaseObject {
    68  	return b
    69  }
    70  
    71  func (b *BaseObject) Equals(o Object) bool {
    72  	return b == o
    73  }
    74  
    75  func (b *BaseObject) GoString() string {
    76  	return b.String()
    77  }
    78  
    79  func (b *BaseObject) String() string {
    80  	return fmt.Sprintf("BaseObject@%p", unsafe.Pointer(b))
    81  }
    82  
    83  func (b *BaseObject) ToString() String {
    84  	return NewString(b.String())
    85  }
    86  
    87  func (b *BaseObject) HashCode() int32 {
    88  	return Hash64(b)
    89  }
    90  
    91  func (b *BaseObject) Compare(t IsBaseObject) int {
    92  	o := t.AsBaseObject()
    93  	if b == o {
    94  		return 0
    95  	} else if uintptr(unsafe.Pointer(b)) > uintptr(unsafe.Pointer(o)) {
    96  		return 1
    97  	} else {
    98  		return -1
    99  	}
   100  }