GoCollections/collections/Collection.go

103 lines
2.1 KiB
Go
Raw Normal View History

2022-11-25 18:19:33 +08:00
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
2021-05-20 17:32:35 +08:00
package collections
import (
"fmt"
2022-04-23 11:53:41 +08:00
"strings"
2021-05-21 09:48:33 +08:00
"github.com/tursom/GoCollections/exceptions"
2022-03-21 11:02:41 +08:00
"github.com/tursom/GoCollections/lang"
2021-05-20 17:32:35 +08:00
)
type (
2022-04-23 11:53:41 +08:00
Collection[E any] interface {
Iterable[E]
2022-03-21 11:02:41 +08:00
Size() int
2021-05-20 17:32:35 +08:00
IsEmpty() bool
2022-04-23 11:53:41 +08:00
Contains(element E) bool
ContainsAll(c Collection[E]) bool
2021-05-20 17:32:35 +08:00
}
2022-03-21 11:02:41 +08:00
MutableCollection[T any] interface {
Collection[T]
MutableIterable[T]
2021-05-20 17:32:35 +08:00
2022-03-21 11:02:41 +08:00
Add(element T) bool
Remove(element T) exceptions.Exception
AddAll(c Collection[T]) bool
RemoveAll(c Collection[T]) bool
RetainAll(c Collection[T]) bool
2021-05-20 17:32:35 +08:00
Clear()
}
2022-03-21 11:02:41 +08:00
List[T any] interface {
Collection[T]
2021-05-20 17:32:35 +08:00
2022-03-21 11:02:41 +08:00
Get(index int) (T, exceptions.Exception)
SubList(from, to int) List[T]
2022-04-23 11:53:41 +08:00
ListIterator() ListIterator[T]
2021-05-20 17:32:35 +08:00
}
2022-03-21 11:02:41 +08:00
MutableList[T any] interface {
List[T]
MutableCollection[T]
2021-05-20 17:32:35 +08:00
2022-03-21 11:02:41 +08:00
Set(index int, element T) exceptions.Exception
AddAtIndex(index int, element T) bool
RemoveAt(index int) exceptions.Exception
SubMutableList(from, to int) MutableList[T]
2022-04-23 11:53:41 +08:00
MutableListIterator() MutableListIterator[T]
2021-05-20 17:32:35 +08:00
}
)
2022-03-23 10:15:18 +08:00
func ListGet[T lang.Object](list List[T], index int) T {
get, err := list.Get(index)
if err != nil {
panic(err)
}
return get
}
2022-03-21 11:02:41 +08:00
func ContainsAll[T lang.Object](l Collection[T], collection Collection[T]) bool {
return Loop[T](collection, func(e T) exceptions.Exception {
2021-05-20 17:32:35 +08:00
if l.Contains(e) {
return nil
} else {
return exceptions.ElementNotFound
}
}) == nil
}
2022-03-21 11:02:41 +08:00
func AddAll[T any](l MutableCollection[T], collection Collection[T]) bool {
return Loop[T](collection, func(e T) exceptions.Exception {
2021-05-20 17:32:35 +08:00
if !l.Add(e) {
return exceptions.CollectionLoopFinished
}
return nil
}) == nil
}
2022-03-21 11:02:41 +08:00
func String[T any](l Iterable[T]) string {
2021-05-21 23:07:48 +08:00
iterator := l.Iterator()
if !iterator.HasNext() {
2021-05-20 17:32:35 +08:00
return "[]"
}
builder := strings.Builder{}
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))
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()
}