github.com/kaydxh/golang@v0.0.131/go/time/exponentialbackeoff_syncmap.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package time 23 24 import ( 25 "sync" // Used by sync.Map. 26 ) 27 28 type ExponentialBackOffMap sync.Map 29 30 // Generate code that will fail if the constants change value. 31 func _() { 32 // An "cannot convert ExponentialBackOffMap literal (type ExponentialBackOffMap) to type sync.Map" compiler error signifies that the base type have changed. 33 // Re-run the go-syncmap command to generate them again. 34 _ = (sync.Map)(ExponentialBackOffMap{}) 35 } 36 37 var _nil_ExponentialBackOffMap_ExponentialBackOff_value = func() (val ExponentialBackOff) { return }() 38 39 // Load returns the value stored in the map for a key, or nil if no 40 // value is present. 41 // The ok result indicates whether value was found in the map. 42 func (m *ExponentialBackOffMap) Load(key string) (ExponentialBackOff, bool) { 43 value, ok := (*sync.Map)(m).Load(key) 44 if value == nil { 45 return _nil_ExponentialBackOffMap_ExponentialBackOff_value, ok 46 } 47 return value.(ExponentialBackOff), ok 48 } 49 50 // Store sets the value for a key. 51 func (m *ExponentialBackOffMap) Store(key string, value ExponentialBackOff) { 52 (*sync.Map)(m).Store(key, value) 53 } 54 55 // LoadOrStore returns the existing value for the key if present. 56 // Otherwise, it stores and returns the given value. 57 // The loaded result is true if the value was loaded, false if stored. 58 func (m *ExponentialBackOffMap) LoadOrStore(key string, value ExponentialBackOff) (ExponentialBackOff, bool) { 59 actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) 60 if actual == nil { 61 return _nil_ExponentialBackOffMap_ExponentialBackOff_value, loaded 62 } 63 return actual.(ExponentialBackOff), loaded 64 } 65 66 // LoadAndDelete deletes the value for a key, returning the previous value if any. 67 // The loaded result reports whether the key was present. 68 func (m *ExponentialBackOffMap) LoadAndDelete(key string) (value ExponentialBackOff, loaded bool) { 69 actual, loaded := (*sync.Map)(m).LoadAndDelete(key) 70 if actual == nil { 71 return _nil_ExponentialBackOffMap_ExponentialBackOff_value, loaded 72 } 73 return actual.(ExponentialBackOff), loaded 74 } 75 76 // Delete deletes the value for a key. 77 func (m *ExponentialBackOffMap) Delete(key string) { 78 (*sync.Map)(m).Delete(key) 79 } 80 81 // Range calls f sequentially for each key and value present in the map. 82 // If f returns false, range stops the iteration. 83 // 84 // Range does not necessarily correspond to any consistent snapshot of the Map's 85 // contents: no key will be visited more than once, but if the value for any key 86 // is stored or deleted concurrently, Range may reflect any mapping for that key 87 // from any point during the Range call. 88 // 89 // Range may be O(N) with the number of elements in the map even if f returns 90 // false after a constant number of calls. 91 func (m *ExponentialBackOffMap) Range(f func(key string, value ExponentialBackOff) bool) { 92 (*sync.Map)(m).Range(func(key, value interface{}) bool { 93 return f(key.(string), value.(ExponentialBackOff)) 94 }) 95 }