import java.util.*; // source: http://people.inf.elte.hu/pgj/fny2_msc/fny2_msc_20160407.pdf, pages 4--5 public class ZipList { private List left, right; public ZipList(List list) { left = new LinkedList<>(); right = new LinkedList<>(list); } public void move(Direction dir) { switch (dir) { case LEFT : { if (!left.isEmpty()) { T first = left.get(0); left.remove(0); right.add(0, first); } else throw new NoMoreElementException("Focus is at the beginning of the list."); break; } case RIGHT : { if (right.size() > 1) { T first = right.get(0); right.remove(0); left.add(0, first); } else throw new NoMoreElementException("Focus is at the end of the list."); break; } } } public T get() throws EmptyFocusException { if (!right.isEmpty()) return right.get(0); else throw new EmptyFocusException("The ZipList is empty."); } public void put(T elem) { right.add(0, elem); } public List toList() { List result = new LinkedList<>(left); Collections.reverse(result); result.addAll(right); return result; } }