github.com/tursom/GoCollections@v0.3.10/lang/Int64.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 "strconv"
    10  
    11  type Int64 int64
    12  
    13  func CastInt64(v any) (int64, bool) {
    14  	switch i := v.(type) {
    15  	case int:
    16  		return int64(i), true
    17  	case int8:
    18  		return int64(i), true
    19  	case int16:
    20  		return int64(i), true
    21  	case int32:
    22  		return int64(i), true
    23  	case int64:
    24  		return i, true
    25  	case uint:
    26  		return int64(i), true
    27  	case uint8:
    28  		return int64(i), true
    29  	case uint16:
    30  		return int64(i), true
    31  	case uint32:
    32  		return int64(i), true
    33  	case uint64:
    34  		return int64(i), true
    35  	case float32:
    36  		return int64(i), true
    37  	case float64:
    38  		return int64(i), true
    39  	case Number:
    40  		return i.ToInt64().V(), true
    41  	default:
    42  		return 0, false
    43  	}
    44  }
    45  
    46  func EqualsInt64(i1 Number, i2 any) bool {
    47  	i2, ok := CastInt64(i2)
    48  	return ok && i2 == i1.ToUInt64().V()
    49  }
    50  
    51  func (i Int64) V() int64 {
    52  	return int64(i)
    53  }
    54  
    55  func (i *Int64) P() *int64 {
    56  	return (*int64)(i)
    57  }
    58  
    59  func (i Int64) String() string {
    60  	return strconv.FormatInt(int64(i), 10)
    61  }
    62  
    63  func (i Int64) AsObject() Object {
    64  	return i
    65  }
    66  
    67  func (i Int64) Equals(e Object) bool {
    68  	return EqualsInt64(i, e)
    69  }
    70  
    71  func (i Int64) ToString() String {
    72  	return NewString(i.String())
    73  }
    74  
    75  func (i Int64) HashCode() int32 {
    76  	return HashInt64(int64(i))
    77  }
    78  
    79  func (i Int64) Compare(t Int64) int {
    80  	switch {
    81  	case i > t:
    82  		return 1
    83  	case i == t:
    84  		return 0
    85  	default:
    86  		return -1
    87  	}
    88  }
    89  
    90  func (i Int64) ToInt64() Int64 {
    91  	return Int64(i)
    92  }
    93  
    94  func (i Int64) ToUInt64() UInt64 {
    95  	return UInt64(i)
    96  }
    97  
    98  func (i Int64) ToFloat64() Float64 {
    99  	return Float64(i)
   100  }
   101  
   102  func HashInt64(i int64) int32 {
   103  	return int32(i) ^ int32(i>>32)
   104  }