github.com/tursom/GoCollections@v0.3.10/collections/CopyOnWriteList.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package collections
     8  
     9  import (
    10  	"github.com/tursom/GoCollections/exceptions"
    11  	"github.com/tursom/GoCollections/lang"
    12  )
    13  
    14  type (
    15  	CopyOnWriteList[E lang.Object] struct {
    16  		lang.BaseObject
    17  		arr []E
    18  	}
    19  
    20  	copyOnWriteListIterator[E lang.Object] struct {
    21  		lang.BaseObject
    22  		list  *CopyOnWriteList[E]
    23  		arr   []E
    24  		index int
    25  	}
    26  )
    27  
    28  func (l *CopyOnWriteList[E]) Iterator() Iterator[E] {
    29  	return l.MutableIterator()
    30  }
    31  
    32  func (l *CopyOnWriteList[E]) Size() int {
    33  	return len(l.arr)
    34  }
    35  
    36  func (l *CopyOnWriteList[E]) IsEmpty() bool {
    37  	return l.Size() == 0
    38  }
    39  
    40  func (l *CopyOnWriteList[E]) Contains(element E) bool {
    41  	return Contains[E](l, element)
    42  }
    43  
    44  func (l *CopyOnWriteList[E]) ContainsAll(c Collection[E]) bool {
    45  	return ContainsAll[E](l, c)
    46  }
    47  
    48  func (l *CopyOnWriteList[E]) MutableIterator() MutableIterator[E] {
    49  	return &copyOnWriteListIterator[E]{
    50  		list:  l,
    51  		arr:   l.arr,
    52  		index: 0,
    53  	}
    54  }
    55  
    56  func (l *CopyOnWriteList[E]) Add(element E) bool {
    57  	l.arr = append(l.arr, element)
    58  	return true
    59  }
    60  
    61  func (l *CopyOnWriteList[E]) Remove(element E) exceptions.Exception {
    62  	return Remove[E](l, element)
    63  }
    64  
    65  func (l *CopyOnWriteList[E]) AddAll(c Collection[E]) bool {
    66  	_ = Loop[E](c, func(element E) exceptions.Exception {
    67  		l.arr = append(l.arr, element)
    68  		return nil
    69  	})
    70  	return true
    71  }
    72  
    73  func (l *CopyOnWriteList[E]) RemoveAll(c Collection[E]) bool {
    74  	return RemoveAll[E](l, c)
    75  }
    76  
    77  func (l *CopyOnWriteList[E]) RetainAll(c Collection[E]) bool {
    78  	return RetainAll[E](l, c)
    79  }
    80  
    81  func (l *CopyOnWriteList[E]) Clear() {
    82  	l.arr = make([]E, 0)
    83  }
    84  
    85  func (l *CopyOnWriteList[E]) Get(index int) (E, exceptions.Exception) {
    86  	if index < 0 || index >= l.Size() {
    87  		return lang.Nil[E](), exceptions.NewIndexOutOfBound("", nil)
    88  	}
    89  	return l.arr[index], nil
    90  }
    91  
    92  func (l *CopyOnWriteList[E]) SubList(from, to int) List[E] {
    93  	return l.SubList(from, to)
    94  }
    95  
    96  func (l *CopyOnWriteList[E]) Set(index int, element E) exceptions.Exception {
    97  	if index < 0 || index >= l.Size() {
    98  		return exceptions.NewIndexOutOfBound("", nil)
    99  	}
   100  	l.arr[index] = element
   101  	return nil
   102  }
   103  
   104  func (l *CopyOnWriteList[E]) AddAtIndex(index int, element E) bool {
   105  	//TODO implement me
   106  	panic("implement me")
   107  }
   108  
   109  func (l *CopyOnWriteList[E]) RemoveAt(index int) exceptions.Exception {
   110  	//TODO implement me
   111  	panic("implement me")
   112  }
   113  
   114  func (l *CopyOnWriteList[E]) SubMutableList(from, to int) MutableList[E] {
   115  	//TODO implement me
   116  	panic("implement me")
   117  }
   118  
   119  func (c *copyOnWriteListIterator[E]) HasNext() bool {
   120  	//TODO implement me
   121  	panic("implement me")
   122  }
   123  
   124  func (c *copyOnWriteListIterator[E]) Next() (E, exceptions.Exception) {
   125  	//TODO implement me
   126  	panic("implement me")
   127  }
   128  
   129  func (c *copyOnWriteListIterator[E]) Remove() exceptions.Exception {
   130  	//TODO implement me
   131  	panic("implement me")
   132  }