22
33import 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+ */
519public 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