@@ -89,7 +89,7 @@ public static StaticAttributeTag versionAttribute() {
89
89
return VERSION_ATTRIBUTE ;
90
90
}
91
91
92
- public static StaticAttributeTag versionAttribute (int startAt , int incrementBy ) {
92
+ public static StaticAttributeTag versionAttribute (Integer startAt , Integer incrementBy ) {
93
93
return new VersionAttribute (startAt , incrementBy );
94
94
}
95
95
}
@@ -98,15 +98,15 @@ private static final class VersionAttribute implements StaticAttributeTag {
98
98
private static final String START_AT_METADATA_KEY = "VersionedRecordExtension:StartAt" ;
99
99
private static final String INCREMENT_BY_METADATA_KEY = "VersionedRecordExtension:IncrementBy" ;
100
100
101
- private final int startAt ;
102
- private final int incrementBy ;
101
+ private final Integer startAt ;
102
+ private final Integer incrementBy ;
103
103
104
104
private VersionAttribute () {
105
- this .startAt = 0 ;
106
- this .incrementBy = 1 ;
105
+ this .startAt = null ;
106
+ this .incrementBy = null ;
107
107
}
108
108
109
- private VersionAttribute (int startAt , int incrementBy ) {
109
+ private VersionAttribute (Integer startAt , Integer incrementBy ) {
110
110
this .startAt = startAt ;
111
111
this .incrementBy = incrementBy ;
112
112
}
@@ -120,6 +120,14 @@ public Consumer<StaticTableMetadata.Builder> modifyMetadata(String attributeName
120
120
"is supported." , attributeName , attributeValueType .name ()));
121
121
}
122
122
123
+ if (startAt != null && startAt < 0 ) {
124
+ throw new IllegalArgumentException ("StartAt cannot be negative." );
125
+ }
126
+
127
+ if (incrementBy != null && incrementBy < 1 ) {
128
+ throw new IllegalArgumentException ("IncrementBy must be greater than 0." );
129
+ }
130
+
123
131
return metadata -> metadata
124
132
.addCustomMetadataObject (CUSTOM_METADATA_KEY , attributeName )
125
133
.addCustomMetadataObject (START_AT_METADATA_KEY , startAt )
@@ -137,7 +145,6 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex
137
145
return WriteModification .builder ().build ();
138
146
}
139
147
140
-
141
148
Pair <AttributeValue , Expression > updates = getRecordUpdates (versionAttributeKey .get (), context );
142
149
143
150
// Unpack values from Pair
@@ -169,20 +176,13 @@ private Pair<AttributeValue, Expression> getRecordUpdates(String versionAttribut
169
176
}
170
177
171
178
private boolean isInitialVersion (AttributeValue existingVersionValue , DynamoDbExtensionContext .BeforeWrite context ) {
172
- if (isNullAttributeValue (existingVersionValue )) {
173
- return true ;
174
- }
175
-
176
-
177
179
Optional <Integer > versionStartAtFromAnnotation = context .tableMetadata ()
178
180
.customMetadataObject (VersionAttribute .START_AT_METADATA_KEY ,
179
181
Integer .class );
180
182
181
183
return isNullAttributeValue (existingVersionValue )
182
- || (versionStartAtFromAnnotation .isPresent () &&
183
- getExistingVersion (existingVersionValue ) == versionStartAtFromAnnotation .get ())
184
- || (!versionStartAtFromAnnotation .isPresent () &&
185
- getExistingVersion (existingVersionValue ) == this .startAt );
184
+ || (versionStartAtFromAnnotation .isPresent () && getExistingVersion (existingVersionValue ) == versionStartAtFromAnnotation .get ())
185
+ || getExistingVersion (existingVersionValue ) == this .startAt ;
186
186
}
187
187
188
188
private Pair <AttributeValue , Expression > createInitialRecord (String versionAttributeKey ,
@@ -262,6 +262,9 @@ private Builder() {
262
262
* @return the builder instance
263
263
*/
264
264
public Builder startAt (int startAt ) {
265
+ if (startAt < 0 ) {
266
+ throw new IllegalArgumentException ("StartAt cannot be negative." );
267
+ }
265
268
this .startAt = startAt ;
266
269
return this ;
267
270
}
@@ -274,6 +277,9 @@ public Builder startAt(int startAt) {
274
277
* @return the builder instance
275
278
*/
276
279
public Builder incrementBy (int incrementBy ) {
280
+ if (incrementBy < 1 ) {
281
+ throw new IllegalArgumentException ("IncrementBy must be greater than 0." );
282
+ }
277
283
this .incrementBy = incrementBy ;
278
284
return this ;
279
285
}
0 commit comments