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