-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopic.java
More file actions
48 lines (38 loc) · 995 Bytes
/
Copy pathTopic.java
File metadata and controls
48 lines (38 loc) · 995 Bytes
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
package test;
import java.util.ArrayList;
import java.util.List;
public class Topic {
public final String name;
private final List<Agent> subscribers = new ArrayList<>();
private final List<Agent> publishers = new ArrayList<>();
Topic(String name) {
this.name = name;
}
public void subscribe(Agent a) {
if (!subscribers.contains(a)) {
subscribers.add(a);
}
}
public void unsubscribe(Agent a) {
subscribers.remove(a);
}
public void publish(Message m) {
for (Agent a : subscribers) {
a.callback(this.name, m);
}
}
public void addPublisher(Agent a) {
if (!publishers.contains(a)) {
publishers.add(a);
}
}
public void removePublisher(Agent a) {
publishers.remove(a);
}
public List<Agent> getSubscribers() {
return subscribers;
}
public List<Agent> getPublishers() {
return publishers;
}
}