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 public class SynchronizedRedBlackTree<T extends Number & Comparable<T>> implements EventListInterface<T>, Serializable
22 {
23
24 private static final long serialVersionUID = 1L;
25
26
27 private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();
28
29
30
31
32 public SynchronizedRedBlackTree()
33 {
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
70 @Override
71 public int size()
72 {
73 synchronized (this.tree)
74 {
75 return this.tree.size();
76 }
77 }
78
79
80 @Override
81 public boolean isEmpty()
82 {
83 synchronized (this.tree)
84 {
85 return this.tree.isEmpty();
86 }
87 }
88
89
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
100 @Override
101 public Iterator<SimEventInterface<T>> iterator()
102 {
103 synchronized (this.tree)
104 {
105 return this.tree.iterator();
106 }
107 }
108
109
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
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
130 @Override
131 public void clear()
132 {
133 this.tree.clear();
134 }
135
136
137 @Override
138 public String toString()
139 {
140 return "SynchronizedRedBlackTree [" + this.tree.size() + " events]";
141 }
142
143 }