Skip to content

Commit 83b2a2a

Browse files
committed
Refine contribution #4351
- Rename methods to match operation names - Update Javadocs - Fix code formatting Issue #4149
1 parent 34e3e0e commit 83b2a2a

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,29 @@
5858
*/
5959
public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
6060

61+
/**
62+
* Operation mode of the item writer.
63+
*
64+
* @since 5.1
65+
*/
6166
public enum Mode {
62-
INSERT, UPSERT, REMOVE;
67+
68+
/**
69+
* Insert items into the target collection using
70+
* {@link BulkOperations#insert(Object)}.
71+
*/
72+
INSERT,
73+
/**
74+
* Insert or update items into the target collection using
75+
* {@link BulkOperations#replaceOne(Query, Object, FindAndReplaceOptions)}.
76+
*/
77+
UPSERT,
78+
/**
79+
* Remove items from the target collection using
80+
* {@link BulkOperations#remove(Query)}.
81+
*/
82+
REMOVE;
83+
6384
}
6485

6586
private static final String ID_KEY = "_id";
@@ -79,19 +100,22 @@ public MongoItemWriter() {
79100

80101
/**
81102
* Indicates if the items being passed to the writer are to be saved or removed from
82-
* the data store. If set to false (default), the items will be saved. If set to true,
83-
* the items will be removed.
103+
* the data store. If set to false (default), the items will be saved or update using
104+
* {@link Mode#UPSERT}. If set to true, then items will be removed.
84105
* @param delete removal indicator
85-
* @deprecated use {@link MongoItemWriter#setMode(Mode)}
106+
* @deprecated use {@link MongoItemWriter#setMode(Mode)} instead. Scheduled for
107+
* removal in v5.3 or later.
86108
*/
87-
@Deprecated
109+
@Deprecated(since = "5.1", forRemoval = true)
88110
public void setDelete(boolean delete) {
89111
this.mode = (delete) ? Mode.REMOVE : Mode.UPSERT;
90112
}
91113

92114
/**
93-
* Set the operating {@link Mode} to be applied by this writer.
115+
* Set the operating {@link Mode} to be applied by this writer. Defaults to
116+
* {@link Mode#UPSERT}.
94117
* @param mode the mode to be used.
118+
* @since 5.1
95119
*/
96120
public void setMode(final Mode mode) {
97121
this.mode = mode;
@@ -148,20 +172,14 @@ public void write(Chunk<? extends T> chunk) throws Exception {
148172
protected void doWrite(Chunk<? extends T> chunk) {
149173
if (!CollectionUtils.isEmpty(chunk.getItems())) {
150174
switch (this.mode) {
151-
case INSERT:
152-
save(chunk);
153-
break;
154-
case REMOVE:
155-
delete(chunk);
156-
break;
157-
default:
158-
saveOrUpdate(chunk);
159-
break;
175+
case INSERT -> insert(chunk);
176+
case REMOVE -> remove(chunk);
177+
default -> upsert(chunk);
160178
}
161179
}
162180
}
163181

164-
private void save(final Chunk<? extends T> chunk) {
182+
private void insert(final Chunk<? extends T> chunk) {
165183
final BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0));
166184
final MongoConverter mongoConverter = this.template.getConverter();
167185
for (final Object item : chunk) {
@@ -172,7 +190,7 @@ private void save(final Chunk<? extends T> chunk) {
172190
bulkOperations.execute();
173191
}
174192

175-
private void delete(Chunk<? extends T> chunk) {
193+
private void remove(Chunk<? extends T> chunk) {
176194
BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0));
177195
MongoConverter mongoConverter = this.template.getConverter();
178196
for (Object item : chunk) {
@@ -187,7 +205,7 @@ private void delete(Chunk<? extends T> chunk) {
187205
bulkOperations.execute();
188206
}
189207

190-
private void saveOrUpdate(Chunk<? extends T> chunk) {
208+
private void upsert(Chunk<? extends T> chunk) {
191209
BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0));
192210
MongoConverter mongoConverter = this.template.getConverter();
193211
FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert();

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
* A builder implementation for the {@link MongoItemWriter}
2626
*
2727
* @author Glenn Renfro
28+
* @author Mahmoud Ben Hassine
2829
* @since 4.0
2930
* @see MongoItemWriter
3031
*/
@@ -43,20 +44,23 @@ public class MongoItemWriterBuilder<T> {
4344
* @param delete removal indicator
4445
* @return The current instance of the builder
4546
* @see MongoItemWriter#setDelete(boolean)
46-
* @deprecated use {@link MongoItemWriterBuilder#mode(Mode)}
47+
* @deprecated Use {@link MongoItemWriterBuilder#mode(Mode)} instead. Scheduled for
48+
* removal in v5.3 or later.
4749
*/
48-
@Deprecated
50+
@Deprecated(since = "5.1", forRemoval = true)
4951
public MongoItemWriterBuilder<T> delete(boolean delete) {
5052
this.mode = (delete) ? Mode.REMOVE : Mode.UPSERT;
5153

5254
return this;
5355
}
5456

5557
/**
56-
* Set the operating {@link Mode} to be applied by this writer.
58+
* Set the operating {@link Mode} to be applied by this writer. Defaults to
59+
* {@link Mode#UPSERT}.
5760
* @param mode the mode to be used.
5861
* @return The current instance of the builder
5962
* @see MongoItemWriter#setMode(Mode)
63+
* @since 5.1
6064
*/
6165
public MongoItemWriterBuilder<T> mode(final Mode mode) {
6266
this.mode = mode;

0 commit comments

Comments
 (0)