View Javadoc
1   package org.opentrafficsim.core.dsol;
2   
3   import java.io.Serializable;
4   import java.util.Iterator;
5   import java.util.NoSuchElementException;
6   import java.util.TreeSet;
7   
8   import nl.tudelft.simulation.dsol.eventlists.EventListInterface;
9   import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
10  
11  /**
12   * A SynchronizedRedBlackTree implementation of the eventlistInterface. This implementation is based on Java's TreeSet.
13   * <p>
14   * (c) copyright 2002-2005 <a href="https://www.simulation.tudelft.nl">Delft University of Technology </a>, the Netherlands.
15   * <br>
16   * See for project information <a href="https://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   * @param <T> the type of simulation time, e.g. SimTimeCalendarLong or SimTimeDouble or SimTimeDoubleUnit.
20   * @since 1.5
21   */
22  public class SynchronizedRedBlackTree<T extends Number & Comparable<T>> implements EventListInterface<T>, Serializable
23  {
24      /** The default serial version UID for serializable classes. */
25      private static final long serialVersionUID = 1L;
26  
27      /** wrapped treeset. */
28      private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();
29  
30      /**
31       * Constructs a new <code>SynchronizedRedBlackTree</code>.
32       */
33      public SynchronizedRedBlackTree()
34      {
35      }
36  
37      @Override
38      public SimEventInterface<T> removeFirst()
39      {
40          synchronized (this.tree)
41          {
42              SimEventInterface<T> first = this.first();
43              this.remove(first);
44              return first;
45          }
46      }
47  
48      /**
49       * we re-implemented the first method. Instead of throwing exceptions if the tree is empty, we return a null value.
50       * @see java.util.TreeSet#first()
51       * @return the first SimEvent in the tree.
52       */
53      @Override
54      public SimEventInterface<T> first()
55      {
56          synchronized (this.tree)
57          {
58              try
59              {
60                  return this.tree.first();
61              }
62              catch (NoSuchElementException noSuchElementException)
63              {
64                  return null;
65              }
66          }
67      }
68  
69      @Override
70      public int size()
71      {
72          synchronized (this.tree)
73          {
74              return this.tree.size();
75          }
76      }
77  
78      @Override
79      public boolean isEmpty()
80      {
81          synchronized (this.tree)
82          {
83              return this.tree.isEmpty();
84          }
85      }
86  
87      @Override
88      public boolean contains(final SimEventInterface<T> o)
89      {
90          synchronized (this.tree)
91          {
92              return this.tree.contains(o);
93          }
94      }
95  
96      @Override
97      public Iterator<SimEventInterface<T>> iterator()
98      {
99          synchronized (this.tree)
100         {
101             return this.tree.iterator();
102         }
103     }
104 
105     @Override
106     public void add(final SimEventInterface<T> e)
107     {
108         synchronized (this.tree)
109         {
110             this.tree.add(e);
111         }
112     }
113 
114     @Override
115     public boolean remove(final SimEventInterface<T> o)
116     {
117         synchronized (this.tree)
118         {
119             return this.tree.remove(o);
120         }
121     }
122 
123     @Override
124     public void clear()
125     {
126         this.tree.clear();
127     }
128 
129     @Override
130     public String toString()
131     {
132         return "SynchronizedRedBlackTree [" + this.tree.size() + " events]";
133     }
134 
135 }