SinglyLinkedList implements PStack<E> { @Override public E peek() { return element; } private  SinglyLinkedList(E element); private  SinglyLinkedList(E element, SinglyLinkedList<E> next); @Override PStack<E> push(E e); @Override E peek(); @Override E peek(int i); @Override PStack<E> pop(); @Override boolean isEmpty(); @Override void forEach(Consumer<E> action); @Override boolean anyMatch(Predicate<E> predicate); @Override int size(); @Override int hashCode(); @Override String toString(); @Override boolean equals(Object obj);  }
@Test void empty_peek() { PStack<Object> stack = PCollections.emptyStack(); assertThrows(IllegalStateException.class, () -> stack.peek()); }
@Test void peek() { PStack<Object> emptyStack = PCollections.emptyStack(); assertThatThrownBy(() -> emptyStack.peek(0)).isInstanceOf(IllegalStateException.class); Object a = new Object(); PStack<Object> s = PCollections.emptyStack().push(a); assertThat(s.peek(0)).isEqualTo(s.peek()); Object b = new Object(); s = s.push(b); assertThat(s.peek(1)).isEqualTo(a); PStack<Object> finalS = s; assertThatThrownBy(() -> finalS.peek(2)).isInstanceOf(IllegalStateException.class); }