SynchronizedRedBlackTree.java

  1. package org.opentrafficsim.core.dsol;

  2. import java.io.Serializable;
  3. import java.util.Collection;
  4. import java.util.Comparator;
  5. import java.util.Iterator;
  6. import java.util.NoSuchElementException;
  7. import java.util.SortedSet;
  8. import java.util.TreeSet;

  9. import nl.tudelft.simulation.dsol.eventlists.EventListInterface;
  10. import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
  11. import nl.tudelft.simulation.dsol.simtime.SimTime;

  12. /**
  13.  * A SynchronizedRedBlackTree implementation of the eventlistInterface. This implementation is based on Java's TreeSet.
  14.  * <p>
  15.  * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the Netherlands. <br>
  16.  * See for project information <a href="http://www.simulation.tudelft.nl"> www.simulation.tudelft.nl </a> <br>
  17.  * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL) </a>, no warranty.
  18.  * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
  19.  * @version $Revision: 1.2 $ $Date: 2010/08/10 11:36:45 $
  20.  * @param <T> the type of simulation time, e.g. SimTimeCalendarLong or SimTimeDouble or SimTimeDoubleUnit.
  21.  * @since 1.5
  22.  */
  23. public class SynchronizedRedBlackTree<T extends SimTime<?, ?, T>> implements EventListInterface<T>, Serializable
  24. {
  25.     /** The default serial version UID for serializable classes. */
  26.     private static final long serialVersionUID = 1L;

  27.     /** wrapped treeset. */
  28.     private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();

  29.     /**
  30.      * Constructs a new <code>SynchronizedRedBlackTree</code>.
  31.      */
  32.     public SynchronizedRedBlackTree()
  33.     {
  34.         super();
  35.     }

  36.     /** {@inheritDoc} */
  37.     @Override
  38.     @SuppressWarnings("checkstyle:designforextension")
  39.     public SimEventInterface<T> removeFirst()
  40.     {
  41.         synchronized (this.tree)
  42.         {
  43.             SimEventInterface<T> first = this.first();
  44.             this.remove(first);
  45.             return first;
  46.         }
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     @SuppressWarnings("checkstyle:designforextension")
  51.     public SimEventInterface<T> removeLast()
  52.     {
  53.         synchronized (this.tree)
  54.         {
  55.             SimEventInterface<T> last = this.last();
  56.             this.remove(last);
  57.             return last;
  58.         }
  59.     }

  60.     /**
  61.      * we re-implemented the first method. Instead of throwing exceptions if the tree is empty, we return a null value.
  62.      * @see java.util.TreeSet#first()
  63.      * @return the first SimEvent in the tree.
  64.      */
  65.     @Override
  66.     @SuppressWarnings("checkstyle:designforextension")
  67.     public SimEventInterface<T> first()
  68.     {
  69.         synchronized (this.tree)
  70.         {
  71.             try
  72.             {
  73.                 return this.tree.first();
  74.             }
  75.             catch (NoSuchElementException noSuchElementException)
  76.             {
  77.                 return null;
  78.             }
  79.         }
  80.     }

  81.     /**
  82.      * we re-implemented the last method. Instead of throwing exceptions if the tree is empty, we return a null value.
  83.      * @see java.util.TreeSet#last()
  84.      * @return the last SimEvent in the tree.
  85.      */
  86.     @Override
  87.     @SuppressWarnings("checkstyle:designforextension")
  88.     public SimEventInterface<T> last()
  89.     {
  90.         synchronized (this.tree)
  91.         {
  92.             try
  93.             {
  94.                 return this.tree.last();
  95.             }
  96.             catch (NoSuchElementException noSuchElementException)
  97.             {
  98.                 return null;
  99.             }
  100.         }
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public Comparator<? super SimEventInterface<T>> comparator()
  105.     {
  106.         synchronized (this.tree)
  107.         {
  108.             return this.tree.comparator();
  109.         }
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     public SortedSet<SimEventInterface<T>> subSet(final SimEventInterface<T> fromElement, final SimEventInterface<T> toElement)
  114.     {
  115.         synchronized (this.tree)
  116.         {
  117.             return this.tree.subSet(fromElement, toElement);
  118.         }
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public SortedSet<SimEventInterface<T>> headSet(final SimEventInterface<T> toElement)
  123.     {
  124.         synchronized (this.tree)
  125.         {
  126.             return this.tree.headSet(toElement);
  127.         }
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public SortedSet<SimEventInterface<T>> tailSet(final SimEventInterface<T> fromElement)
  132.     {
  133.         synchronized (this.tree)
  134.         {
  135.             return this.tree.tailSet(fromElement);
  136.         }
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     public int size()
  141.     {
  142.         synchronized (this.tree)
  143.         {
  144.             return this.tree.size();
  145.         }
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public boolean isEmpty()
  150.     {
  151.         synchronized (this.tree)
  152.         {
  153.             return this.tree.isEmpty();
  154.         }
  155.     }

  156.     /** {@inheritDoc} */
  157.     @Override
  158.     public boolean contains(final Object o)
  159.     {
  160.         synchronized (this.tree)
  161.         {
  162.             return this.tree.contains(o);
  163.         }
  164.     }

  165.     /** {@inheritDoc} */
  166.     @Override
  167.     public Iterator<SimEventInterface<T>> iterator()
  168.     {
  169.         synchronized (this.tree)
  170.         {
  171.             return this.tree.iterator();
  172.         }
  173.     }

  174.     /** {@inheritDoc} */
  175.     @Override
  176.     public Object[] toArray()
  177.     {
  178.         synchronized (this.tree)
  179.         {
  180.             return this.tree.toArray();
  181.         }
  182.     }

  183.     /** {@inheritDoc} */
  184.     @Override
  185.     public <X> X[] toArray(final X[] a)
  186.     {
  187.         synchronized (this.tree)
  188.         {
  189.             return this.tree.toArray(a);
  190.         }
  191.     }

  192.     /** {@inheritDoc} */
  193.     @Override
  194.     public boolean add(final SimEventInterface<T> e)
  195.     {
  196.         synchronized (this.tree)
  197.         {
  198.             return this.tree.add(e);
  199.         }
  200.     }

  201.     /** {@inheritDoc} */
  202.     @Override
  203.     public boolean remove(final Object o)
  204.     {
  205.         synchronized (this.tree)
  206.         {
  207.             return this.tree.remove(o);
  208.         }
  209.     }

  210.     /** {@inheritDoc} */
  211.     @Override
  212.     public boolean containsAll(final Collection<?> c)
  213.     {
  214.         synchronized (this.tree)
  215.         {
  216.             return this.tree.containsAll(c);
  217.         }
  218.     }

  219.     /** {@inheritDoc} */
  220.     @Override
  221.     public boolean addAll(final Collection<? extends SimEventInterface<T>> c)
  222.     {
  223.         synchronized (this.tree)
  224.         {
  225.             return this.tree.addAll(c);
  226.         }
  227.     }

  228.     /** {@inheritDoc} */
  229.     @Override
  230.     public boolean retainAll(final Collection<?> c)
  231.     {
  232.         synchronized (this.tree)
  233.         {
  234.             return this.tree.retainAll(c);
  235.         }
  236.     }

  237.     /** {@inheritDoc} */
  238.     @Override
  239.     public boolean removeAll(final Collection<?> c)
  240.     {
  241.         synchronized (this.tree)
  242.         {
  243.             return this.tree.removeAll(c);
  244.         }
  245.     }

  246.     /** {@inheritDoc} */
  247.     @Override
  248.     public void clear()
  249.     {
  250.         this.tree.clear();
  251.     }

  252.     /** {@inheritDoc} */
  253.     @Override
  254.     public String toString()
  255.     {
  256.         return "SynchronizedRedBlackTree [" + this.tree.size() + " events]";
  257.     }

  258. }