diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/util')
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/util/History.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/util/History.java b/src/main/java/cuchaz/enigma/gui/util/History.java new file mode 100644 index 0000000..94f3105 --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/util/History.java | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | package cuchaz.enigma.gui.util; | ||
| 2 | |||
| 3 | import com.google.common.collect.Queues; | ||
| 4 | |||
| 5 | import java.util.Deque; | ||
| 6 | |||
| 7 | public class History<T> { | ||
| 8 | private final Deque<T> previous = Queues.newArrayDeque(); | ||
| 9 | private final Deque<T> next = Queues.newArrayDeque(); | ||
| 10 | private T current; | ||
| 11 | |||
| 12 | public History(T initial) { | ||
| 13 | current = initial; | ||
| 14 | } | ||
| 15 | |||
| 16 | public T getCurrent() { | ||
| 17 | return current; | ||
| 18 | } | ||
| 19 | |||
| 20 | public void push(T value) { | ||
| 21 | previous.addLast(current); | ||
| 22 | current = value; | ||
| 23 | next.clear(); | ||
| 24 | } | ||
| 25 | |||
| 26 | public void replace(T value) { | ||
| 27 | current = value; | ||
| 28 | } | ||
| 29 | |||
| 30 | public boolean canGoBack() { | ||
| 31 | return !previous.isEmpty(); | ||
| 32 | } | ||
| 33 | |||
| 34 | public T goBack() { | ||
| 35 | next.addFirst(current); | ||
| 36 | current = previous.removeLast(); | ||
| 37 | return current; | ||
| 38 | } | ||
| 39 | |||
| 40 | public boolean canGoForward() { | ||
| 41 | return !next.isEmpty(); | ||
| 42 | } | ||
| 43 | |||
| 44 | public T goForward() { | ||
| 45 | previous.addLast(current); | ||
| 46 | current = next.removeFirst(); | ||
| 47 | return current; | ||
| 48 | } | ||
| 49 | } | ||