forked from strongback/strongback-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutables.java
104 lines (88 loc) · 3.26 KB
/
Executables.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Strongback
* Copyright 2015, Strongback and individual contributors by the @authors tag.
* See the COPYRIGHT.txt in the distribution for a full listing of individual
* contributors.
*
* Licensed under the MIT License; you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.strongback;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.strongback.annotation.ThreadSafe;
/**
* A simple threadsafe list of {@link Executable} instances.
*
* @author Randall Hauch
*/
@ThreadSafe
final class Executables implements Executor {
private final CopyOnWriteArrayList<Executable> highPriority = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<Executable> mediumPriority = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<Executable> lowPriority = new CopyOnWriteArrayList<>();
Executables() {
}
@Override
public boolean register(Executable r, Priority priority) {
if (priority != null) {
unregister(r);
switch (priority) {
case HIGH:
mediumPriority.remove(r);
lowPriority.remove(r);
return highPriority.addIfAbsent(r);
case MEDIUM:
highPriority.remove(r);
lowPriority.remove(r);
return mediumPriority.addIfAbsent(r);
case LOW:
highPriority.remove(r);
mediumPriority.remove(r);
return lowPriority.addIfAbsent(r);
}
}
return false;
}
@Override
public boolean unregister(Executable r) {
if (r != null) {
// Remove from all
boolean removed = highPriority.remove(r);
removed = mediumPriority.remove(r) || removed;
removed = lowPriority.remove(r) || removed;
return removed;
}
return false;
}
@Override
public void unregisterAll() {
highPriority.clear();
mediumPriority.clear();
lowPriority.clear();
}
public List<Executable> lowPriorityExecutables() {
return lowPriority;
}
public List<Executable> mediumPriorityExecutables() {
return mediumPriority;
}
public List<Executable> highPriorityExecutables() {
return highPriority;
}
protected Executable[] lowPriorityExecutablesAsArrays() {
return lowPriority.toArray(new Executable[0]); // will be reallocated with correct size
}
protected Executable[] mediumPriorityExecutablesAsArrays() {
return mediumPriority.toArray(new Executable[0]); // will be reallocated with correct size
}
protected Executable[] highPriorityExecutablesAsArrays() {
return highPriority.toArray(new Executable[0]); // will be reallocated with correct size
}
}