From 569bc350904ce54a0351d974cc29ff47f1460c42 Mon Sep 17 00:00:00 2001 From: tursom Date: Fri, 21 May 2021 16:57:33 +0800 Subject: [PATCH] update Exception --- collections/ArrayList.go | 12 +++++------ collections/Collection.go | 22 ++++++++++---------- collections/Iterable.go | 32 ++++++++++++++++++++---------- collections/LinkedList.go | 14 ++++++------- collections/MutableSubList.go | 8 ++++---- collections/SubList.go | 4 +++- exceptions/CollectionOperations.go | 27 +++---------------------- exceptions/Exception.go | 6 +++--- main.go | 13 ++++-------- 9 files changed, 63 insertions(+), 75 deletions(-) diff --git a/collections/ArrayList.go b/collections/ArrayList.go index fd98e62..f3b5273 100644 --- a/collections/ArrayList.go +++ b/collections/ArrayList.go @@ -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 diff --git a/collections/Collection.go b/collections/Collection.go index 5a7a070..e0f97dc 100644 --- a/collections/Collection.go +++ b/collections/Collection.go @@ -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() } diff --git a/collections/Iterable.go b/collections/Iterable.go index b33dc4f..383b31e 100644 --- a/collections/Iterable.go +++ b/collections/Iterable.go @@ -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 { diff --git a/collections/LinkedList.go b/collections/LinkedList.go index e65127f..5f6752c 100644 --- a/collections/LinkedList.go +++ b/collections/LinkedList.go @@ -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) } diff --git a/collections/MutableSubList.go b/collections/MutableSubList.go index 04057c4..3a477c4 100644 --- a/collections/MutableSubList.go +++ b/collections/MutableSubList.go @@ -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) } diff --git a/collections/SubList.go b/collections/SubList.go index 061e88a..9667b96 100644 --- a/collections/SubList.go +++ b/collections/SubList.go @@ -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) } diff --git a/exceptions/CollectionOperations.go b/exceptions/CollectionOperations.go index 6cd737c..277dad3 100644 --- a/exceptions/CollectionOperations.go +++ b/exceptions/CollectionOperations.go @@ -1,26 +1,5 @@ package exceptions -var ElementFound = ElementFoundThrowable{} -var ElementNotFound = ElementFoundThrowable{} -var CollectionLoopFinished = CollectionLoop{} - -type ElementFoundThrowable struct { -} - -func (e ElementFoundThrowable) Error() string { - return "element found" -} - -type ElementNotFoundThrowable struct { -} - -func (e ElementNotFoundThrowable) Error() string { - return "element not found" -} - -type CollectionLoop struct { -} - -func (e CollectionLoop) Error() string { - return "collection loop finished" -} +var ElementFound = NewRuntimeException("", "element found", false, nil) +var ElementNotFound = NewRuntimeException("", "element not found", false, nil) +var CollectionLoopFinished = NewRuntimeException("", "collection loop finished", false, nil) diff --git a/exceptions/Exception.go b/exceptions/Exception.go index e811a04..d308767 100644 --- a/exceptions/Exception.go +++ b/exceptions/Exception.go @@ -56,9 +56,9 @@ func BuildStackTrace(builder *strings.Builder, e Exception, exceptionMsg string) } func Try( - f func() (ret interface{}, err error), - catch func(panic interface{}) (ret interface{}, err error), -) (ret interface{}, err error) { + f func() (ret interface{}, err Exception), + catch func(panic interface{}) (ret interface{}, err Exception), +) (ret interface{}, err Exception) { defer func() { if r := recover(); r != nil { ret, err = catch(r) diff --git a/main.go b/main.go index 386f81f..aaa3b6e 100644 --- a/main.go +++ b/main.go @@ -7,18 +7,13 @@ import ( ) func main() { - _, err := exceptions.Try(func() (interface{}, error) { + _, err := exceptions.Try(func() (interface{}, exceptions.Exception) { panic("test") - }, func(r interface{}) (interface{}, error) { + }, func(r interface{}) (interface{}, exceptions.Exception) { fmt.Println("recover from panic", r) return nil, exceptions.NewIndexOutOfBound(fmt.Sprint(r), true) }) - exceptions.NewRuntimeException( - err, - "test exception:", - true, - err, - ).PrintStackTrace() + exceptions.Print(err) list := collections.NewArrayList() fmt.Println(list) @@ -27,7 +22,7 @@ func main() { //fmt.Println(list) } - _ = collections.LoopMutable(list, func(element interface{}, iterator collections.MutableIterator) (err error) { + _ = collections.LoopMutable(list, func(element interface{}, iterator collections.MutableIterator) (err exceptions.Exception) { if element.(int)&1 == 0 { err = iterator.Remove() }