Permalink
Browse files

Docs and annotations

  • Loading branch information...
1 parent b9bd981 commit aea16de1060f196e80e0d2b452c4bcc818fe960b @mikera committed Dec 27, 2012
Showing with 35 additions and 11 deletions.
  1. +35 −11 src/main/java/mikera/util/LinkedStack.java
View
46 src/main/java/mikera/util/LinkedStack.java
@@ -6,20 +6,27 @@
import java.util.Queue;
import java.util.concurrent.atomic.AtomicReference;
+/**
+ * A stack implemented as a linked list.
+ * @author Mike
+ * @param <T> Type of object held in the stack
+ */
public class LinkedStack<T> implements Queue<T> {
-AtomicReference<Node<T>> topOfStack=new AtomicReference<Node<T>>();
+ AtomicReference<Node<T>> topOfStack=new AtomicReference<Node<T>>();
-
-private static final class Node<T> {
- final T object;
- final Node<T> next;
-
- private Node (T object, Node<T> next) {
- this.object=object;
- this.next=next;
+ /**
+ * Private inner class used to represent linked list nodes
+ */
+ private static final class Node<T> {
+ final T object;
+ final Node<T> next;
+
+ private Node (T object, Node<T> next) {
+ this.object=object;
+ this.next=next;
+ }
}
-}
private static final class StackIterator<T> implements Iterator<T> {
Node<T> head;
@@ -43,6 +50,7 @@ public void remove() {
}
}
+ @Override
public int size() {
Node<T> head=topOfStack.get();
int result=0;
@@ -53,6 +61,7 @@ public int size() {
return result;
}
+ @Override
public boolean isEmpty() {
return (topOfStack.get()==null);
}
@@ -68,7 +77,7 @@ public int search(Object o) {
return -1;
}
-
+ @Override
public boolean contains(Object o) {
Node<T> head=topOfStack.get();
while (head!=null) {
@@ -78,43 +87,53 @@ public boolean contains(Object o) {
return false;
}
+ @Override
public Iterator<T> iterator() {
return new StackIterator<T>(topOfStack.get());
}
+ @Override
public Object[] toArray() {
throw new UnsupportedOperationException();
}
@SuppressWarnings("hiding")
+ @Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
+ @Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
+ @Override
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
+ @Override
public boolean addAll(Collection<? extends T> c) {
throw new UnsupportedOperationException();
}
+ @Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
+ @Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
+ @Override
public void clear() {
topOfStack=null;
}
+ @Override
public boolean add(T e) {
while(true) {
Node<T> oldTop=topOfStack.get();
@@ -124,6 +143,7 @@ public boolean add(T e) {
return true;
}
+ @Override
public T remove() {
while(true) {
Node<T> oldTop=topOfStack.get();
@@ -134,10 +154,12 @@ public T remove() {
}
+ @Override
public boolean offer(T e) {
return add(e);
}
+ @Override
public T poll() {
while(true) {
Node<T> oldTop=topOfStack.get();
@@ -147,12 +169,14 @@ public T poll() {
}
}
+ @Override
public T element() {
Node<T> top=topOfStack.get();
if (top==null) throw new NoSuchElementException();
return top.object;
}
+ @Override
public T peek() {
Node<T> top=topOfStack.get();
if (top==null) return null;

0 comments on commit aea16de

Please sign in to comment.