com.ajjpj.abase.collection.mutable.ArrayStack Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
package com.ajjpj.abase.collection.mutable; import com.ajjpj.abase.collection.ACollectionHelper; import com.ajjpj.abase.collection.immutable.AOption; import java.util.Iterator; import java.util.NoSuchElementException; /** * This is a mutable array-based implementation of a LIFO stack. It is intended as a replacement of Java's *
order without modifying the stack */ public Iteratorjava.util.Stack
that is broken in many ways. * * @author arno */ public class ArrayStackimplements Iterable { private T[] data; private int size = 0; public ArrayStack() { this(10); } @SuppressWarnings("unchecked") public ArrayStack(int initialSize) { if(initialSize <= 0) { throw new IllegalArgumentException("size must be greater than 0"); } data = (T[]) new Object[initialSize]; } /** * Adds an element to the top of the stack. */ @SuppressWarnings("unchecked") public void push(T el) { if(size >= data.length) { final T[] oldData = data; data = (T[]) new Object[2*oldData.length]; System.arraycopy(oldData, 0, data, 0, oldData.length); } data[size] = el; size += 1; } /** * Removes and returns the element at the top of the stack, throwing a NoSuchElementException
if the * stack is empty. */ public T pop() { if(isEmpty()) { throw new NoSuchElementException("stack is empty"); } final T result = data[size-1]; data[size-1] = null; // allow GC of previous element size -= 1; return result; } /** * Returns the element at the top of the stack without removing it, throwing aNoSuchElementException
* if the stack is empty. */ public T peek() { if(isEmpty()) { throw new NoSuchElementException("stack is empty"); } return data[size-1]; } /** * Removes the element at the top of the stack and returns it in anAOption.some(...)
if the stack * is non-empty, and returnsAOption.none()
otherwise. */ public AOptiontryPop() { if(isEmpty()) { return AOption.none(); } return AOption.some(pop()); } /** * Returns the element at the top of the stack in an AOption.some(...)
without removing it, or returns *AOption.none()
if the stack is empty. */ public AOptiontryPeek() { if(isEmpty()) { return AOption.none(); } return AOption.some(data[size-1]); } public boolean contains(T el) { for(int i=0; i pop() iterator() { return new Iterator () { int idx=size(); @Override public boolean hasNext() { return idx>0; } @Override public T next() { idx -= 1; return data[idx]; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } /** * iterates through the stack's elements in reverse pop()
order, i.e. starting with the element that * was added first end finishing with the most recently added element. */ public IteratorreverseIterator() { return new Iterator () { int idx = 0; @Override public boolean hasNext() { return idx < size(); } @Override public T next() { return data[idx++]; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } public int size() { return size; } public boolean isEmpty() { return size == 0; } public boolean nonEmpty() { return size > 0; } @Override public String toString() { return ACollectionHelper.mkString(this, "[", ", ", "]"); } }