mirror of
https://github.com/tursom/GoCollections.git
synced 2024-12-27 17:10:25 +08:00
update project
This commit is contained in:
parent
e870487949
commit
0fca6a7ca3
@ -14,6 +14,10 @@ func NewArrayList() *ArrayList {
|
||||
}
|
||||
}
|
||||
|
||||
func (a ArrayList) String() string {
|
||||
return String(a)
|
||||
}
|
||||
|
||||
func (a ArrayList) Iterator() Iterator {
|
||||
return a.MutableIterator()
|
||||
}
|
||||
@ -34,10 +38,6 @@ func (a ArrayList) ContainsAll(c Collection) bool {
|
||||
return ContainsAll(a, c)
|
||||
}
|
||||
|
||||
func (a *ArrayList) MutableIterator() MutableIterator {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a *ArrayList) Add(element interface{}) bool {
|
||||
if a.used >= uint32(len(a.array)) {
|
||||
oldArray := a.array
|
||||
@ -59,24 +59,12 @@ func (a ArrayList) IndexOf(element interface{}) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func (a *ArrayList) RemoveIndex(index uint32) error {
|
||||
if index >= a.used {
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
|
||||
array := a.array
|
||||
for i := index + 1; i < a.used; i++ {
|
||||
array[index-1] = array[index]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ArrayList) Remove(element interface{}) error {
|
||||
index := a.IndexOf(element)
|
||||
if index < 0 {
|
||||
return exceptions.NewElementNotFoundException("", true)
|
||||
} else {
|
||||
return a.RemoveIndex(uint32(index))
|
||||
return a.RemoveAt(uint32(index))
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,13 +105,63 @@ func (a *ArrayList) Set(index uint32, element interface{}) error {
|
||||
}
|
||||
|
||||
func (a *ArrayList) AddAtIndex(index uint32, element interface{}) bool {
|
||||
panic("implement me")
|
||||
if !a.Add(element) {
|
||||
return false
|
||||
}
|
||||
|
||||
array := a.array
|
||||
for i := a.used - 1; i > index; i++ {
|
||||
array[i] = array[i-1]
|
||||
}
|
||||
array[index] = element
|
||||
a.used++
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *ArrayList) RemoveAt(index uint32) bool {
|
||||
panic("implement me")
|
||||
func (a *ArrayList) RemoveAt(index uint32) error {
|
||||
if index >= a.used {
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
|
||||
array := a.array
|
||||
for i := index + 1; i < a.used; i++ {
|
||||
array[i-1] = array[i]
|
||||
}
|
||||
a.used--
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ArrayList) SubMutableList(from, to uint32) MutableList {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a *ArrayList) MutableIterator() MutableIterator {
|
||||
return &arrayListIterator{a, 0}
|
||||
}
|
||||
|
||||
type arrayListIterator struct {
|
||||
arrayList *ArrayList
|
||||
index uint32
|
||||
}
|
||||
|
||||
func (a *arrayListIterator) HasNext() bool {
|
||||
return a.index < a.arrayList.used
|
||||
}
|
||||
|
||||
func (a *arrayListIterator) Next() (interface{}, error) {
|
||||
value, err := a.arrayList.Get(a.index)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.index++
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (a *arrayListIterator) Remove() error {
|
||||
err := a.arrayList.RemoveAt(a.index - 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.index--
|
||||
return nil
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ type (
|
||||
|
||||
Set(index uint32, element interface{}) error
|
||||
AddAtIndex(index uint32, element interface{}) bool
|
||||
RemoveAt(index uint32) bool
|
||||
RemoveAt(index uint32) error
|
||||
SubMutableList(from, to uint32) MutableList
|
||||
}
|
||||
)
|
||||
@ -102,13 +102,12 @@ func RemoveAll(l MutableCollection, collection Collection) bool {
|
||||
}
|
||||
|
||||
func RetainAll(l MutableCollection, collection Collection) bool {
|
||||
_ = LoopMutable(l, func(element interface{}, iterator MutableIterator) error {
|
||||
return LoopMutable(l, func(element interface{}, iterator MutableIterator) error {
|
||||
if !collection.Contains(element) {
|
||||
iterator.Remove()
|
||||
return iterator.Remove()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return true
|
||||
}) == nil
|
||||
}
|
||||
|
||||
func String(l List) string {
|
||||
|
@ -12,7 +12,7 @@ type Iterable interface {
|
||||
type MutableIterator interface {
|
||||
HasNext() bool
|
||||
Next() (interface{}, error)
|
||||
Remove() bool
|
||||
Remove() error
|
||||
}
|
||||
|
||||
type MutableIterable interface {
|
||||
@ -38,7 +38,7 @@ func Loop(iterable Iterable, f func(element interface{}) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoopMutable(iterable MutableIterable, f func(element interface{}, iterator MutableIterator) error) error {
|
||||
func LoopMutable(iterable MutableIterable, f func(element interface{}, iterator MutableIterator) (err error)) error {
|
||||
if f == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -112,17 +112,17 @@ func (l *LinkedList) AddAtIndex(index uint32, element interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *LinkedList) RemoveAt(index uint32) bool {
|
||||
func (l *LinkedList) RemoveAt(index uint32) error {
|
||||
node := l.head
|
||||
for node != l.head {
|
||||
if index == 0 {
|
||||
l.size--
|
||||
node.remove()
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
index--
|
||||
}
|
||||
return false
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
|
||||
func (l *LinkedList) SubMutableList(from, to uint32) MutableList {
|
||||
@ -166,13 +166,13 @@ func (l *linkedListIterator) Next() (interface{}, error) {
|
||||
return l.node.prev.value, nil
|
||||
}
|
||||
|
||||
func (l *linkedListIterator) Remove() bool {
|
||||
func (l *linkedListIterator) Remove() error {
|
||||
if l.node.prev == l.head {
|
||||
return false
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}
|
||||
l.node.prev.remove()
|
||||
l.list.size--
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *linkedListNode) remove() {
|
||||
|
@ -84,8 +84,8 @@ func (s *MutableSubList) AddAtIndex(_ uint32, _ interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *MutableSubList) RemoveAt(_ uint32) bool {
|
||||
return false
|
||||
func (s *MutableSubList) RemoveAt(index uint32) error {
|
||||
return exceptions.NewOperationNotSupportedException("", true)
|
||||
}
|
||||
|
||||
func (s *MutableSubList) SubMutableList(from, to uint32) MutableList {
|
||||
|
@ -1,42 +1,11 @@
|
||||
package exceptions
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ElementNotFoundException struct {
|
||||
message string
|
||||
stackTrace []StackTrace
|
||||
RuntimeException
|
||||
}
|
||||
|
||||
func NewElementNotFoundException(message string, getStackTrace bool) *ElementNotFoundException {
|
||||
var stackTrace []StackTrace = nil
|
||||
if getStackTrace {
|
||||
stackTrace = GetStackTrace()
|
||||
}
|
||||
return &ElementNotFoundException{
|
||||
message: message,
|
||||
stackTrace: stackTrace,
|
||||
NewRuntimeException(message, "exception caused ElementNotFoundException:", getStackTrace),
|
||||
}
|
||||
}
|
||||
|
||||
func (e ElementNotFoundException) Error() string {
|
||||
if len(e.message) == 0 {
|
||||
return "element not found"
|
||||
} else {
|
||||
return e.message
|
||||
}
|
||||
}
|
||||
|
||||
func (e ElementNotFoundException) StackTrace() []StackTrace {
|
||||
return e.stackTrace
|
||||
}
|
||||
|
||||
func (e ElementNotFoundException) PrintStackTrace() {
|
||||
e.PrintStackTraceTo(os.Stderr)
|
||||
}
|
||||
|
||||
func (e ElementNotFoundException) PrintStackTraceTo(writer io.Writer) {
|
||||
PrintStackTrace(writer, e, "exception caused ElementNotFoundException:")
|
||||
}
|
||||
|
@ -25,15 +25,15 @@ func PrintStackTrace(writer io.Writer, e Exception, exceptionMsg string) {
|
||||
}
|
||||
}
|
||||
|
||||
func Try(f func() (interface{}, error), catch func(interface{}) interface{}) (ret interface{}) {
|
||||
func Try(f func() (ret interface{}, err error), catch func(interface{}) (ret interface{}, err error)) (ret interface{}, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ret = catch(r)
|
||||
ret, err = catch(r)
|
||||
}
|
||||
}()
|
||||
ret, err := f()
|
||||
ret, err = f()
|
||||
if err != nil {
|
||||
ret = catch(err)
|
||||
ret, err = catch(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -1,42 +1,11 @@
|
||||
package exceptions
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type IndexOutOfBound struct {
|
||||
message string
|
||||
stackTrace []StackTrace
|
||||
RuntimeException
|
||||
}
|
||||
|
||||
func NewIndexOutOfBound(message string, getStackTrace bool) IndexOutOfBound {
|
||||
var stackTrace []StackTrace = nil
|
||||
if getStackTrace {
|
||||
stackTrace = GetStackTrace()
|
||||
}
|
||||
return IndexOutOfBound{
|
||||
message: message,
|
||||
stackTrace: stackTrace,
|
||||
}
|
||||
}
|
||||
|
||||
func (i IndexOutOfBound) StackTrace() []StackTrace {
|
||||
return i.stackTrace
|
||||
}
|
||||
|
||||
func (i IndexOutOfBound) PrintStackTrace() {
|
||||
i.PrintStackTraceTo(os.Stderr)
|
||||
}
|
||||
|
||||
func (i IndexOutOfBound) PrintStackTraceTo(writer io.Writer) {
|
||||
PrintStackTrace(writer, i, "exception caused IndexOutOfBound:")
|
||||
}
|
||||
|
||||
func (i IndexOutOfBound) Error() string {
|
||||
if len(i.message) == 0 {
|
||||
return "index out of bound"
|
||||
} else {
|
||||
return i.message
|
||||
NewRuntimeException(message, "exception caused IndexOutOfBound:", getStackTrace),
|
||||
}
|
||||
}
|
||||
|
11
exceptions/OperationNotSupportedException.go
Normal file
11
exceptions/OperationNotSupportedException.go
Normal file
@ -0,0 +1,11 @@
|
||||
package exceptions
|
||||
|
||||
type OperationNotSupportedException struct {
|
||||
RuntimeException
|
||||
}
|
||||
|
||||
func NewOperationNotSupportedException(message string, getStackTrace bool) OperationNotSupportedException {
|
||||
return OperationNotSupportedException{
|
||||
NewRuntimeException(message, "exception caused OperationNotSupportedException:", getStackTrace),
|
||||
}
|
||||
}
|
49
exceptions/RuntimeException.go
Normal file
49
exceptions/RuntimeException.go
Normal file
@ -0,0 +1,49 @@
|
||||
package exceptions
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type RuntimeException struct {
|
||||
message string
|
||||
exceptionMessage string
|
||||
stackTrace []StackTrace
|
||||
}
|
||||
|
||||
func NewRuntimeException(message, exceptionMessage string, getStackTrace bool) RuntimeException {
|
||||
var stackTrace []StackTrace = nil
|
||||
if getStackTrace {
|
||||
stackTrace = GetStackTrace()
|
||||
}
|
||||
|
||||
if len(exceptionMessage) == 0 {
|
||||
exceptionMessage = "exception caused:"
|
||||
}
|
||||
|
||||
return RuntimeException{
|
||||
message: message,
|
||||
exceptionMessage: exceptionMessage,
|
||||
stackTrace: stackTrace,
|
||||
}
|
||||
}
|
||||
|
||||
func (o RuntimeException) Error() string {
|
||||
if len(o.message) == 0 {
|
||||
return "index out of bound"
|
||||
} else {
|
||||
return o.message
|
||||
}
|
||||
}
|
||||
|
||||
func (o RuntimeException) StackTrace() []StackTrace {
|
||||
return o.stackTrace
|
||||
}
|
||||
|
||||
func (o RuntimeException) PrintStackTrace() {
|
||||
o.PrintStackTraceTo(os.Stderr)
|
||||
}
|
||||
|
||||
func (o RuntimeException) PrintStackTraceTo(writer io.Writer) {
|
||||
PrintStackTrace(writer, o, o.exceptionMessage)
|
||||
}
|
17
main.go
17
main.go
@ -7,26 +7,27 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(exceptions.Try(func() (interface{}, error) {
|
||||
_, err := exceptions.Try(func() (interface{}, error) {
|
||||
panic("test")
|
||||
}, func(r interface{}) interface{} {
|
||||
}, func(r interface{}) (interface{}, error) {
|
||||
fmt.Println("recover from panic", r)
|
||||
return exceptions.NewIndexOutOfBound("", true)
|
||||
}))
|
||||
return nil, exceptions.NewIndexOutOfBound(fmt.Sprint(r), true)
|
||||
})
|
||||
err.(exceptions.Exception).PrintStackTrace()
|
||||
|
||||
list := collections.NewLinkedList()
|
||||
list := collections.NewArrayList()
|
||||
fmt.Println(list)
|
||||
for i := 0; i < 20; i++ {
|
||||
list.Add(i)
|
||||
//fmt.Println(list)
|
||||
}
|
||||
|
||||
_ = collections.LoopMutable(list, func(element interface{}, iterator collections.MutableIterator) error {
|
||||
_ = collections.LoopMutable(list, func(element interface{}, iterator collections.MutableIterator) (err error) {
|
||||
if element.(int)&1 == 0 {
|
||||
iterator.Remove()
|
||||
err = iterator.Remove()
|
||||
}
|
||||
fmt.Println(list)
|
||||
return nil
|
||||
return
|
||||
})
|
||||
//for i := 0; i < 10; i++ {
|
||||
// list.Remove(i * 2)
|
||||
|
Loading…
Reference in New Issue
Block a user