Browse Source

Polishing.

Fix contract annotations, add missing Override annotations, make fields final where possible.
hacking/annotation-polishing
Mark Paluch 7 months ago
parent
commit
a1887d530d
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 19
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java
  2. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

19
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

@ -15,7 +15,7 @@
*/ */
package org.springframework.data.mongodb.core.query; package org.springframework.data.mongodb.core.query;
import static org.springframework.util.ObjectUtils.nullSafeHashCode; import static org.springframework.util.ObjectUtils.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -34,6 +34,7 @@ import org.bson.BsonType;
import org.bson.Document; import org.bson.Document;
import org.bson.types.Binary; import org.bson.types.Binary;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.springframework.data.domain.Example; import org.springframework.data.domain.Example;
import org.springframework.data.geo.Circle; import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
@ -76,8 +77,8 @@ public class Criteria implements CriteriaDefinition {
private static final Object NOT_SET = new Object(); private static final Object NOT_SET = new Object();
private @Nullable String key; private @Nullable String key;
private List<Criteria> criteriaChain; private final List<Criteria> criteriaChain;
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>(); private final LinkedHashMap<String, @Nullable Object> criteria = new LinkedHashMap<String, Object>();
private @Nullable Object isValue = NOT_SET; private @Nullable Object isValue = NOT_SET;
public Criteria() { public Criteria() {
@ -120,7 +121,7 @@ public class Criteria implements CriteriaDefinition {
/** /**
* Static factory method to create a {@link Criteria} matching an example object. <br /> * Static factory method to create a {@link Criteria} matching an example object. <br />
* By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when * By default, the {@link Example} uses typed matching restricting it to probe assignable types. For example, when
* sticking with the default type key ({@code _class}), the query has restrictions such as * sticking with the default type key ({@code _class}), the query has restrictions such as
* <code>_class : &#123; $in : [com.acme.Person] &#125; </code>. <br /> * <code>_class : &#123; $in : [com.acme.Person] &#125; </code>. <br />
* To avoid the above-mentioned type restriction use an {@link UntypedExampleMatcher} with * To avoid the above-mentioned type restriction use an {@link UntypedExampleMatcher} with
@ -224,7 +225,7 @@ public class Criteria implements CriteriaDefinition {
* Missing Fields: Equality Filter</a> * Missing Fields: Equality Filter</a>
* @since 3.3 * @since 3.3
*/ */
@Contract("_ -> this") @Contract("-> this")
public Criteria isNull() { public Criteria isNull() {
return is(null); return is(null);
} }
@ -241,7 +242,7 @@ public class Criteria implements CriteriaDefinition {
* Fields: Type Check</a> * Fields: Type Check</a>
* @since 3.3 * @since 3.3
*/ */
@Contract("_ -> this") @Contract("-> this")
public Criteria isNullValue() { public Criteria isNullValue() {
criteria.put("$type", BsonType.NULL.getValue()); criteria.put("$type", BsonType.NULL.getValue());
@ -391,7 +392,7 @@ public class Criteria implements CriteriaDefinition {
* @return this. * @return this.
* @see <a href="https://docs.mongodb.com/manual/reference/operator/query/mod/">MongoDB Query operator: $mod</a> * @see <a href="https://docs.mongodb.com/manual/reference/operator/query/mod/">MongoDB Query operator: $mod</a>
*/ */
@Contract("_ -> this") @Contract("_, _ -> this")
public Criteria mod(Number value, Number remainder) { public Criteria mod(Number value, Number remainder) {
List<Object> l = new ArrayList<>(2); List<Object> l = new ArrayList<>(2);
l.add(value); l.add(value);
@ -818,7 +819,7 @@ public class Criteria implements CriteriaDefinition {
} }
/** /**
* Creates a criteria using the {@code $or} operator for all of the provided criteria. * Creates a criteria using the {@code $or} operator for all provided criteria.
* <p> * <p>
* Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator. * Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator.
* *
@ -933,6 +934,7 @@ public class Criteria implements CriteriaDefinition {
* @return this * @return this
* @since 5.0 * @since 5.0
*/ */
@Contract("_, _ -> this")
public Criteria raw(String operator, Object value) { public Criteria raw(String operator, Object value) {
criteria.put(operator, value); criteria.put(operator, value);
return this; return this;
@ -957,6 +959,7 @@ public class Criteria implements CriteriaDefinition {
return this.key; return this.key;
} }
@Override
public Document getCriteriaObject() { public Document getCriteriaObject() {
if (this.criteriaChain.size() == 1) { if (this.criteriaChain.size() == 1) {

8
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

@ -443,14 +443,17 @@ public class Update implements UpdateDefinition {
return this; return this;
} }
@Override
public Boolean isIsolated() { public Boolean isIsolated() {
return isolated; return isolated;
} }
@Override
public Document getUpdateObject() { public Document getUpdateObject() {
return new Document(modifierOps); return new Document(modifierOps);
} }
@Override
public List<ArrayFilter> getArrayFilters() { public List<ArrayFilter> getArrayFilters() {
return Collections.unmodifiableList(this.arrayFilters); return Collections.unmodifiableList(this.arrayFilters);
} }
@ -486,6 +489,7 @@ public class Update implements UpdateDefinition {
* @param key the field name. * @param key the field name.
* @return {@literal true} if given field is updated. * @return {@literal true} if given field is updated.
*/ */
@Override
public boolean modifies(String key) { public boolean modifies(String key) {
return this.keysToUpdate.contains(key); return this.keysToUpdate.contains(key);
} }
@ -544,7 +548,7 @@ public class Update implements UpdateDefinition {
*/ */
public static class Modifiers { public static class Modifiers {
private Map<String, Modifier> modifiers; private final Map<String, Modifier> modifiers;
public Modifiers() { public Modifiers() {
this.modifiers = new LinkedHashMap<>(1); this.modifiers = new LinkedHashMap<>(1);
@ -727,7 +731,7 @@ public class Update implements UpdateDefinition {
*/ */
private static class Slice extends AbstractModifier { private static class Slice extends AbstractModifier {
private int count; private final int count;
Slice(int count) { Slice(int count) {
this.count = count; this.count = count;

Loading…
Cancel
Save