2021-05-20 17:32:35 +08:00
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-05-21 09:48:33 +08:00
|
|
|
"github.com/tursom/GoCollections/exceptions"
|
2021-05-20 17:32:35 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Collection interface {
|
|
|
|
Iterator() Iterator
|
|
|
|
Size() uint32
|
|
|
|
IsEmpty() bool
|
|
|
|
Contains(element interface{}) bool
|
|
|
|
ContainsAll(c Collection) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableCollection interface {
|
|
|
|
Iterator() Iterator
|
|
|
|
Size() uint32
|
|
|
|
IsEmpty() bool
|
|
|
|
Contains(element interface{}) bool
|
|
|
|
ContainsAll(c Collection) bool
|
|
|
|
|
|
|
|
MutableIterator() MutableIterator
|
|
|
|
Add(element interface{}) bool
|
2021-05-21 16:57:33 +08:00
|
|
|
Remove(element interface{}) exceptions.Exception
|
2021-05-20 17:32:35 +08:00
|
|
|
AddAll(c Collection) bool
|
|
|
|
RemoveAll(c Collection) bool
|
|
|
|
RetainAll(c Collection) bool
|
|
|
|
Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
List interface {
|
|
|
|
Iterator() Iterator
|
|
|
|
Size() uint32
|
|
|
|
IsEmpty() bool
|
|
|
|
Contains(element interface{}) bool
|
|
|
|
ContainsAll(c Collection) bool
|
|
|
|
|
2021-05-21 16:57:33 +08:00
|
|
|
Get(index uint32) (interface{}, exceptions.Exception)
|
2021-05-20 17:32:35 +08:00
|
|
|
SubList(from, to uint32) List
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableList interface {
|
|
|
|
Iterator() Iterator
|
|
|
|
Size() uint32
|
|
|
|
IsEmpty() bool
|
|
|
|
Contains(element interface{}) bool
|
|
|
|
ContainsAll(c Collection) bool
|
|
|
|
|
|
|
|
MutableIterator() MutableIterator
|
|
|
|
Add(element interface{}) bool
|
2021-05-21 16:57:33 +08:00
|
|
|
Remove(element interface{}) exceptions.Exception
|
2021-05-20 17:32:35 +08:00
|
|
|
AddAll(c Collection) bool
|
|
|
|
RemoveAll(c Collection) bool
|
|
|
|
RetainAll(c Collection) bool
|
|
|
|
Clear()
|
|
|
|
|
2021-05-21 16:57:33 +08:00
|
|
|
Get(index uint32) (interface{}, exceptions.Exception)
|
2021-05-20 17:32:35 +08:00
|
|
|
SubList(from, to uint32) List
|
|
|
|
|
2021-05-21 16:57:33 +08:00
|
|
|
Set(index uint32, element interface{}) exceptions.Exception
|
2021-05-20 17:32:35 +08:00
|
|
|
AddAtIndex(index uint32, element interface{}) bool
|
2021-05-21 16:57:33 +08:00
|
|
|
RemoveAt(index uint32) exceptions.Exception
|
2021-05-20 17:32:35 +08:00
|
|
|
SubMutableList(from, to uint32) MutableList
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Contains(l Collection, element interface{}) bool {
|
2021-05-21 16:57:33 +08:00
|
|
|
return Loop(l, func(e interface{}) exceptions.Exception {
|
2021-05-20 17:32:35 +08:00
|
|
|
if e == element {
|
|
|
|
return exceptions.ElementFound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContainsAll(l Collection, collection Collection) bool {
|
2021-05-21 16:57:33 +08:00
|
|
|
return Loop(collection, func(e interface{}) exceptions.Exception {
|
2021-05-20 17:32:35 +08:00
|
|
|
if l.Contains(e) {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return exceptions.ElementNotFound
|
|
|
|
}
|
|
|
|
}) == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddAll(l MutableCollection, collection Collection) bool {
|
2021-05-21 16:57:33 +08:00
|
|
|
return Loop(collection, func(e interface{}) exceptions.Exception {
|
2021-05-20 17:32:35 +08:00
|
|
|
if !l.Add(e) {
|
|
|
|
return exceptions.CollectionLoopFinished
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}) == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveAll(l MutableCollection, collection Collection) bool {
|
2021-05-21 16:57:33 +08:00
|
|
|
return Loop(collection, func(e interface{}) exceptions.Exception {
|
2021-05-21 09:41:58 +08:00
|
|
|
return l.Remove(e)
|
2021-05-20 17:32:35 +08:00
|
|
|
}) == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RetainAll(l MutableCollection, collection Collection) bool {
|
2021-05-21 16:57:33 +08:00
|
|
|
return LoopMutable(l, func(element interface{}, iterator MutableIterator) exceptions.Exception {
|
2021-05-20 17:32:35 +08:00
|
|
|
if !collection.Contains(element) {
|
2021-05-21 10:31:49 +08:00
|
|
|
return iterator.Remove()
|
2021-05-20 17:32:35 +08:00
|
|
|
}
|
|
|
|
return nil
|
2021-05-21 10:31:49 +08:00
|
|
|
}) == nil
|
2021-05-20 17:32:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func String(l List) string {
|
|
|
|
if l.IsEmpty() {
|
|
|
|
return "[]"
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := strings.Builder{}
|
|
|
|
builder.WriteString("[")
|
|
|
|
iterator := l.Iterator()
|
2021-05-21 09:41:58 +08:00
|
|
|
next, _ := iterator.Next()
|
2021-05-20 17:32:35 +08:00
|
|
|
builder.WriteString(fmt.Sprint(next))
|
|
|
|
for iterator.HasNext() {
|
|
|
|
builder.WriteString(", ")
|
2021-05-21 09:41:58 +08:00
|
|
|
next, _ = iterator.Next()
|
2021-05-20 17:32:35 +08:00
|
|
|
builder.WriteString(fmt.Sprint(next))
|
|
|
|
}
|
|
|
|
builder.WriteString("]")
|
|
|
|
return builder.String()
|
|
|
|
}
|