gitlab.com/evatix-go/core@v1.3.55/coredata/coreonce/IntegerOnce.go (about)

     1  package coreonce
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  )
     7  
     8  type IntegerOnce struct {
     9  	innerData       int
    10  	initializerFunc func() int
    11  	isInitialized   bool
    12  }
    13  
    14  func NewIntegerOnce(initializerFunc func() int) IntegerOnce {
    15  	return IntegerOnce{
    16  		initializerFunc: initializerFunc,
    17  	}
    18  }
    19  
    20  func NewIntegerOncePtr(initializerFunc func() int) *IntegerOnce {
    21  	return &IntegerOnce{
    22  		initializerFunc: initializerFunc,
    23  	}
    24  }
    25  
    26  func (it *IntegerOnce) MarshalJSON() ([]byte, error) {
    27  	return json.Marshal(it.Value())
    28  }
    29  
    30  func (it *IntegerOnce) UnmarshalJSON(data []byte) error {
    31  	it.isInitialized = true
    32  
    33  	return json.Unmarshal(data, &it.innerData)
    34  }
    35  
    36  func (it *IntegerOnce) Value() int {
    37  	if it.isInitialized {
    38  		return it.innerData
    39  	}
    40  
    41  	it.innerData = it.initializerFunc()
    42  	it.isInitialized = true
    43  
    44  	return it.innerData
    45  }
    46  
    47  func (it *IntegerOnce) Execute() int {
    48  	return it.Value()
    49  }
    50  
    51  // IsEmpty returns true if zero
    52  func (it *IntegerOnce) IsEmpty() bool {
    53  	return it.Value() == 0
    54  }
    55  
    56  func (it *IntegerOnce) IsZero() bool {
    57  	return it.Value() == 0
    58  }
    59  
    60  func (it *IntegerOnce) IsAboveZero() bool {
    61  	return it.Value() > 0
    62  }
    63  
    64  func (it *IntegerOnce) IsAboveEqualZero() bool {
    65  	return it.Value() >= 0
    66  }
    67  
    68  func (it *IntegerOnce) IsLessThanZero() bool {
    69  	return it.Value() < 0
    70  }
    71  
    72  func (it *IntegerOnce) IsLessThanEqualZero() bool {
    73  	return it.Value() <= 0
    74  }
    75  
    76  func (it *IntegerOnce) IsAbove(i int) bool {
    77  	return it.Value() > i
    78  }
    79  
    80  func (it *IntegerOnce) IsAboveEqual(i int) bool {
    81  	return it.Value() >= i
    82  }
    83  
    84  func (it *IntegerOnce) IsLessThan(i int) bool {
    85  	return it.Value() < i
    86  }
    87  
    88  func (it *IntegerOnce) IsLessThanEqual(i int) bool {
    89  	return it.Value() <= i
    90  }
    91  
    92  func (it *IntegerOnce) IsInvalidIndex() bool {
    93  	return it.Value() < 0
    94  }
    95  
    96  func (it *IntegerOnce) IsValidIndex() bool {
    97  	return it.Value() >= 0
    98  }
    99  
   100  func (it *IntegerOnce) IsNegative() bool {
   101  	return it.Value() < 0
   102  }
   103  
   104  func (it *IntegerOnce) IsPositive() bool {
   105  	return it.Value() > 0
   106  }
   107  
   108  func (it *IntegerOnce) String() string {
   109  	return strconv.Itoa(it.Value())
   110  }
   111  
   112  func (it *IntegerOnce) Serialize() ([]byte, error) {
   113  	value := it.Value()
   114  
   115  	return json.Marshal(value)
   116  }