PeekStack.java

  1. package org.opentrafficsim.road.network.factory;

  2. import java.util.ArrayList;

  3. /**
  4.  * Stack object that allows easy verification of the values of the last few entries.
  5.  * <p>
  6.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  7.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  8.  * <p>
  9.  * $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, @version $Revision: 1155 $, by $Author: averbraeck $,
  10.  * initial version 24 jun. 2015 <br>
  11.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  12.  * @param <T> type of the objects stored on this PeekStack
  13.  */
  14. public class PeekStack<T> extends ArrayList<T>
  15. {
  16.     /** */
  17.     private static final long serialVersionUID = 20150624L;

  18.     /**
  19.      * Push an element on this stack.
  20.      * @param element T; the element to push onto this stack
  21.      */
  22.     public final void push(final T element)
  23.     {
  24.         add(element);
  25.     }

  26.     /**
  27.      * Pop an element off this stack.
  28.      * @return T; the element that was popped of this stack
  29.      */
  30.     public final T pop()
  31.     {
  32.         int index = this.size() - 1;
  33.         T result = get(index);
  34.         remove(index);
  35.         return result;
  36.     }

  37.     /**
  38.      * Return the N-from-last element of this PeekStack.
  39.      * @param offset int; if 0 the last pushed, but not yet popped element is returned; if offset is 1, the before last pushed,
  40.      *            but not yet popped element is returned; etc.
  41.      * @return T; the addressed element
  42.      */
  43.     public final T peekNthLast(final int offset)
  44.     {
  45.         return get(this.size() - 1 - offset);
  46.     }

  47.     /**
  48.      * Check if the elements on the top of this PeekStack are equal to the provided entries.
  49.      * @param entries T...; the provided entries
  50.      * @return boolean; true if this PeekStack contains, at the top, the provided entries in the specified order; false
  51.      *         otherwise
  52.      */
  53.     public final boolean topEntriesEqual(@SuppressWarnings("unchecked") final T... entries)
  54.     {
  55.         int mySize = size();
  56.         if (entries.length > mySize)
  57.         {
  58.             return false;
  59.         }
  60.         for (int index = 0; index < entries.length; index++)
  61.         {
  62.             if (!peekNthLast(index).equals(entries[index]))
  63.             {
  64.                 return false;
  65.             }
  66.         }
  67.         return true;
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public final String toString()
  72.     {
  73.         return "PeekStack [size=" + size() + "]";
  74.     }

  75. }