1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2019 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.chat; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.event.Dispatcher; |
23
|
|
|
import fr.quatrevieux.araknemu.game.chat.event.ChannelSubscriptionAdded; |
24
|
|
|
import fr.quatrevieux.araknemu.game.chat.event.ChannelSubscriptionRemoved; |
25
|
|
|
import org.checkerframework.checker.index.qual.NonNegative; |
26
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull; |
27
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
28
|
|
|
|
29
|
|
|
import java.util.Collection; |
30
|
|
|
import java.util.Collections; |
31
|
|
|
import java.util.EnumSet; |
32
|
|
|
import java.util.Iterator; |
33
|
|
|
import java.util.Set; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set of subscribed channels with a dispatcher for synchronise operations |
37
|
|
|
*/ |
38
|
|
|
public final class ChannelSet implements Set<ChannelType> { |
39
|
|
|
private final Set<ChannelType> set; |
40
|
|
|
private final Dispatcher dispatcher; |
41
|
|
|
|
42
|
1 |
|
public ChannelSet(Set<ChannelType> set, Dispatcher dispatcher) { |
43
|
1 |
|
this.set = set; |
44
|
1 |
|
this.dispatcher = dispatcher; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
@Override |
48
|
|
|
public @NonNegative int size() { |
49
|
1 |
|
return set.size(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
@Override |
53
|
|
|
public boolean isEmpty() { |
54
|
1 |
|
return set.isEmpty(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
@Override |
58
|
|
|
public boolean contains(Object o) { |
59
|
1 |
|
return set.contains(o); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
@Override |
63
|
|
|
public Iterator<ChannelType> iterator() { |
64
|
1 |
|
return set.iterator(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
@Override |
68
|
|
|
public Object[] toArray() { |
69
|
|
|
return set.toArray(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
@Override |
73
|
|
|
@SuppressWarnings({"toarray.nullable.elements.not.newarray", "override.param", "return"}) |
74
|
|
|
public <T> T @NonNull[] toArray(T @NonNull[] ts) { |
75
|
|
|
return set.toArray(ts); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
@Override |
79
|
|
|
public boolean add(ChannelType channelType) { |
80
|
1 |
|
if (set.add(channelType)) { |
81
|
1 |
|
dispatcher.dispatch(new ChannelSubscriptionAdded(Collections.singleton(channelType))); |
82
|
1 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
@Override |
89
|
|
|
public boolean remove(Object o) { |
90
|
1 |
|
if (set.remove(o)) { |
91
|
1 |
|
dispatcher.dispatch(new ChannelSubscriptionRemoved(Collections.singleton((ChannelType) o))); |
92
|
1 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
@Override |
99
|
|
|
public boolean containsAll(Collection<?> collection) { |
100
|
1 |
|
return set.containsAll(collection); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
@Override |
104
|
|
|
public boolean addAll(Collection<? extends ChannelType> collection) { |
105
|
1 |
|
if (set.addAll(collection)) { |
106
|
1 |
|
dispatcher.dispatch(new ChannelSubscriptionAdded((Collection<ChannelType>) collection)); |
107
|
1 |
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
@Override |
114
|
|
|
public boolean retainAll(Collection<?> collection) { |
115
|
|
|
throw new UnsupportedOperationException(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
@Override |
119
|
|
|
public boolean removeAll(Collection<?> collection) { |
120
|
1 |
|
if (set.removeAll(collection)) { |
121
|
1 |
|
dispatcher.dispatch(new ChannelSubscriptionRemoved((Collection<ChannelType>) collection)); |
122
|
1 |
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
@Override |
129
|
|
|
public void clear() { |
130
|
1 |
|
set.clear(); |
131
|
1 |
|
dispatcher.dispatch(new ChannelSubscriptionRemoved(EnumSet.allOf(ChannelType.class))); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
@Override |
135
|
|
|
public boolean equals(@Nullable Object obj) { |
|
|
|
|
136
|
1 |
|
return set.equals(obj); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
The Java tutorial explains the
equals()
andhashCode()
in its entry on the Object class.