1 package org.opentrafficsim.core.egtf;
2
3
4
5
6
7
8
9
10
11
12 public class EgtfParallelListener implements EgtfListener
13 {
14
15
16 private Filter filter;
17
18
19 private Object lock = new Object();
20
21
22 private double progress = 0.0;
23
24
25 private boolean interrupted = false;
26
27
28
29
30 EgtfParallelListener()
31 {
32
33 }
34
35
36
37
38
39
40
41
42 public double wait(final double untilProgress, final long timeout) throws InterruptedException
43 {
44 long t1 = System.currentTimeMillis() + timeout;
45 long tOut = timeout;
46 double uProgress = Math.min(1.0, untilProgress);
47 while (this.progress < uProgress && tOut > 0)
48 {
49 synchronized (this.lock)
50 {
51 this.lock.wait(tOut);
52 }
53 tOut = t1 - System.currentTimeMillis();
54 }
55 return this.progress;
56 }
57
58
59
60
61
62 public void setFilter(final Filter filter)
63 {
64 this.filter = filter;
65 this.progress = 1.0;
66 synchronized (this.lock)
67 {
68 this.lock.notifyAll();
69 }
70 }
71
72
73
74
75
76 public Filter getFilter()
77 {
78 if (this.filter == null)
79 {
80 throw new IllegalStateException("Trying to obtain the filter results before filtering is done.");
81 }
82 return this.filter;
83 }
84
85
86 @Override
87 public void notifyProgress(final EgtfEvent event)
88 {
89 if (this.interrupted)
90 {
91 event.interrupt();
92 this.progress = 1.0;
93 }
94 else
95 {
96
97 this.progress = Math.min(event.getProgress(), 1.0 - 1e-9);
98 }
99 synchronized (this.lock)
100 {
101 this.lock.notifyAll();
102 }
103 }
104
105
106
107
108 public void interrupt()
109 {
110 this.interrupted = true;
111 }
112
113
114 @Override
115 public String toString()
116 {
117 return "EgtfParallelListener [filter=" + this.filter + ", progress=" + this.progress + "]";
118 }
119
120 }