View Javadoc
1   package org.opentrafficsim.core.dsol;
2   
3   import java.io.Serializable;
4   import java.util.Collection;
5   import java.util.Comparator;
6   import java.util.Iterator;
7   import java.util.NoSuchElementException;
8   import java.util.SortedSet;
9   import java.util.TreeSet;
10  
11  import nl.tudelft.simulation.dsol.eventlists.EventListInterface;
12  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
13  import nl.tudelft.simulation.dsol.simtime.SimTime;
14  
15  /**
16   * A SynchronizedRedBlackTree implementation of the eventlistInterface. This implementation is based on Java's TreeSet.
17   * <p>
18   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the Netherlands. <br>
19   * See for project information <a href="http://www.simulation.tudelft.nl"> www.simulation.tudelft.nl </a> <br>
20   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL) </a>, no warranty.
21   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
22   * @version $Revision: 1.2 $ $Date: 2010/08/10 11:36:45 $
23   * @param <T> the type of simulation time, e.g. SimTimeCalendarLong or SimTimeDouble or SimTimeDoubleUnit.
24   * @since 1.5
25   */
26  public class SynchronizedRedBlackTree<T extends SimTime<?, ?, T>> implements EventListInterface<T>, Serializable
27  {
28      /** The default serial version UID for serializable classes. */
29      private static final long serialVersionUID = 1L;
30  
31      /** wrapped treeset. */
32      private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();
33  
34      /**
35       * Constructs a new <code>SynchronizedRedBlackTree</code>.
36       */
37      public SynchronizedRedBlackTree()
38      {
39          super();
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      @SuppressWarnings("checkstyle:designforextension")
45      public SimEventInterface<T> removeFirst()
46      {
47          synchronized (this.tree)
48          {
49              SimEventInterface<T> first = this.first();
50              this.remove(first);
51              return first;
52          }
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      @SuppressWarnings("checkstyle:designforextension")
58      public SimEventInterface<T> removeLast()
59      {
60          synchronized (this.tree)
61          {
62              SimEventInterface<T> last = this.last();
63              this.remove(last);
64              return last;
65          }
66      }
67  
68      /**
69       * we re-implemented the first method. Instead of throwing exceptions if the tree is empty, we return a null value.
70       * @see java.util.TreeSet#first()
71       * @return the first SimEvent in the tree.
72       */
73      @Override
74      @SuppressWarnings("checkstyle:designforextension")
75      public SimEventInterface<T> first()
76      {
77          synchronized (this.tree)
78          {
79              try
80              {
81                  return this.tree.first();
82              }
83              catch (NoSuchElementException noSuchElementException)
84              {
85                  return null;
86              }
87          }
88      }
89  
90      /**
91       * we re-implemented the last method. Instead of throwing exceptions if the tree is empty, we return a null value.
92       * @see java.util.TreeSet#last()
93       * @return the last SimEvent in the tree.
94       */
95      @Override
96      @SuppressWarnings("checkstyle:designforextension")
97      public SimEventInterface<T> last()
98      {
99          synchronized (this.tree)
100         {
101             try
102             {
103                 return this.tree.last();
104             }
105             catch (NoSuchElementException noSuchElementException)
106             {
107                 return null;
108             }
109         }
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public Comparator<? super SimEventInterface<T>> comparator()
115     {
116         synchronized (this.tree)
117         {
118             return this.tree.comparator();
119         }
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public SortedSet<SimEventInterface<T>> subSet(final SimEventInterface<T> fromElement, final SimEventInterface<T> toElement)
125     {
126         synchronized (this.tree)
127         {
128             return this.tree.subSet(fromElement, toElement);
129         }
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public SortedSet<SimEventInterface<T>> headSet(final SimEventInterface<T> toElement)
135     {
136         synchronized (this.tree)
137         {
138             return this.tree.headSet(toElement);
139         }
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public SortedSet<SimEventInterface<T>> tailSet(final SimEventInterface<T> fromElement)
145     {
146         synchronized (this.tree)
147         {
148             return this.tree.tailSet(fromElement);
149         }
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public int size()
155     {
156         synchronized (this.tree)
157         {
158             return this.tree.size();
159         }
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     public boolean isEmpty()
165     {
166         synchronized (this.tree)
167         {
168             return this.tree.isEmpty();
169         }
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public boolean contains(final Object o)
175     {
176         synchronized (this.tree)
177         {
178             return this.tree.contains(o);
179         }
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     public Iterator<SimEventInterface<T>> iterator()
185     {
186         synchronized (this.tree)
187         {
188             return this.tree.iterator();
189         }
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public Object[] toArray()
195     {
196         synchronized (this.tree)
197         {
198             return this.tree.toArray();
199         }
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     public <X> X[] toArray(final X[] a)
205     {
206         synchronized (this.tree)
207         {
208             return this.tree.toArray(a);
209         }
210     }
211 
212     /** {@inheritDoc} */
213     @Override
214     public boolean add(final SimEventInterface<T> e)
215     {
216         synchronized (this.tree)
217         {
218             return this.tree.add(e);
219         }
220     }
221 
222     /** {@inheritDoc} */
223     @Override
224     public boolean remove(final Object o)
225     {
226         synchronized (this.tree)
227         {
228             return this.tree.remove(o);
229         }
230     }
231 
232     /** {@inheritDoc} */
233     @Override
234     public boolean containsAll(final Collection<?> c)
235     {
236         synchronized (this.tree)
237         {
238             return this.tree.containsAll(c);
239         }
240     }
241 
242     /** {@inheritDoc} */
243     @Override
244     public boolean addAll(final Collection<? extends SimEventInterface<T>> c)
245     {
246         synchronized (this.tree)
247         {
248             return this.tree.addAll(c);
249         }
250     }
251 
252     /** {@inheritDoc} */
253     @Override
254     public boolean retainAll(final Collection<?> c)
255     {
256         synchronized (this.tree)
257         {
258             return this.tree.retainAll(c);
259         }
260     }
261 
262     /** {@inheritDoc} */
263     @Override
264     public boolean removeAll(final Collection<?> c)
265     {
266         synchronized (this.tree)
267         {
268             return this.tree.removeAll(c);
269         }
270     }
271 
272     /** {@inheritDoc} */
273     @Override
274     public void clear()
275     {
276         this.tree.clear();
277     }
278 
279     /** {@inheritDoc} */
280     @Override
281     public String toString()
282     {
283         return "SynchronizedRedBlackTree [" + this.tree.size() + " events]";
284     }
285 
286 }