github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/lockable_slice.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  type _LockableSlice struct {
    24  	slice  []interface{}
    25  	locked bool
    26  	index  int
    27  }
    28  
    29  func _NewLockableSlice() *_LockableSlice {
    30  	return &_LockableSlice{
    31  		slice: []interface{}{},
    32  	}
    33  }
    34  
    35  func (this *_LockableSlice) _RequireNotLocked() {
    36  	if this.locked {
    37  		panic(errLockedSlice)
    38  	}
    39  }
    40  
    41  func (this *_LockableSlice) _SetLocked(locked bool) *_LockableSlice { // nolint
    42  	this.locked = locked
    43  	return this
    44  }
    45  
    46  func (this *_LockableSlice) _SetSlice(slice []interface{}) *_LockableSlice { //nolint
    47  	this._RequireNotLocked()
    48  	this.slice = slice
    49  	this.index = 0
    50  	return this
    51  }
    52  
    53  func (this *_LockableSlice) _Push(items ...interface{}) *_LockableSlice {
    54  	this._RequireNotLocked()
    55  	this.slice = append(this.slice, items...)
    56  	return this
    57  }
    58  
    59  func (this *_LockableSlice) _Clear() *_LockableSlice { //nolint
    60  	this._RequireNotLocked()
    61  	this.slice = []interface{}{}
    62  	return this
    63  }
    64  
    65  func (this *_LockableSlice) _Get(index int) interface{} { //nolint
    66  	return this.slice[index]
    67  }
    68  
    69  func (this *_LockableSlice) _Set(index int, item interface{}) *_LockableSlice { //nolint
    70  	this._RequireNotLocked()
    71  
    72  	if len(this.slice) == index {
    73  		this.slice = append(this.slice, item)
    74  	} else {
    75  		this.slice[index] = item
    76  	}
    77  
    78  	return this
    79  }
    80  
    81  func (this *_LockableSlice) _SetIfAbsent(index int, item interface{}) *_LockableSlice { //nolint
    82  	this._RequireNotLocked()
    83  	if len(this.slice) == index || this.slice[index] == nil {
    84  		this._Set(index, item)
    85  	}
    86  	return this
    87  }
    88  
    89  func (this *_LockableSlice) _GetNext() interface{} { //nolint
    90  	return this._Get(this._Advance())
    91  }
    92  
    93  func (this *_LockableSlice) _GetCurrent() interface{} { //nolint
    94  	return this._Get(this.index)
    95  }
    96  
    97  func (this *_LockableSlice) _Advance() int { //nolint
    98  	index := this.index
    99  	if len(this.slice) != 0 {
   100  		this.index = (this.index + 1) % len(this.slice)
   101  	}
   102  	return index
   103  }
   104  
   105  func (this *_LockableSlice) _IsEmpty() bool { //nolint
   106  	return len(this.slice) == 0
   107  }
   108  
   109  func (this *_LockableSlice) _Length() int { //nolint
   110  	return len(this.slice)
   111  }