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. <br>
15   * See for project information <a href="https://www.simulation.tudelft.nl"> www.simulation.tudelft.nl </a> <br>
16   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL) </a>, no warranty.
17   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
18   * @param <T> the type of simulation time, e.g. SimTimeCalendarLong or SimTimeDouble or SimTimeDoubleUnit.
19   * @since 1.5
20   */
21  public class SynchronizedRedBlackTree<T extends Number & Comparable<T>> implements EventListInterface<T>, Serializable
22  {
23      /** The default serial version UID for serializable classes. */
24      private static final long serialVersionUID = 1L;
25  
26      /** wrapped treeset. */
27      private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();
28  
29      /**
30       * Constructs a new <code>SynchronizedRedBlackTree</code>.
31       */
32      public SynchronizedRedBlackTree()
33      {
34      }
35  
36      /** {@inheritDoc} */
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      /** {@inheritDoc} */
70      @Override
71      public int size()
72      {
73          synchronized (this.tree)
74          {
75              return this.tree.size();
76          }
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public boolean isEmpty()
82      {
83          synchronized (this.tree)
84          {
85              return this.tree.isEmpty();
86          }
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public boolean contains(final SimEventInterface<T> o)
92      {
93          synchronized (this.tree)
94          {
95              return this.tree.contains(o);
96          }
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public Iterator<SimEventInterface<T>> iterator()
102     {
103         synchronized (this.tree)
104         {
105             return this.tree.iterator();
106         }
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public void add(final SimEventInterface<T> e)
112     {
113         synchronized (this.tree)
114         {
115             this.tree.add(e);
116         }
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public boolean remove(final SimEventInterface<T> o)
122     {
123         synchronized (this.tree)
124         {
125             return this.tree.remove(o);
126         }
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public void clear()
132     {
133         this.tree.clear();
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public String toString()
139     {
140         return "SynchronizedRedBlackTree [" + this.tree.size() + " events]";
141     }
142 
143 }