36
36
*
37
37
* @author Christoph Strobl
38
38
* @author Mark Paluch
39
+ * @author Myroslav Kosinskyi
39
40
* @since 2.1
40
41
*/
41
42
public class ChangeStreamEvent <T > {
42
43
43
44
@ SuppressWarnings ("rawtypes" ) //
44
- private static final AtomicReferenceFieldUpdater <ChangeStreamEvent , Object > CONVERTED_UPDATER = AtomicReferenceFieldUpdater
45
- .newUpdater (ChangeStreamEvent .class , Object .class , "converted" );
45
+ private static final AtomicReferenceFieldUpdater <ChangeStreamEvent , Object > CONVERTED_FULL_DOCUMENT_UPDATER = AtomicReferenceFieldUpdater
46
+ .newUpdater (ChangeStreamEvent .class , Object .class , "convertedFullDocument" );
47
+
48
+ @ SuppressWarnings ("rawtypes" ) //
49
+ private static final AtomicReferenceFieldUpdater <ChangeStreamEvent , Object > CONVERTED_FULL_DOCUMENT_BEFORE_CHANGE_UPDATER = AtomicReferenceFieldUpdater
50
+ .newUpdater (ChangeStreamEvent .class , Object .class , "convertedFullDocumentBeforeChange" );
46
51
47
52
private final @ Nullable ChangeStreamDocument <Document > raw ;
48
53
49
54
private final Class <T > targetType ;
50
55
private final MongoConverter converter ;
51
56
52
- // accessed through CONVERTED_UPDATER.
53
- private volatile @ Nullable T converted ;
57
+ // accessed through CONVERTED_FULL_DOCUMENT_UPDATER.
58
+ private volatile @ Nullable T convertedFullDocument ;
59
+
60
+ // accessed through CONVERTED_FULL_DOCUMENT_BEFORE_CHANGE_UPDATER.
61
+ private volatile @ Nullable T convertedFullDocumentBeforeChange ;
54
62
55
63
/**
56
64
* @param raw can be {@literal null}.
@@ -147,27 +155,36 @@ public String getCollectionName() {
147
155
@ Nullable
148
156
public T getBody () {
149
157
150
- if (raw == null ) {
158
+ if (raw == null || raw . getFullDocument () == null ) {
151
159
return null ;
152
160
}
153
161
154
- Document fullDocument = raw .getFullDocument ();
162
+ return getConvertedFullDocument (raw .getFullDocument ());
163
+ }
164
+
165
+ @ Nullable
166
+ public T getBodyBeforeChange () {
155
167
156
- if (fullDocument == null ) {
157
- return targetType . cast ( fullDocument ) ;
168
+ if (raw == null || raw . getFullDocumentBeforeChange () == null ) {
169
+ return null ;
158
170
}
159
171
160
- return getConverted (fullDocument );
172
+ return getConvertedFullDocumentBeforeChange (raw .getFullDocumentBeforeChange ());
173
+ }
174
+
175
+ @ SuppressWarnings ("unchecked" )
176
+ private T getConvertedFullDocumentBeforeChange (Document fullDocument ) {
177
+ return (T ) doGetConverted (fullDocument , CONVERTED_FULL_DOCUMENT_BEFORE_CHANGE_UPDATER );
161
178
}
162
179
163
180
@ SuppressWarnings ("unchecked" )
164
- private T getConverted (Document fullDocument ) {
165
- return (T ) doGetConverted (fullDocument );
181
+ private T getConvertedFullDocument (Document fullDocument ) {
182
+ return (T ) doGetConverted (fullDocument , CONVERTED_FULL_DOCUMENT_UPDATER );
166
183
}
167
184
168
- private Object doGetConverted (Document fullDocument ) {
185
+ private Object doGetConverted (Document fullDocument , AtomicReferenceFieldUpdater < ChangeStreamEvent , Object > updater ) {
169
186
170
- Object result = CONVERTED_UPDATER .get (this );
187
+ Object result = updater .get (this );
171
188
172
189
if (result != null ) {
173
190
return result ;
@@ -176,13 +193,13 @@ private Object doGetConverted(Document fullDocument) {
176
193
if (ClassUtils .isAssignable (Document .class , fullDocument .getClass ())) {
177
194
178
195
result = converter .read (targetType , fullDocument );
179
- return CONVERTED_UPDATER .compareAndSet (this , null , result ) ? result : CONVERTED_UPDATER .get (this );
196
+ return updater .compareAndSet (this , null , result ) ? result : updater .get (this );
180
197
}
181
198
182
199
if (converter .getConversionService ().canConvert (fullDocument .getClass (), targetType )) {
183
200
184
201
result = converter .getConversionService ().convert (fullDocument , targetType );
185
- return CONVERTED_UPDATER .compareAndSet (this , null , result ) ? result : CONVERTED_UPDATER .get (this );
202
+ return updater .compareAndSet (this , null , result ) ? result : updater .get (this );
186
203
}
187
204
188
205
throw new IllegalArgumentException (
0 commit comments