DATAMONGO-2423 - Nullability refinements for Update.

Ease non null restrictions for operators that may use null values like $set.

Original pull request: #815.
This commit is contained in:
Christoph Strobl
2019-12-11 09:59:50 +01:00
committed by Mark Paluch
parent 4efaed4741
commit 3100e0db01
2 changed files with 16 additions and 14 deletions

View File

@@ -19,6 +19,7 @@ import java.util.Arrays;
import java.util.Collections;
import org.bson.Document;
import org.springframework.lang.Nullable;
/**
* @author Thomas Risberg
@@ -42,7 +43,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update set(String key, Object value) {
public Update set(String key, @Nullable Object value) {
updateObject.put("$set", Collections.singletonMap(key, value));
return this;
}
@@ -60,7 +61,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update push(String key, Object value) {
public Update push(String key, @Nullable Object value) {
updateObject.put("$push", Collections.singletonMap(key, value));
return this;
}
@@ -74,7 +75,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update addToSet(String key, Object value) {
public Update addToSet(String key, @Nullable Object value) {
updateObject.put("$addToSet", Collections.singletonMap(key, value));
return this;
}
@@ -86,7 +87,7 @@ public class BasicUpdate extends Update {
}
@Override
public Update pull(String key, Object value) {
public Update pull(String key, @Nullable Object value) {
updateObject.put("$pull", Collections.singletonMap(key, value));
return this;
}

View File

@@ -65,7 +65,7 @@ public class Update implements UpdateDefinition {
* @param key
* @return
*/
public static Update update(String key, Object value) {
public static Update update(String key, @Nullable Object value) {
return new Update().set(key, value);
}
@@ -107,11 +107,12 @@ public class Update implements UpdateDefinition {
* Update using the {@literal $set} update modifier
*
* @param key
* @param value
* @return
* @param value can be {@literal null}. In this case the property remains in the db with a {@literal null} value. To
* remove it use {@link #unset(String)}.
* @return this.
* @see <a href="https://docs.mongodb.com/manual/reference/operator/update/set/">MongoDB Update operator: $set</a>
*/
public Update set(String key, Object value) {
public Update set(String key, @Nullable Object value) {
addMultiFieldOperation("$set", key, value);
return this;
}
@@ -120,12 +121,12 @@ public class Update implements UpdateDefinition {
* Update using the {@literal $setOnInsert} update modifier
*
* @param key
* @param value
* @param value can be {@literal null}.
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/">MongoDB Update operator:
* $setOnInsert</a>
*/
public Update setOnInsert(String key, Object value) {
public Update setOnInsert(String key, @Nullable Object value) {
addMultiFieldOperation("$setOnInsert", key, value);
return this;
}
@@ -172,7 +173,7 @@ public class Update implements UpdateDefinition {
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/push/">MongoDB Update operator: $push</a>
*/
public Update push(String key, Object value) {
public Update push(String key, @Nullable Object value) {
addMultiFieldOperation("$push", key, value);
return this;
}
@@ -235,7 +236,7 @@ public class Update implements UpdateDefinition {
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/addToSet/">MongoDB Update operator:
* $addToSet</a>
*/
public Update addToSet(String key, Object value) {
public Update addToSet(String key, @Nullable Object value) {
addMultiFieldOperation("$addToSet", key, value);
return this;
}
@@ -261,7 +262,7 @@ public class Update implements UpdateDefinition {
* @return
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/pull/">MongoDB Update operator: $pull</a>
*/
public Update pull(String key, Object value) {
public Update pull(String key, @Nullable Object value) {
addMultiFieldOperation("$pull", key, value);
return this;
}
@@ -432,7 +433,7 @@ public class Update implements UpdateDefinition {
this.keysToUpdate.add(key);
}
protected void addMultiFieldOperation(String operator, String key, Object value) {
protected void addMultiFieldOperation(String operator, String key, @Nullable Object value) {
Assert.hasText(key, "Key/Path for update must not be null or blank.");
Object existingValue = this.modifierOps.get(operator);