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

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