github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/lists/singlylinkedlist/singlylinkedlist_safe.go (about)

     1  package singlylinkedlist
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     5  	"github.com/songzhibin97/go-baseutils/structure/lists"
     6  	"sync"
     7  )
     8  
     9  var _ lists.List[any] = (*ListSafe[any])(nil)
    10  
    11  type ListSafe[E any] struct {
    12  	unsafe *List[E]
    13  	lock   sync.Mutex
    14  }
    15  
    16  func (s *ListSafe[E]) Add(values ...E) {
    17  	s.lock.Lock()
    18  	defer s.lock.Unlock()
    19  	s.unsafe.Add(values...)
    20  
    21  }
    22  
    23  func (s *ListSafe[E]) Append(values ...E) {
    24  	s.lock.Lock()
    25  	defer s.lock.Unlock()
    26  	s.unsafe.Append(values...)
    27  
    28  }
    29  
    30  func (s *ListSafe[E]) Prepend(values ...E) {
    31  	s.lock.Lock()
    32  	defer s.lock.Unlock()
    33  	s.unsafe.Prepend(values...)
    34  
    35  }
    36  
    37  func (s *ListSafe[E]) Get(index int) (E, bool) {
    38  	s.lock.Lock()
    39  	defer s.lock.Unlock()
    40  	return s.unsafe.Get(index)
    41  }
    42  
    43  func (s *ListSafe[E]) Remove(index int) {
    44  	s.lock.Lock()
    45  	defer s.lock.Unlock()
    46  	s.unsafe.Remove(index)
    47  
    48  }
    49  
    50  func (s *ListSafe[E]) Contains(values ...E) bool {
    51  	s.lock.Lock()
    52  	defer s.lock.Unlock()
    53  	return s.unsafe.Contains(values...)
    54  }
    55  
    56  func (s *ListSafe[E]) Values() []E {
    57  	s.lock.Lock()
    58  	defer s.lock.Unlock()
    59  	return s.unsafe.Values()
    60  }
    61  
    62  func (s *ListSafe[E]) IndexOf(value E) int {
    63  	s.lock.Lock()
    64  	defer s.lock.Unlock()
    65  	return s.unsafe.IndexOf(value)
    66  }
    67  
    68  func (s *ListSafe[E]) Empty() bool {
    69  	s.lock.Lock()
    70  	defer s.lock.Unlock()
    71  	return s.unsafe.Empty()
    72  }
    73  
    74  func (s *ListSafe[E]) Size() int {
    75  	s.lock.Lock()
    76  	defer s.lock.Unlock()
    77  	return s.unsafe.Size()
    78  }
    79  
    80  func (s *ListSafe[E]) Clear() {
    81  	s.lock.Lock()
    82  	defer s.lock.Unlock()
    83  	s.unsafe.Clear()
    84  
    85  }
    86  
    87  func (s *ListSafe[E]) Sort(comparator bcomparator.Comparator[E]) {
    88  	s.lock.Lock()
    89  	defer s.lock.Unlock()
    90  	s.unsafe.Sort(comparator)
    91  
    92  }
    93  
    94  func (s *ListSafe[E]) Swap(i int, j int) {
    95  	s.lock.Lock()
    96  	defer s.lock.Unlock()
    97  	s.unsafe.Swap(i, j)
    98  
    99  }
   100  
   101  func (s *ListSafe[E]) Insert(index int, values ...E) {
   102  	s.lock.Lock()
   103  	defer s.lock.Unlock()
   104  	s.unsafe.Insert(index, values...)
   105  
   106  }
   107  
   108  func (s *ListSafe[E]) Set(index int, value E) {
   109  	s.lock.Lock()
   110  	defer s.lock.Unlock()
   111  	s.unsafe.Set(index, value)
   112  
   113  }
   114  
   115  func (s *ListSafe[E]) String() string {
   116  	s.lock.Lock()
   117  	defer s.lock.Unlock()
   118  	return s.unsafe.String()
   119  }
   120  
   121  func (s *ListSafe[E]) UnmarshalJSON(bytes []byte) error {
   122  	s.lock.Lock()
   123  	defer s.lock.Unlock()
   124  	return s.unsafe.UnmarshalJSON(bytes)
   125  }
   126  
   127  func (s *ListSafe[E]) MarshalJSON() ([]byte, error) {
   128  	s.lock.Lock()
   129  	defer s.lock.Unlock()
   130  	return s.unsafe.MarshalJSON()
   131  }