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
13
14
15
16
17
18
19
20
21
22 public class SynchronizedRedBlackTree<T extends Number & Comparable<T>> implements EventListInterface<T>, Serializable
23 {
24
25 private static final long serialVersionUID = 1L;
26
27
28 private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();
29
30
31
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
50
51
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 }