-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathEventBusBuilder.java
212 lines (191 loc) · 6.77 KB
/
EventBusBuilder.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package me.zero.alpine.bus;
import me.zero.alpine.listener.*;
import me.zero.alpine.listener.discovery.ListenerDiscoveryStrategy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import java.util.*;
/**
* A builder class for {@link EventManager} and {@link AttachableEventManager}. A new instance of this class is created
* and returned by {@link EventManager#builder()}. May be used to construct an {@link EventManager} with
* {@link #build()}, or get passed to {@link EventManager#EventManager(EventBusBuilder)} when constructing a new
* instance or defining a subclass.
*
* @author Brady
* @since 2.0.0
*/
public final class EventBusBuilder<T extends EventBus> {
// Default Settings
private String name = null;
private boolean parentDiscovery = false;
private boolean superListeners = false;
private ListenerExceptionHandler exceptionHandler = ListenerExceptionHandler.defaultHandler();
private ListenerListFactory listenerListFactory = ListenerListFactory.defaultFactory();
private final List<ListenerDiscoveryStrategy> discoveryStrategies = new ArrayList<>();
private boolean attachable = false;
EventBusBuilder() {
this.discoveryStrategies.add(ListenerDiscoveryStrategy.subscribeFields());
this.discoveryStrategies.add(ListenerDiscoveryStrategy.subscribeMethods());
}
/**
* Sets the name of the {@link EventBus}.
*
* @param name The name
* @return This builder
* @see EventBus#name()
* @since 2.0.0
*/
public @NotNull EventBusBuilder<T> setName(@NotNull String name) {
Objects.requireNonNull(name);
this.name = name;
return this;
}
/**
* Enables parent {@link Listener} discovery, allowing superclasses of a {@link Subscriber} implementation (which
* must also implement {@link Subscriber}) to be passed to the listener discovery strategies.
*
* @return This builder
* @since 2.0.0
*/
public @NotNull EventBusBuilder<T> setParentDiscovery() {
this.parentDiscovery = true;
return this;
}
/**
* Enables {@link Listener} targeting an event's supertype to be invoked with the event in addition to
* {@link Listener}s directly targeting the event type.
*
* @return This builder
* @since 2.0.0
*/
public @NotNull EventBusBuilder<T> setSuperListeners() {
this.superListeners = true;
return this;
}
/**
* Sets the exception handler that will be invoked when an exception is thrown by a Listener. The specified
* exception handler may be {@code null}, indicating that no explicit exception handling is to occur, and
* exceptions should just propagate upwards.
*
* @param exceptionHandler The exception handler
* @return This builder
* @see ListenerExceptionHandler#defaultHandler()
* @since 3.0.0
*/
public @NotNull EventBusBuilder<T> setExceptionHandler(@Nullable ListenerExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
return this;
}
/**
* Disables exception handling. Equivalent to {@code setExceptionHandler(null)}.
*
* @return This builder
* @since 3.0.0
*/
public @NotNull EventBusBuilder<T> noExceptionHandler() {
return this.setExceptionHandler(null);
}
/**
* Sets the factory that will be used to create {@link ListenerList} objects for each event type.
*
* @param factory The factory
* @return This builder
* @see ListenerListFactory#defaultFactory()
* @since 3.0.0
*/
public @NotNull EventBusBuilder<T> setListenerListFactory(@NotNull ListenerListFactory factory) {
Objects.requireNonNull(factory);
this.listenerListFactory = factory;
return this;
}
/**
* Replaces the current list of discovery strategies with the specified strategies.
*
* @param strategies The new strategies
* @return This builder
* @see EventBusBuilder#addDiscoveryStrategies
* @since 3.0.0
*/
public @NotNull EventBusBuilder<T> setDiscoveryStrategies(@NotNull ListenerDiscoveryStrategy... strategies) {
this.discoveryStrategies.clear();
return this.addDiscoveryStrategies(strategies);
}
/**
* Adds the specified strategies to the list of discovery strategies. The strategies that are included by default
* are {@link ListenerDiscoveryStrategy#subscribeFields()} and {@link ListenerDiscoveryStrategy#subscribeMethods()}.
*
* @param strategies The strategies
* @return This builder
* @since 3.0.0
*/
public @NotNull EventBusBuilder<T> addDiscoveryStrategies(@NotNull ListenerDiscoveryStrategy... strategies) {
Collections.addAll(this.discoveryStrategies, strategies);
return this;
}
/**
* Causes this builder to create an {@link EventBus} which implements {@link AttachableEventBus}.
*
* @return This builder
* @since 2.0.0
*/
@SuppressWarnings("unchecked")
public @NotNull EventBusBuilder<AttachableEventBus> setAttachable() {
this.attachable = true;
// Illegal? Maybe.
return (EventBusBuilder<AttachableEventBus>) this;
}
/**
* Returns a newly constructed {@link EventBus} instance using this builder.
*
* @since 2.0.0
*/
@SuppressWarnings("unchecked")
public @NotNull T build() {
Objects.requireNonNull(this.name);
return this.attachable
? (T) new AttachableEventManager(this)
: (T) new EventManager(this);
}
/**
* Returns the name
* @since 3.0.0
*/
public String getName() {
return this.name;
}
/**
* Returns {@code true} if parent class discovery is enabled
* @since 3.0.0
*/
public boolean isParentDiscovery() {
return this.parentDiscovery;
}
/**
* Returns {@code true} if super listeners are enabled
* @since 3.0.0
*/
public boolean isSuperListeners() {
return this.superListeners;
}
/**
* Returns an optional containing the exception handler, or {@link Optional#empty()} if none
* @since 3.0.0
*/
public Optional<ListenerExceptionHandler> getExceptionHandler() {
return Optional.ofNullable(this.exceptionHandler);
}
/**
* Returns the listener list factory
* @since 3.0.0
*/
public @NotNull ListenerListFactory getListenerListFactory() {
return this.listenerListFactory;
}
/**
* Returns the discovery strategies
* @since 3.0.0
*/
public @NotNull @UnmodifiableView List<ListenerDiscoveryStrategy> getDiscoveryStrategies() {
return Collections.unmodifiableList(this.discoveryStrategies);
}
}