gitlab.com/evatix-go/core@v1.3.55/coredata/coreonce/IntegersOnce.go (about) 1 package coreonce 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "sort" 7 8 "gitlab.com/evatix-go/core/constants" 9 ) 10 11 type IntegersOnce struct { 12 innerData []int 13 initializerFunc func() []int 14 isInitialized bool 15 sortedValues []int 16 } 17 18 func NewIntegersOnce(initializerFunc func() []int) IntegersOnce { 19 return IntegersOnce{ 20 initializerFunc: initializerFunc, 21 } 22 } 23 24 func NewIntegersOncePtr(initializerFunc func() []int) *IntegersOnce { 25 return &IntegersOnce{ 26 initializerFunc: initializerFunc, 27 } 28 } 29 30 func (it *IntegersOnce) MarshalJSON() ([]byte, error) { 31 return json.Marshal(it.Value()) 32 } 33 34 func (it *IntegersOnce) UnmarshalJSON(data []byte) error { 35 it.isInitialized = true 36 37 return json.Unmarshal(data, &it.innerData) 38 } 39 40 func (it *IntegersOnce) Value() []int { 41 if it.isInitialized { 42 return it.innerData 43 } 44 45 it.innerData = it.initializerFunc() 46 it.isInitialized = true 47 48 return it.innerData 49 } 50 51 // IsEmpty returns true if zero 52 func (it *IntegersOnce) IsEmpty() bool { 53 return len(it.Value()) == 0 54 } 55 56 func (it *IntegersOnce) IsZero() bool { 57 return len(it.Value()) == 0 58 } 59 60 // Sorted 61 // 62 // Warning : Current values will be mutated, 63 // so better to make a clone of it. 64 func (it *IntegersOnce) Sorted() []int { 65 if it.sortedValues != nil { 66 return it.sortedValues 67 } 68 69 it.sortedValues = it.Value() 70 sort.Ints(it.sortedValues) 71 72 return it.sortedValues 73 } 74 75 func (it *IntegersOnce) RangesMap() map[int]int { 76 values := it.Value() 77 78 if len(values) == 0 { 79 return map[int]int{} 80 } 81 82 newMap := make(map[int]int, len(values)) 83 84 for i, value := range values { 85 newMap[value] = i 86 } 87 88 return newMap 89 } 90 91 func (it *IntegersOnce) RangesBoolMap() map[int]bool { 92 values := it.Value() 93 94 if len(values) == 0 { 95 return map[int]bool{} 96 } 97 98 newMap := make(map[int]bool, len(values)) 99 100 for _, value := range values { 101 newMap[value] = true 102 } 103 104 return newMap 105 } 106 107 func (it *IntegersOnce) UniqueMap() map[int]bool { 108 values := it.Value() 109 110 if len(values) == 0 { 111 return map[int]bool{} 112 } 113 114 newMap := make(map[int]bool, len(values)) 115 116 for _, value := range values { 117 newMap[value] = true 118 } 119 120 return newMap 121 } 122 123 func (it *IntegersOnce) Serialize() ([]byte, error) { 124 values := it.Value() 125 126 return json.Marshal(values) 127 } 128 129 func (it *IntegersOnce) IsEqual(integerItems ...int) bool { 130 if it == nil && integerItems == nil { 131 return true 132 } 133 134 currentItems := it.Value() 135 if currentItems == nil && integerItems == nil { 136 return true 137 } 138 139 if currentItems == nil || integerItems == nil { 140 return false 141 } 142 143 if len(currentItems) != len(integerItems) { 144 return false 145 } 146 147 for i, item := range currentItems { 148 if item != integerItems[i] { 149 return false 150 } 151 } 152 153 return true 154 } 155 156 func (it *IntegersOnce) String() string { 157 return fmt.Sprintf( 158 constants.SprintValueFormat, 159 it.Value()) 160 } 161 162 func (it *IntegersOnce) Values() []int { 163 return it.Value() 164 } 165 166 func (it *IntegersOnce) Execute() []int { 167 return it.Value() 168 } 169 170 func (it *IntegersOnce) Integers() []int { 171 return it.Value() 172 } 173 174 func (it *IntegersOnce) Slice() []int { 175 return it.Value() 176 } 177 178 func (it *IntegersOnce) List() []int { 179 return it.Value() 180 }