Skip to content

Commit 1ab4776

Browse files
committed
enhancing documentation
1 parent 0002fad commit 1ab4776

File tree

11 files changed

+956
-171
lines changed

11 files changed

+956
-171
lines changed

src/main/java/de/backendstack21/realtime/pubsub/ConnectionInfo.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,85 @@
22

33
import java.util.Map;
44

5-
5+
/**
6+
* Represents the connection information for a client.
7+
*
8+
* <h2>Typical Usage Example:</h2>
9+
* <pre>{@code
10+
* // Register event listener for session started
11+
* client.on("session.started", (Object... eventArgs) -> {
12+
* logger.info("Session started: " + (ConnectionInfo) eventArgs[0]);
13+
*
14+
* // ...
15+
* });
16+
* }</pre>
17+
*/
618
public class ConnectionInfo {
19+
/**
20+
* A unique identifier for this connection. This ID typically remains constant
21+
* throughout the lifecycle of a single session or connection instance.
22+
*/
723
private final String id;
24+
25+
/**
26+
* The application identifier associated with this connection. This value is often used
27+
* to distinguish one application's client connections from another's.
28+
*/
829
private final String appId;
30+
31+
/**
32+
* The remote network address of the connected client (e.g., IP address).
33+
* This information can be vital for auditing, debugging, or policy enforcement.
34+
*/
935
private final String remoteAddress;
1036

37+
/**
38+
* Constructs a new {@code ConnectionInfo} instance with the provided attributes.
39+
*
40+
* @param id the unique identifier of the connection
41+
* @param appId the application identifier associated with the connection
42+
* @param remoteAddress the remote address (e.g., IP address) of the connection
43+
*/
1144
private ConnectionInfo(String id, String appId, String remoteAddress) {
1245
this.id = id;
1346
this.appId = appId;
1447
this.remoteAddress = remoteAddress;
1548
}
1649

50+
/**
51+
* Retrieves the unique identifier of this connection.
52+
*
53+
* @return the connection ID as a {@link String}
54+
*/
1755
public String getId() {
1856
return id;
1957
}
2058

59+
/**
60+
* Retrieves the application ID associated with this connection.
61+
*
62+
* @return the application ID as a {@link String}
63+
*/
2164
public String getAppId() {
2265
return appId;
2366
}
2467

68+
/**
69+
* Retrieves the remote address of the client. This may be an IP address or
70+
* a domain name depending on the server's configuration.
71+
*
72+
* @return the remote address as a {@link String}
73+
*/
2574
public String getRemoteAddress() {
2675
return remoteAddress;
2776
}
2877

78+
/**
79+
* Constructs a new {@code ConnectionInfo} instance from a map of attributes.
80+
*
81+
* @param message a {@link Map} containing connection attributes
82+
* @return a new {@code ConnectionInfo} instance constructed from the provided attributes
83+
*/
2984
public static ConnectionInfo from(Map<String, Object> message) {
3085
String id = (String) message.get("id");
3186
String appId = (String) message.get("appId");
@@ -34,6 +89,18 @@ public static ConnectionInfo from(Map<String, Object> message) {
3489
return new ConnectionInfo(id, appId, remoteAddress);
3590
}
3691

92+
/**
93+
* Constructs a new {@code ConnectionInfo} instance from a generic object,
94+
* which must be a {@link Map} containing connection attributes.
95+
* <p>
96+
* If the provided object is not a {@link Map}, this method will throw
97+
* an {@link IllegalArgumentException}.
98+
* </p>
99+
*
100+
* @param message the object expected to be a {@link Map} of connection attributes
101+
* @return a new {@code ConnectionInfo} instance
102+
* @throws IllegalArgumentException if the provided object is not a {@link Map}
103+
*/
37104
@SuppressWarnings("unchecked")
38105
public static ConnectionInfo from(Object message) {
39106
if (message instanceof Map) {
@@ -43,8 +110,14 @@ public static ConnectionInfo from(Object message) {
43110
}
44111
}
45112

113+
/**
114+
* Returns a string representation of this {@code ConnectionInfo},
115+
* including the connection ID, application ID, and remote address.
116+
*
117+
* @return a string in the format {@code ConnectionInfo{id='...', appId='...', remoteAddress='...'}}
118+
*/
46119
@Override
47120
public String toString() {
48121
return "ConnectionInfo{" + "id='" + id + '\'' + ", appId='" + appId + '\'' + ", remoteAddress='" + remoteAddress + '\'' + '}';
49122
}
50-
}
123+
}

src/main/java/de/backendstack21/realtime/pubsub/EventEmitter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class EventEmitter {
1616
*/
1717
private static final Logger logger = Logger.getLogger(EventEmitter.class.getName());
1818

19-
2019
private final Map<String, List<EventListener>> events;
2120

2221
/**
@@ -113,6 +112,15 @@ public static boolean eventMatches(String pattern, String eventName) {
113112
return matchSegments(patternSegments, eventSegments, 0, 0);
114113
}
115114

115+
/**
116+
* Helper method to match event segments with pattern segments.
117+
*
118+
* @param patternSegments The segments of the event pattern.
119+
* @param eventSegments The segments of the event name.
120+
* @param i The current index in the pattern segments.
121+
* @param j The current index in the event segments.
122+
* @return True if the segments match, False otherwise.
123+
*/
116124
private static boolean matchSegments(String[] patternSegments, String[] eventSegments, int i, int j) {
117125
while (i < patternSegments.length && j < eventSegments.length) {
118126
if (patternSegments[i].equals("**")) {

src/main/java/de/backendstack21/realtime/pubsub/EventListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
/**
44
* A functional interface for event listeners.
5+
* Implementations of this interface can be used to handle events emitted by the EventEmitter.
56
*/
67
@FunctionalInterface
78
public interface EventListener {
9+
/**
10+
* Handles an event with the given arguments.
11+
*
12+
* @param args the arguments passed to the event
13+
* @throws Exception if an error occurs while handling the event
14+
*/
815
void handle(Object... args) throws Exception;
916
}

src/main/java/de/backendstack21/realtime/pubsub/IncomingMessage.java

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,124 @@
22

33
import java.util.Map;
44

5+
/**
6+
* Represents an incoming topic message.
7+
* <p>
8+
* This class encapsulates the details of a message received on a specific topic,
9+
* including its type, payload, and optional compression flag.
10+
* </p>
11+
*
12+
* <h2>Key Features:</h2>
13+
* <ul>
14+
* <li>Stores information about the topic, message type, compression status, and payload.</li>
15+
* <li>Supports creation from a {@link Map} representation or a generic object.</li>
16+
* <li>Provides getters for accessing message details.</li>
17+
* </ul>
18+
*/
519
public class IncomingMessage {
20+
21+
/**
22+
* The topic on which the message was published.
23+
*/
624
private final String topic;
25+
26+
/**
27+
* The type of the message. Used to identify the message format or purpose.
28+
*/
729
private final String messageType;
30+
31+
/**
32+
* Whether the message data is compressed. Defaults to {@code false} if not specified.
33+
*/
834
private final Boolean compression;
35+
36+
/**
37+
* The actual message payload. Can be String or Map.
38+
*/
939
private final Object data;
1040

41+
/**
42+
* Private constructor for creating an IncomingMessage object with the specified properties.
43+
*
44+
* @param topic the topic associated with the message.
45+
* @param messageType the type or purpose of the message.
46+
* @param compression a flag indicating if the message is compressed (optional).
47+
* @param data the payload or data of the message.
48+
*/
1149
private IncomingMessage(String topic, String messageType, Boolean compression, Object data) {
1250
this.topic = topic;
1351
this.messageType = messageType;
1452
this.compression = compression;
1553
this.data = data;
1654
}
1755

56+
/**
57+
* Returns the topic on which the message was published.
58+
*
59+
* @return the topic as a {@link String}.
60+
*/
1861
public String getTopic() {
1962
return topic;
2063
}
2164

65+
/**
66+
* Returns the type of the message.
67+
*
68+
* @return the message type as a {@link String}.
69+
*/
2270
public String getMessageType() {
2371
return messageType;
2472
}
2573

74+
/**
75+
* Returns whether the message data is compressed.
76+
* Defaults to {@code false} if not explicitly specified.
77+
*
78+
* @return a {@link Boolean} indicating if the data is compressed.
79+
*/
2680
public Boolean getCompression() {
2781
return compression;
2882
}
2983

84+
/**
85+
* Returns the message data.
86+
*
87+
* @return the payload as an {@link Object}.
88+
*/
3089
public Object getData() {
3190
return data;
3291
}
3392

93+
/**
94+
* Creates an {@link IncomingMessage} object from a {@link Map} representation.
95+
* The map is expected to contain the following keys:
96+
* <ul>
97+
* <li>{@code "topic"}: The topic of the message (required).</li>
98+
* <li>{@code "messageType"}: The type of the message (required).</li>
99+
* <li>{@code "compression"}: A flag indicating compression (optional, defaults to {@code false}).</li>
100+
* <li>{@code "data"}: The message payload (required).</li>
101+
* </ul>
102+
*
103+
* @param message a {@link Map} containing the message properties.
104+
* @return an {@link IncomingMessage} object constructed from the map.
105+
*/
34106
public static IncomingMessage from(Map<String, Object> message) {
35107
String topic = (String) message.get("topic");
36108
String messageType = (String) message.get("messageType");
37-
Boolean compression = (Boolean) message.get("compression");
109+
Boolean compression = (Boolean) message.getOrDefault("compression", false);
38110
Object data = message.get("data");
39111

40112
return new IncomingMessage(topic, messageType, compression, data);
41113
}
42114

115+
/**
116+
* Creates an {@link IncomingMessage} object from a generic object.
117+
* The object must be a {@link Map}, or an {@link IllegalArgumentException} will be thrown.
118+
*
119+
* @param message an {@link Object}, expected to be a {@link Map} representation of a message.
120+
* @return an {@link IncomingMessage} object constructed from the object.
121+
* @throws IllegalArgumentException if the input object is not a {@link Map}.
122+
*/
43123
@SuppressWarnings("unchecked")
44124
public static IncomingMessage from(Object message) {
45125
if (message instanceof Map) {
@@ -49,6 +129,12 @@ public static IncomingMessage from(Object message) {
49129
}
50130
}
51131

132+
/**
133+
* Returns a string representation of the {@link IncomingMessage}.
134+
* The string includes the topic, message type, compression status, and payload.
135+
*
136+
* @return a {@link String} representing the message details.
137+
*/
52138
@Override
53139
public String toString() {
54140
return "IncomingMessage{" + "topic='" + topic + '\'' + ", messageType='" + messageType + '\'' + ", compression=" + compression + ", data=" + data + '}';

0 commit comments

Comments
 (0)