Skip to content

Commit 8febfe0

Browse files
Improve javadoc coverage (#96)
Co-Authored-By: Jonathing <[email protected]>
1 parent 73e1cbc commit 8febfe0

File tree

13 files changed

+167
-23
lines changed

13 files changed

+167
-23
lines changed

src/main/java/net/minecraftforge/eventbus/api/bus/BusGroup.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,47 @@
99
import net.minecraftforge.eventbus.api.listener.SubscribeEvent;
1010
import net.minecraftforge.eventbus.internal.BusGroupImpl;
1111

12+
import java.lang.invoke.LambdaMetafactory;
1213
import java.lang.invoke.MethodHandles;
1314
import java.util.Collection;
1415

1516
/**
16-
* A collection of {@link EventBus} instances that are grouped together for easier management.
17+
* A collection of {@link EventBus} instances that are grouped together for easier management, allowing for bulk
18+
* operations.
1719
*/
1820
public sealed interface BusGroup permits BusGroupImpl {
21+
/**
22+
* The default BusGroup, which is used when an {@link EventBus} is created without specifying a BusGroup.
23+
*/
1924
BusGroup DEFAULT = create("default");
2025

26+
/**
27+
* Creates a new BusGroup with the given name.
28+
*
29+
* @param name The unique name of the BusGroup
30+
* @return A new BusGroup with the given name
31+
* @throws IllegalArgumentException if the name is already in use by another BusGroup
32+
* @apiNote To enforce a base type for all events in this BusGroup, use {@link #create(String, Class)}.
33+
*/
2134
static BusGroup create(String name) {
2235
return new BusGroupImpl(name, Event.class);
2336
}
2437

38+
/**
39+
* Creates a new BusGroup with the given name and base type.
40+
*
41+
* @param name The unique name of the BusGroup
42+
* @param baseType The base type that all events in this BusGroup must extend or implement
43+
* @return A new BusGroup with the given name and base type
44+
* @throws IllegalArgumentException if the name is already in use by another BusGroup
45+
*/
2546
static BusGroup create(String name, Class<?> baseType) {
2647
return new BusGroupImpl(name, baseType);
2748
}
2849

2950
/**
3051
* The unique name of this BusGroup.
52+
* <p>The uniqueness of this name is enforced when the bus group is {@linkplain #create(String) created}.</p>
3153
*/
3254
String name();
3355

@@ -40,21 +62,30 @@ static BusGroup create(String name, Class<?> baseType) {
4062
/**
4163
* Shuts down all EventBus instances associated with this BusGroup, preventing any further events from being posted
4264
* until {@link #startup()} is called.
65+
*
66+
* @apiNote If you don't intend on using this BusGroup again, prefer {@link #dispose()} instead as that will also
67+
* free up resources.
4368
*/
4469
void shutdown();
4570

4671
/**
47-
* Shuts down all EventBus instances associated with this BusGroup, unregisters all listeners and frees resources
48-
* no longer needed.
49-
* <p>Warning: This is a destructive operation - this BusGroup should not be used again after calling this method.</p>
72+
* {@linkplain #shutdown() Shuts down} all EventBus instances associated with this BusGroup,
73+
* {@linkplain #unregister(Collection) unregisters} all listeners and frees no longer needed resources.
74+
*
75+
* <p>Warning: This is a destructive operation - this BusGroup should not be used again after calling this method -
76+
* attempting to do so may throw exceptions or act as a no-op.</p>
77+
*
78+
* @apiNote If you plan on using this BusGroup again, prefer {@link #shutdown()} instead.
5079
*/
5180
void dispose();
5281

5382
/**
54-
* Experimental feature - may be removed, renamed or otherwise changed without notice.
55-
* <p>Trims the backing lists of all EventBus instances associated with this BusGroup to free up resources.</p>
83+
* Trims the backing lists of all EventBus instances associated with this BusGroup to free up resources.
84+
*
5685
* <p>Warning: This is only intended to be called <b>once</b> after all listeners are registered - calling this
5786
* repeatedly may hurt performance.</p>
87+
*
88+
* @apiNote This is an experimental feature that may be removed, renamed or otherwise changed without notice.
5889
*/
5990
void trim();
6091

@@ -68,6 +99,11 @@ static BusGroup create(String name, Class<?> baseType) {
6899
* @apiNote This method only registers static listeners.
69100
* <p>If you want to register both instance and static methods, use
70101
* {@link BusGroup#register(MethodHandles.Lookup, Object)} instead.</p>
102+
* @implNote Internally, bulk registration uses {@link LambdaMetafactory} to create method references to the
103+
* annotated methods using the provided {@code callerLookup} - said lookup must have
104+
* {@linkplain MethodHandles.Lookup#hasFullPrivilegeAccess() full privilege access} as
105+
* {@linkplain LambdaMetafactory LMF} may need to spin an inner class for implementing the lambda, which
106+
* inherently allows access to private fields and methods.
71107
*/
72108
Collection<EventListener> register(MethodHandles.Lookup callerLookup, Class<?> utilityClassWithStaticListeners);
73109

@@ -80,6 +116,11 @@ static BusGroup create(String name, Class<?> baseType) {
80116
*
81117
* @apiNote If you know all the listeners are static methods, use
82118
* {@link BusGroup#register(MethodHandles.Lookup, Class)} instead for better registration performance.
119+
* @implNote Internally, bulk registration uses {@link LambdaMetafactory} to create method references to the
120+
* annotated methods using the provided {@code callerLookup} - said lookup must have
121+
* {@linkplain MethodHandles.Lookup#hasFullPrivilegeAccess() full privilege access} as
122+
* {@linkplain LambdaMetafactory LMF} may need to spin an inner class for implementing the lambda, which
123+
* inherently allows access to private fields and methods.
83124
*/
84125
Collection<EventListener> register(MethodHandles.Lookup callerLookup, Object listener);
85126

src/main/java/net/minecraftforge/eventbus/api/bus/EventBus.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
import java.util.function.Consumer;
1616

17+
/**
18+
* @see CancellableEventBus if your event type implements {@link Cancellable}
19+
* @param <T> The type of event this EventBus handles
20+
*/
1721
public sealed interface EventBus<T extends Event> permits CancellableEventBus, AbstractEventBusImpl, EventBusImpl {
1822
/**
1923
* Adds a listener to this EventBus with the default priority of {@link Priority#NORMAL}.

src/main/java/net/minecraftforge/eventbus/api/bus/package-info.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
* Copyright (c) Forge Development LLC
33
* SPDX-License-Identifier: LGPL-2.1-only
44
*/
5+
/**
6+
* EventBus is built around the idea of {@linkplain net.minecraftforge.eventbus.api.bus.EventBus event buses} that are
7+
* grouped together by {@linkplain net.minecraftforge.eventbus.api.bus.BusGroup bus groups}. This package contains this
8+
* core part of the API.
9+
*/
510
@NullMarked
611
package net.minecraftforge.eventbus.api.bus;
712

src/main/java/net/minecraftforge/eventbus/api/event/InheritableEvent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66

77
import net.minecraftforge.eventbus.internal.Event;
88

9+
/**
10+
* A hybrid of an event base type and characteristic - implementing this interface on your event type allows for event
11+
* posting on subclasses to propagate to your event type, essentially opting into event inheritance. This allows for
12+
* flexible event hierarchies with mixed levels of encapsulation and mutability.
13+
*/
914
public non-sealed interface InheritableEvent extends Event {}

src/main/java/net/minecraftforge/eventbus/api/event/MutableEvent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@
77
import net.minecraftforge.eventbus.internal.Event;
88
import net.minecraftforge.eventbus.internal.MutableEventInternals;
99

10+
/**
11+
* For mutable event classes that may also extend other classes and/or support being extended.
12+
* <p>More advanced techniques like protected fields and methods are also possible, where the supertype may be a
13+
* protected abstract class with some internals handled for you, but only the concrete types are actual events.</p>
14+
*
15+
* <p>Consider {@link RecordEvent} for better performance and conciseness if field mutability and direct extendability
16+
* aren't needed.</p>
17+
*
18+
* @see RecordEvent
19+
*/
1020
public non-sealed abstract class MutableEvent extends MutableEventInternals implements Event {}

src/main/java/net/minecraftforge/eventbus/api/event/RecordEvent.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,34 @@
44
*/
55
package net.minecraftforge.eventbus.api.event;
66

7+
import net.minecraftforge.eventbus.api.bus.EventBus;
8+
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
79
import net.minecraftforge.eventbus.internal.Event;
810

911
/**
1012
* For read-only or shallowly-immutable records.
13+
* <p>Provides a means for declaring events in a concise and performant manner.</p>
14+
*
15+
* <h2>Examples</h2>
16+
* <p>Here are some conceptual examples of record events:</p>
17+
* <ul>
18+
* <li>Consider a leaderboard event that provides a list of players that listeners can mutate but doesn't allow
19+
* the list to be set to null or an immutable list that would result in unexpected behaviour for the other
20+
* listeners.</li>
21+
* <li>An event where listeners are notified of a player joining the server and can optionally cancel it to kick them
22+
* (when combined with the {@link Cancellable} characteristic), but can't set the player to someone else.</li>
23+
* <li>Stateless events which do not carry any data inside but are still useful for notifying listeners <i>when</i>
24+
* a specific action occurs, such as some types of lifecycle events.</li>
25+
* </ul>
26+
*
27+
* <p>Note that while records are final and cannot extend other classes, inheritance is still possible through other
28+
* means, such as by implementing a sealed interface and using the Java module system.</p>
29+
*
30+
* @apiNote Cancellation is supported for record events as long as they also implement the {@link Cancellable}
31+
* characteristic - this is possible thanks to the cancellation state being kept on the stack separately from
32+
* the record instance itself.
33+
* @implSpec This event base type can only be applied to {@linkplain Record Java record classes}.
34+
* If you implement this interface on an ordinary class, an exception will be thrown when attempting to
35+
* create an associated {@link EventBus}.
1136
*/
1237
public non-sealed interface RecordEvent extends Event {}

src/main/java/net/minecraftforge/eventbus/api/event/characteristic/Cancellable.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,25 @@
55
package net.minecraftforge.eventbus.api.event.characteristic;
66

77
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
8+
import net.minecraftforge.eventbus.api.event.RecordEvent;
9+
import net.minecraftforge.eventbus.api.listener.Priority;
810
import net.minecraftforge.eventbus.internal.Event;
911
import net.minecraftforge.eventbus.internal.EventCharacteristic;
1012

13+
import java.util.function.Consumer;
14+
import java.util.function.Predicate;
15+
1116
/**
12-
* A cancellable event returns {@code true} from {@link CancellableEventBus#post(Event)} if it was cancelled.
13-
* <p>When an event is cancelled, it will not be passed to any further non-monitor listeners.</p>
17+
* A cancellable event returns {@code true} from {@link CancellableEventBus#post(Event)} if it was cancelled by a
18+
* {@linkplain CancellableEventBus#addListener(Predicate) 'maybe cancelling'} or
19+
* {@linkplain CancellableEventBus#addListener(boolean, Consumer) 'always cancelling'} listener.
20+
*
21+
* <p>When an event is cancelled, it will not be passed to any further non-{@linkplain Priority#MONITOR monitor}
22+
* listeners.
23+
* For further details on a cancellable event's interactions with an EventBus, see {@link CancellableEventBus}.</p>
24+
*
25+
* @implNote Internally, the cancellation state is kept on the stack separately from the event instance itself, allowing
26+
* for this characteristic to be applied to any event type - even {@link RecordEvent}s.
27+
* @see CancellableEventBus
1428
*/
1529
public non-sealed interface Cancellable extends EventCharacteristic {}

src/main/java/net/minecraftforge/eventbus/api/event/characteristic/MonitorAware.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
*/
55
package net.minecraftforge.eventbus.api.event.characteristic;
66

7+
import net.minecraftforge.eventbus.api.bus.EventBus;
78
import net.minecraftforge.eventbus.api.event.MutableEvent;
9+
import net.minecraftforge.eventbus.api.event.RecordEvent;
10+
import net.minecraftforge.eventbus.api.listener.Priority;
811
import net.minecraftforge.eventbus.internal.EventCharacteristic;
912
import net.minecraftforge.eventbus.internal.MutableEventInternals;
1013

1114
/**
12-
* Experimental feature - may be removed, renamed or otherwise changed without notice.
13-
* <p>Events that are {@link MonitorAware} can provide stronger immutability guarantees to monitor listeners by
14-
* returning unmodifiable views or throwing exceptions on mutation attempts when monitoring.</p>
15-
* <p>Only supported for {@link MutableEvent} at this time.</p>
15+
* Events that are {@linkplain Priority#MONITOR monitor}-aware are able to provide stronger immutability guarantees to
16+
* monitoring listeners by returning unmodifiable views or throwing exceptions on mutation attempts when monitoring.
17+
*
18+
* @apiNote This is an experimental feature that may be removed, renamed or otherwise changed without notice.
19+
* @implNote This characteristic is only supported for the {@link MutableEvent} base type at this time.
20+
* If combined with a different base type (such as {@link RecordEvent}), an exception will be thrown when
21+
* attempting to create an associated {@link EventBus}.
1622
*/
1723
public non-sealed interface MonitorAware extends EventCharacteristic {
1824
default boolean isMonitoring() {

src/main/java/net/minecraftforge/eventbus/api/event/characteristic/SelfDestructing.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
*/
55
package net.minecraftforge.eventbus.api.event.characteristic;
66

7+
import net.minecraftforge.eventbus.api.bus.BusGroup;
78
import net.minecraftforge.eventbus.api.bus.EventBus;
8-
import net.minecraftforge.eventbus.internal.AbstractEventBusImpl;
99
import net.minecraftforge.eventbus.internal.EventCharacteristic;
1010

1111
/**
12-
* A self-destructing event will {@link AbstractEventBusImpl#dispose() dispose} of its associated {@link EventBus}
13-
* after it has been posted to free up resources, after which it cannot be posted to again.
12+
* A self-destructing event will {@link BusGroup#dispose() dispose} of its associated {@link EventBus} after it has
13+
* been posted to free up resources, after which it cannot be posted to again.
1414
* <p>This is useful for single-use lifecycle events.</p>
15+
*
16+
* @apiNote The dispose action is similar to {@link BusGroup#dispose()}, but applies to only this event rather than to
17+
* all events in the group.
1518
*/
1619
public non-sealed interface SelfDestructing extends EventCharacteristic {}

src/main/java/net/minecraftforge/eventbus/api/event/characteristic/SelfPosting.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import net.minecraftforge.eventbus.internal.EventCharacteristic;
1010

1111
/**
12-
* Experimental feature - may be removed, renamed or otherwise changed without notice.
13-
* <p>{@link SelfPosting} events are associated with a default {@link EventBus} in order to offer some convenience
14-
* instance methods.</p>
15-
* <u>Example</u>
12+
* Self-posting events are associated with a default {@link EventBus}, allowing for some convenience methods on the
13+
* instance.
14+
*
15+
* <h2>Example</h2>
1616
* {@snippet :
17+
* import net.minecraftforge.eventbus.api.bus.EventBus;
1718
* import net.minecraftforge.eventbus.api.event.RecordEvent;
19+
* import net.minecraftforge.eventbus.api.event.characteristic.SelfPosting;
1820
*
1921
* // Event declaration
2022
* public record ExampleEvent() implements SelfPosting<ExampleEvent>, RecordEvent {
@@ -32,10 +34,13 @@
3234
* // instead of this
3335
* ExampleEvent.BUS.post(new ExampleEvent());
3436
*}
37+
*
38+
* @apiNote This is an experimental feature that may be removed, renamed or otherwise changed without notice.
3539
*/
3640
public non-sealed interface SelfPosting<T extends Event> extends EventCharacteristic {
3741
/**
38-
* @implSpec This should directly return a {@code static final} field without additional logic or processing.
42+
* @implSpec This should directly return a {@code static final} field without additional logic or processing -
43+
* failure to do so may hurt performance.
3944
*/
4045
EventBus<T> getDefaultBus();
4146

0 commit comments

Comments
 (0)