| | -package com.whitemagicsoftware.rxm; |
| | - |
| | -import java.lang.reflect.ParameterizedType; |
| | -import java.lang.reflect.Type; |
| | - |
| | -import java.util.ArrayDeque; |
| | -import java.util.Deque; |
| | -import java.util.NoSuchElementException; |
| | -import java.util.Iterator; |
| | - |
| | -/** |
| | - * Captures and silently ignores stack exceptions. |
| | - */ |
| | -public abstract class SilentStack<E> extends ArrayDeque<E> { |
| | - /** |
| | - * Returns the top-most value off the stack, or a new instance of E |
| | - * if the stack is empty. This removes the object from the stack. |
| | - * |
| | - * @return The top-most element on the stack. |
| | - */ |
| | - public E pop() { |
| | - try { |
| | - return super.pop(); |
| | - } |
| | - catch( NoSuchElementException nsee ) { |
| | - return create(); |
| | - } |
| | - } |
| | - |
| | - /** |
| | - * Creates a new instance of the generic type E. This should not ever |
| | - * return null. |
| | - * |
| | - * @return A new instance of E, or null if it could not be instantiated. |
| | - */ |
| | - @SuppressWarnings("unchecked") |
| | - private E create() { |
| | - try { |
| | - Type sooper = getClass().getGenericSuperclass(); |
| | - Type t = ((ParameterizedType)sooper).getActualTypeArguments()[ 0 ]; |
| | - |
| | - return (E)(Class.forName( t.toString() ).newInstance()); |
| | - } |
| | - catch( Exception e ) { |
| | - return null; |
| | - } |
| | - } |
| | - |
| | - public Iterable<E> inReverse() { |
| | - return new Iterable<E>() { |
| | - public Iterator<E> iterator() { |
| | - return descendingIterator(); |
| | - } |
| | - }; |
| | - } |
| | -} |
| | - |
| | |