mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 09:43:57 +00:00
745f112ef6
Opt an initial batch of files under samples, samples-dev, utils, and runtime/tools/dartfuzz out of null safety in preparation for switching the flag on by default. Change-Id: Icdfd52a5a969e678a7205903332f73fe3841c223 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166960 Reviewed-by: Daco Harkes <dacoharkes@google.com> Commit-Queue: Leaf Petersen <leafp@google.com>
71 lines
1.6 KiB
Dart
71 lines
1.6 KiB
Dart
// @dart = 2.9
|
|
|
|
part of swarmlib;
|
|
|
|
/**
|
|
* An iterator that allows the user to move forward and backward though
|
|
* a set of items. (Bi-directional)
|
|
*/
|
|
class BiIterator<E> {
|
|
/**
|
|
* Provides forward and backward iterator functionality to keep track
|
|
* which item is currently selected.
|
|
*/
|
|
ObservableValue<int> currentIndex;
|
|
|
|
/**
|
|
* The collection of items we will be iterating through.
|
|
*/
|
|
List<E> list;
|
|
|
|
BiIterator(this.list, [List<ChangeListener> oldListeners = null])
|
|
: currentIndex = new ObservableValue<int>(0) {
|
|
if (oldListeners != null) {
|
|
currentIndex.listeners = oldListeners;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the next section from the sections, given the current
|
|
* position. Returns the last source if there is no next section.
|
|
*/
|
|
E next() {
|
|
if (currentIndex.value < list.length - 1) {
|
|
currentIndex.value += 1;
|
|
}
|
|
return list[currentIndex.value];
|
|
}
|
|
|
|
/**
|
|
* Returns the current Section (page in the UI) that the user is
|
|
* looking at.
|
|
*/
|
|
E get current {
|
|
return list[currentIndex.value];
|
|
}
|
|
|
|
/**
|
|
* Returns the previous section from the sections, given the current
|
|
* position. Returns the front section if we are already at the front of
|
|
* the list.
|
|
*/
|
|
E previous() {
|
|
if (currentIndex.value > 0) {
|
|
currentIndex.value -= 1;
|
|
}
|
|
return list[currentIndex.value];
|
|
}
|
|
|
|
/**
|
|
* Move the iterator pointer over so that it points to a given list item.
|
|
*/
|
|
void jumpToValue(E val) {
|
|
for (int i = 0; i < list.length; i++) {
|
|
if (identical(list[i], val)) {
|
|
currentIndex.value = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|