GoCollections/collections/SubList.go

52 lines
1.0 KiB
Go
Raw Normal View History

2021-05-20 17:32:35 +08:00
package collections
2022-03-21 11:02:41 +08:00
import (
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
)
2021-05-21 16:57:33 +08:00
2022-03-21 11:02:41 +08:00
type SubList[T lang.Object] struct {
2022-03-23 10:15:18 +08:00
lang.BaseObject
2022-03-21 11:02:41 +08:00
list List[T]
from, to int
2021-05-20 17:32:35 +08:00
}
2022-03-23 10:15:18 +08:00
func NewSubList[T lang.Object](list List[T], from, to int) *SubList[T] {
return &SubList[T]{lang.NewBaseObject(), list, from, to}
2021-05-20 17:32:35 +08:00
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) Iterator() Iterator[T] {
2021-05-20 17:32:35 +08:00
iterator := s.list.Iterator()
for i := 0; i < int(s.from); i++ {
2021-05-21 09:41:58 +08:00
_, err := iterator.Next()
if err != nil {
return nil
}
2021-05-20 17:32:35 +08:00
}
return iterator
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) Size() int {
2021-05-20 17:32:35 +08:00
return s.to - s.from
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) IsEmpty() bool {
2021-05-20 17:32:35 +08:00
return s.Size() == 0
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) Contains(element T) bool {
return Contains[T](s, element)
2021-05-20 17:32:35 +08:00
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) ContainsAll(c Collection[T]) bool {
return ContainsAll[T](s, c)
2021-05-20 17:32:35 +08:00
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) Get(index int) (T, exceptions.Exception) {
2021-05-20 17:32:35 +08:00
return s.list.Get(index + s.from)
}
2022-03-21 11:02:41 +08:00
func (s *SubList[T]) SubList(from, to int) List[T] {
return NewSubList[T](s, from, to)
2021-05-20 17:32:35 +08:00
}