1+ package io .kurrent .dbclient .v2 ;
2+
3+ import java .util .UUID ;
4+ import java .util .function .Function ;
5+
6+ /**
7+ * Represents a message with payload, metadata, and schema information.
8+ */
9+ public final class Message {
10+ /**
11+ * An empty message instance.
12+ */
13+ public static final Message EMPTY = new Message ();
14+
15+ private final Object value ;
16+ private final Metadata metadata ;
17+ private final UUID recordId ;
18+ private final SchemaDataFormat dataFormat ;
19+
20+ /**
21+ * Initializes a new instance of the Message class with default values.
22+ */
23+ public Message () {
24+ this (null , new Metadata (), UUID .randomUUID (), SchemaDataFormat .JSON );
25+ }
26+
27+ /**
28+ * Initializes a new instance of the Message class.
29+ *
30+ * @param value The message payload.
31+ * @param metadata The message metadata.
32+ * @param recordId The assigned record id.
33+ * @param dataFormat The format of the schema associated with the message.
34+ */
35+ public Message (Object value , Metadata metadata , UUID recordId , SchemaDataFormat dataFormat ) {
36+ this .value = value ;
37+ this .metadata = metadata != null ? metadata : new Metadata ();
38+ this .recordId = recordId != null ? recordId : UUID .randomUUID ();
39+ this .dataFormat = dataFormat != null ? dataFormat : SchemaDataFormat .JSON ;
40+ }
41+
42+ /**
43+ * Gets the message payload.
44+ *
45+ * @return The message payload.
46+ */
47+ public Object getValue () {
48+ return value ;
49+ }
50+
51+ /**
52+ * Gets the message metadata.
53+ *
54+ * @return The message metadata.
55+ */
56+ public Metadata getMetadata () {
57+ return metadata ;
58+ }
59+
60+ /**
61+ * Gets the assigned record id.
62+ *
63+ * @return The assigned record id.
64+ */
65+ public UUID getRecordId () {
66+ return recordId ;
67+ }
68+
69+ /**
70+ * Gets the format of the schema associated with the message.
71+ *
72+ * @return The schema data format.
73+ */
74+ public SchemaDataFormat getDataFormat () {
75+ return dataFormat ;
76+ }
77+
78+ /**
79+ * Creates a new Message builder.
80+ *
81+ * @return A new Message builder.
82+ */
83+ public static Builder builder () {
84+ return new Builder (null , new Metadata (), null , SchemaDataFormat .JSON );
85+ }
86+
87+ /**
88+ * Builder for creating Message instances.
89+ */
90+ public static class Builder {
91+ private final Object value ;
92+ private final Metadata metadata ;
93+ private final UUID recordId ;
94+ private final SchemaDataFormat dataFormat ;
95+
96+ private Builder (Object value , Metadata metadata , UUID recordId , SchemaDataFormat dataFormat ) {
97+ this .value = value ;
98+ this .metadata = metadata ;
99+ this .recordId = recordId ;
100+ this .dataFormat = dataFormat ;
101+ }
102+
103+ /**
104+ * Sets the message payload.
105+ *
106+ * @param value The message payload.
107+ * @return This builder instance.
108+ */
109+ public Builder value (Object value ) {
110+ return new Builder (value , this .metadata , this .recordId , this .dataFormat );
111+ }
112+
113+ /**
114+ * Sets the message metadata.
115+ *
116+ * @param metadata The message metadata.
117+ * @return This builder instance.
118+ */
119+ public Builder metadata (Metadata metadata ) {
120+ assert metadata != null : "Metadata cannot be null" ;
121+ return new Builder (this .value , metadata , this .recordId , this .dataFormat );
122+ }
123+
124+ /**
125+ * Sets the assigned record id.
126+ *
127+ * @param recordId The assigned record id.
128+ * @return This builder instance.
129+ */
130+ public Builder recordId (UUID recordId ) {
131+ assert recordId != null : "Record ID cannot be null" ;
132+ return new Builder (this .value , this .metadata , recordId , this .dataFormat );
133+ }
134+
135+ /**
136+ * Sets the format of the schema associated with the message.
137+ *
138+ * @param dataFormat The schema data format.
139+ * @return This builder instance.
140+ */
141+ public Builder dataFormat (SchemaDataFormat dataFormat ) {
142+ return new Builder (this .value , this .metadata , this .recordId , dataFormat );
143+ }
144+
145+ public Builder when (boolean condition , Function <Builder , Builder > func ) {
146+ return condition ? func .apply (this ) : this ;
147+ }
148+
149+ /**
150+ * Builds a new Message instance.
151+ *
152+ * @return A new Message instance.
153+ */
154+ public Message build () {
155+ return new Message (this .value , this .metadata , this .recordId != null ? this .recordId : UUID .randomUUID (), this .dataFormat );
156+ }
157+ }
158+ }
0 commit comments