aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iterator/iterator.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2022-10-20 16:43:40 +0200
committerRobin Jarry <robin@jarry.cc>2022-10-27 22:44:39 +0200
commitc83ffabf3853e5f06294bba78dc23bc7ff84b0af (patch)
treecfebeb1cd345526ec10eb93bce00e2630106e9d2 /lib/iterator/iterator.go
parentf20933f51220e1946ccecd16e0ebfda05aa2b5cb (diff)
downloadaerc-c83ffabf3853e5f06294bba78dc23bc7ff84b0af.tar.gz
iterator: implement iterators over uid/thread data
Implement an iterator framework for uid and thread data. Iterators ensure that the underlying data is accessed in the right order and provide an abstraction of the index handling. Iterators should be used when the order of the uids/threads is important. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/iterator/iterator.go')
-rw-r--r--lib/iterator/iterator.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/iterator/iterator.go b/lib/iterator/iterator.go
new file mode 100644
index 00000000..28a9b8b3
--- /dev/null
+++ b/lib/iterator/iterator.go
@@ -0,0 +1,35 @@
+package iterator
+
+// Factory is the interface that wraps the NewIterator method. The
+// NewIterator() creates either UID or thread iterators and ensures that both
+// types of iterators implement the same iteration direction.
+type Factory interface {
+ NewIterator(a interface{}) Iterator
+}
+
+// Iterator implements an interface for iterating over UID or thread data. If
+// Next() returns true, the current value of the iterator can be read with
+// Value(). The return value of Value() is an interface{} type which needs to
+// be cast to the correct type.
+//
+// The iterators are implemented such that the first returned value always
+// represents the top message in the message list. Hence, StartIndex() would
+// return the index of the top message whereas EndIndex() returns the index of
+// message at the bottom of the list.
+type Iterator interface {
+ Next() bool
+ Value() interface{}
+ StartIndex() int
+ EndIndex() int
+}
+
+// NewFactory creates an iterator factory. When reverse is true, the iterators
+// are reversed in the sense that the lowest UID messages are displayed at the
+// top of the message list. Otherwise, the default order is with the highest
+// UID message on top.
+func NewFactory(reverse bool) Factory {
+ if reverse {
+ return &reverseFactory{}
+ }
+ return &defaultFactory{}
+}