mirror of
https://github.com/tursom/GoCollections.git
synced 2026-08-22 07:03:28 +08:00
update Exception
This commit is contained in:
@@ -59,7 +59,7 @@ func (a ArrayList) IndexOf(element interface{}) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func (a *ArrayList) Remove(element interface{}) error {
|
||||
func (a *ArrayList) Remove(element interface{}) exceptions.Exception {
|
||||
index := a.IndexOf(element)
|
||||
if index < 0 {
|
||||
return exceptions.NewElementNotFoundException("", true)
|
||||
@@ -84,7 +84,7 @@ func (a *ArrayList) Clear() {
|
||||
a.used = 0
|
||||
}
|
||||
|
||||
func (a ArrayList) Get(index uint32) (interface{}, error) {
|
||||
func (a ArrayList) Get(index uint32) (interface{}, exceptions.Exception) {
|
||||
if index >= a.used {
|
||||
return nil, exceptions.NewIndexOutOfBound("", true)
|
||||
} else {
|
||||
@@ -96,7 +96,7 @@ func (a ArrayList) SubList(from, to uint32) List {
|
||||
return NewSubList(a, from, to)
|
||||
}
|
||||
|
||||
func (a *ArrayList) Set(index uint32, element interface{}) error {
|
||||
func (a *ArrayList) Set(index uint32, element interface{}) exceptions.Exception {
|
||||
if index >= a.used {
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func (a *ArrayList) AddAtIndex(index uint32, element interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *ArrayList) RemoveAt(index uint32) error {
|
||||
func (a *ArrayList) RemoveAt(index uint32) exceptions.Exception {
|
||||
if index >= a.used {
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
@@ -148,7 +148,7 @@ func (a *arrayListIterator) HasNext() bool {
|
||||
return a.index < a.arrayList.used
|
||||
}
|
||||
|
||||
func (a *arrayListIterator) Next() (interface{}, error) {
|
||||
func (a *arrayListIterator) Next() (interface{}, exceptions.Exception) {
|
||||
value, err := a.arrayList.Get(a.index)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -157,7 +157,7 @@ func (a *arrayListIterator) Next() (interface{}, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (a *arrayListIterator) Remove() error {
|
||||
func (a *arrayListIterator) Remove() exceptions.Exception {
|
||||
err := a.arrayList.RemoveAt(a.index - 1)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -24,7 +24,7 @@ type (
|
||||
|
||||
MutableIterator() MutableIterator
|
||||
Add(element interface{}) bool
|
||||
Remove(element interface{}) error
|
||||
Remove(element interface{}) exceptions.Exception
|
||||
AddAll(c Collection) bool
|
||||
RemoveAll(c Collection) bool
|
||||
RetainAll(c Collection) bool
|
||||
@@ -38,7 +38,7 @@ type (
|
||||
Contains(element interface{}) bool
|
||||
ContainsAll(c Collection) bool
|
||||
|
||||
Get(index uint32) (interface{}, error)
|
||||
Get(index uint32) (interface{}, exceptions.Exception)
|
||||
SubList(from, to uint32) List
|
||||
}
|
||||
|
||||
@@ -51,24 +51,24 @@ type (
|
||||
|
||||
MutableIterator() MutableIterator
|
||||
Add(element interface{}) bool
|
||||
Remove(element interface{}) error
|
||||
Remove(element interface{}) exceptions.Exception
|
||||
AddAll(c Collection) bool
|
||||
RemoveAll(c Collection) bool
|
||||
RetainAll(c Collection) bool
|
||||
Clear()
|
||||
|
||||
Get(index uint32) (interface{}, error)
|
||||
Get(index uint32) (interface{}, exceptions.Exception)
|
||||
SubList(from, to uint32) List
|
||||
|
||||
Set(index uint32, element interface{}) error
|
||||
Set(index uint32, element interface{}) exceptions.Exception
|
||||
AddAtIndex(index uint32, element interface{}) bool
|
||||
RemoveAt(index uint32) error
|
||||
RemoveAt(index uint32) exceptions.Exception
|
||||
SubMutableList(from, to uint32) MutableList
|
||||
}
|
||||
)
|
||||
|
||||
func Contains(l Collection, element interface{}) bool {
|
||||
return Loop(l, func(e interface{}) error {
|
||||
return Loop(l, func(e interface{}) exceptions.Exception {
|
||||
if e == element {
|
||||
return exceptions.ElementFound
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func Contains(l Collection, element interface{}) bool {
|
||||
}
|
||||
|
||||
func ContainsAll(l Collection, collection Collection) bool {
|
||||
return Loop(collection, func(e interface{}) error {
|
||||
return Loop(collection, func(e interface{}) exceptions.Exception {
|
||||
if l.Contains(e) {
|
||||
return nil
|
||||
} else {
|
||||
@@ -87,7 +87,7 @@ func ContainsAll(l Collection, collection Collection) bool {
|
||||
}
|
||||
|
||||
func AddAll(l MutableCollection, collection Collection) bool {
|
||||
return Loop(collection, func(e interface{}) error {
|
||||
return Loop(collection, func(e interface{}) exceptions.Exception {
|
||||
if !l.Add(e) {
|
||||
return exceptions.CollectionLoopFinished
|
||||
}
|
||||
@@ -96,13 +96,13 @@ func AddAll(l MutableCollection, collection Collection) bool {
|
||||
}
|
||||
|
||||
func RemoveAll(l MutableCollection, collection Collection) bool {
|
||||
return Loop(collection, func(e interface{}) error {
|
||||
return Loop(collection, func(e interface{}) exceptions.Exception {
|
||||
return l.Remove(e)
|
||||
}) == nil
|
||||
}
|
||||
|
||||
func RetainAll(l MutableCollection, collection Collection) bool {
|
||||
return LoopMutable(l, func(element interface{}, iterator MutableIterator) error {
|
||||
return LoopMutable(l, func(element interface{}, iterator MutableIterator) exceptions.Exception {
|
||||
if !collection.Contains(element) {
|
||||
return iterator.Remove()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import "github.com/tursom/GoCollections/exceptions"
|
||||
|
||||
type Iterator interface {
|
||||
HasNext() bool
|
||||
Next() (interface{}, error)
|
||||
Next() (interface{}, exceptions.Exception)
|
||||
}
|
||||
|
||||
type Iterable interface {
|
||||
@@ -13,8 +13,8 @@ type Iterable interface {
|
||||
|
||||
type MutableIterator interface {
|
||||
HasNext() bool
|
||||
Next() (interface{}, error)
|
||||
Remove() error
|
||||
Next() (interface{}, exceptions.Exception)
|
||||
Remove() exceptions.Exception
|
||||
}
|
||||
|
||||
type MutableIterable interface {
|
||||
@@ -22,11 +22,24 @@ type MutableIterable interface {
|
||||
MutableIterator() MutableIterator
|
||||
}
|
||||
|
||||
func Loop(iterable Iterable, f func(element interface{}) error) error {
|
||||
if f == nil {
|
||||
func Loop(iterable Iterable, f func(element interface{}) exceptions.Exception) exceptions.Exception {
|
||||
if f == nil || iterable == nil {
|
||||
return exceptions.NewNPE("", true)
|
||||
}
|
||||
return LoopIterator(iterable.Iterator(), f)
|
||||
}
|
||||
|
||||
func LoopMutable(iterable MutableIterable, f func(element interface{}, iterator MutableIterator) (err exceptions.Exception)) exceptions.Exception {
|
||||
if f == nil || iterable == nil {
|
||||
return exceptions.NewNPE("", true)
|
||||
}
|
||||
return LoopMutableIterator(iterable.MutableIterator(), f)
|
||||
}
|
||||
|
||||
func LoopIterator(iterator Iterator, f func(element interface{}) exceptions.Exception) exceptions.Exception {
|
||||
if f == nil || iterator == nil {
|
||||
return exceptions.NewNPE("", true)
|
||||
}
|
||||
iterator := iterable.Iterator()
|
||||
for iterator.HasNext() {
|
||||
next, err := iterator.Next()
|
||||
if err != nil {
|
||||
@@ -40,11 +53,10 @@ func Loop(iterable Iterable, f func(element interface{}) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoopMutable(iterable MutableIterable, f func(element interface{}, iterator MutableIterator) (err error)) error {
|
||||
if f == nil {
|
||||
return nil
|
||||
func LoopMutableIterator(iterator MutableIterator, f func(element interface{}, iterator MutableIterator) (err exceptions.Exception)) exceptions.Exception {
|
||||
if f == nil || iterator == nil {
|
||||
return exceptions.NewNPE("", true)
|
||||
}
|
||||
iterator := iterable.MutableIterator()
|
||||
for iterator.HasNext() {
|
||||
next, err := iterator.Next()
|
||||
if err != nil {
|
||||
|
||||
@@ -50,8 +50,8 @@ func (l *LinkedList) Add(element interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *LinkedList) Remove(element interface{}) error {
|
||||
err := LoopMutable(l, func(e interface{}, iterator MutableIterator) error {
|
||||
func (l *LinkedList) Remove(element interface{}) exceptions.Exception {
|
||||
err := LoopMutable(l, func(e interface{}, iterator MutableIterator) exceptions.Exception {
|
||||
if element == e {
|
||||
iterator.Remove()
|
||||
return exceptions.CollectionLoopFinished
|
||||
@@ -86,7 +86,7 @@ func (l LinkedList) SubList(from, to uint32) List {
|
||||
return NewSubList(l, from, to)
|
||||
}
|
||||
|
||||
func (l *LinkedList) Set(index uint32, element interface{}) error {
|
||||
func (l *LinkedList) Set(index uint32, element interface{}) exceptions.Exception {
|
||||
node := l.head
|
||||
for node != l.head {
|
||||
if index == 0 {
|
||||
@@ -112,7 +112,7 @@ func (l *LinkedList) AddAtIndex(index uint32, element interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *LinkedList) RemoveAt(index uint32) error {
|
||||
func (l *LinkedList) RemoveAt(index uint32) exceptions.Exception {
|
||||
node := l.head
|
||||
for node != l.head {
|
||||
if index == 0 {
|
||||
@@ -129,7 +129,7 @@ func (l *LinkedList) SubMutableList(from, to uint32) MutableList {
|
||||
return NewMutableSubList(l, from, to)
|
||||
}
|
||||
|
||||
func (l LinkedList) Get(index uint32) (interface{}, error) {
|
||||
func (l LinkedList) Get(index uint32) (interface{}, exceptions.Exception) {
|
||||
node := l.head
|
||||
for node != l.head {
|
||||
if index == 0 {
|
||||
@@ -158,7 +158,7 @@ func (l *linkedListIterator) HasNext() bool {
|
||||
return l.node != l.head
|
||||
}
|
||||
|
||||
func (l *linkedListIterator) Next() (interface{}, error) {
|
||||
func (l *linkedListIterator) Next() (interface{}, exceptions.Exception) {
|
||||
if l.node == l.head {
|
||||
return nil, exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func (l *linkedListIterator) Next() (interface{}, error) {
|
||||
return l.node.prev.value, nil
|
||||
}
|
||||
|
||||
func (l *linkedListIterator) Remove() error {
|
||||
func (l *linkedListIterator) Remove() exceptions.Exception {
|
||||
if l.node.prev == l.head {
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func (s *MutableSubList) ContainsAll(c Collection) bool {
|
||||
return ContainsAll(s, c)
|
||||
}
|
||||
|
||||
func (s *MutableSubList) Get(index uint32) (interface{}, error) {
|
||||
func (s *MutableSubList) Get(index uint32) (interface{}, exceptions.Exception) {
|
||||
return s.list.Get(index + s.from)
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func (s *MutableSubList) Add(_ interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *MutableSubList) Remove(element interface{}) error {
|
||||
func (s *MutableSubList) Remove(element interface{}) exceptions.Exception {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func (s *MutableSubList) RetainAll(_ Collection) bool {
|
||||
func (s *MutableSubList) Clear() {
|
||||
}
|
||||
|
||||
func (s *MutableSubList) Set(index uint32, element interface{}) error {
|
||||
func (s *MutableSubList) Set(index uint32, element interface{}) exceptions.Exception {
|
||||
if index >= s.to-s.from {
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
@@ -84,7 +84,7 @@ func (s *MutableSubList) AddAtIndex(_ uint32, _ interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *MutableSubList) RemoveAt(index uint32) error {
|
||||
func (s *MutableSubList) RemoveAt(index uint32) exceptions.Exception {
|
||||
return exceptions.NewOperationNotSupportedException("", true)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package collections
|
||||
|
||||
import "github.com/tursom/GoCollections/exceptions"
|
||||
|
||||
type SubList struct {
|
||||
list List
|
||||
from, to uint32
|
||||
@@ -36,7 +38,7 @@ func (s *SubList) ContainsAll(c Collection) bool {
|
||||
return ContainsAll(s, c)
|
||||
}
|
||||
|
||||
func (s *SubList) Get(index uint32) (interface{}, error) {
|
||||
func (s *SubList) Get(index uint32) (interface{}, exceptions.Exception) {
|
||||
return s.list.Get(index + s.from)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user