Skip to content

Commit bcb5628

Browse files
committed
Polishing.
Update javadoc, add since tags. Add non-null assertions. Introduce overload for type(…) accepting a collection See #3286. Original pull request: #811.
1 parent 0b27635 commit bcb5628

File tree

2 files changed

+85
-39
lines changed

2 files changed

+85
-39
lines changed

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

+71-18
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
/**
5252
* Central class for creating queries. It follows a fluent API style so that you can easily chain together multiple
53-
* criteria. Static import of the 'Criteria.where' method will improve readability.
53+
* criteria. Static import of the {@link Criteria#where Criteria.where} method improves readability.
5454
*
5555
* @author Thomas Risberg
5656
* @author Oliver Gierke
@@ -388,7 +388,22 @@ public Criteria type(Type... types) {
388388
Assert.notNull(types, "Types must not be null!");
389389
Assert.noNullElements(types, "Types must not contain null.");
390390

391-
criteria.put("$type", Arrays.asList(types).stream().map(Type::value).collect(Collectors.toList()));
391+
return type(Arrays.asList(types));
392+
}
393+
394+
/**
395+
* Creates a criterion using the {@literal $type} operator.
396+
*
397+
* @param types must not be {@literal null}.
398+
* @return this.
399+
* @since 3.2
400+
* @see <a href="https://docs.mongodb.com/manual/reference/operator/query/type/">MongoDB Query operator: $type</a>
401+
*/
402+
public Criteria type(Collection<Type> types) {
403+
404+
Assert.notNull(types, "Types must not be null!");
405+
406+
criteria.put("$type", types.stream().map(Type::value).collect(Collectors.toList()));
392407
return this;
393408
}
394409

@@ -669,67 +684,103 @@ public BitwiseCriteriaOperators bits() {
669684
}
670685

671686
/**
672-
* Creates an 'or' criteria using the $or operator for all of the provided criteria
673-
* <p>
674-
* Note that mongodb doesn't support an $or operator to be wrapped in a $not operator.
687+
* Creates a criteria using the {@code $or} operator for all of the provided criteria.
675688
* <p>
689+
* Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator.
676690
*
677-
* @throws IllegalArgumentException if {@link #orOperator(Criteria...)} follows a not() call directly.
691+
* @throws IllegalArgumentException if this method follows a {@link #not()} call directly.
678692
* @param criteria must not be {@literal null}.
679693
* @return this.
680694
*/
681695
public Criteria orOperator(Criteria... criteria) {
696+
697+
Assert.notNull(criteria, "Criteria must not be null!");
698+
682699
return orOperator(Arrays.asList(criteria));
683700
}
684701

685702
/**
686-
* {@link #orOperator(Criteria...)}
703+
* Creates a criteria using the {@code $or} operator for all of the provided criteria.
704+
* <p>
705+
* Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator.
706+
*
707+
* @throws IllegalArgumentException if this method follows a {@link #not()} call directly.
708+
* @param criteria must not be {@literal null}.
709+
* @return this.
710+
* @since 3.2
687711
*/
688712
public Criteria orOperator(Collection<Criteria> criteria) {
713+
714+
Assert.notNull(criteria, "Criteria must not be null!");
715+
689716
BasicDBList bsonList = createCriteriaList(criteria);
690717
return registerCriteriaChainElement(new Criteria("$or").is(bsonList));
691718
}
692719

693720
/**
694-
* Creates a 'nor' criteria using the $nor operator for all of the provided criteria.
695-
* <p>
696-
* Note that mongodb doesn't support an $nor operator to be wrapped in a $not operator.
721+
* Creates a criteria using the {@code $nor} operator for all of the provided criteria.
697722
* <p>
723+
* Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator.
698724
*
699-
* @throws IllegalArgumentException if {@link #norOperator(Criteria...)} follows a not() call directly.
725+
* @throws IllegalArgumentException if this method follows a {@link #not()} call directly.
700726
* @param criteria must not be {@literal null}.
701727
* @return this.
702728
*/
703729
public Criteria norOperator(Criteria... criteria) {
730+
731+
Assert.notNull(criteria, "Criteria must not be null!");
732+
704733
return norOperator(Arrays.asList(criteria));
705734
}
706735

707736
/**
708-
* {@link #norOperator(Criteria...)}
737+
* Creates a criteria using the {@code $nor} operator for all of the provided criteria.
738+
* <p>
739+
* Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator.
740+
*
741+
* @throws IllegalArgumentException if this method follows a {@link #not()} call directly.
742+
* @param criteria must not be {@literal null}.
743+
* @return this.
744+
* @since 3.2
709745
*/
710746
public Criteria norOperator(Collection<Criteria> criteria) {
747+
748+
Assert.notNull(criteria, "Criteria must not be null!");
749+
711750
BasicDBList bsonList = createCriteriaList(criteria);
712751
return registerCriteriaChainElement(new Criteria("$nor").is(bsonList));
713752
}
714753

715754
/**
716-
* Creates an 'and' criteria using the $and operator for all of the provided criteria.
717-
* <p>
718-
* Note that mongodb doesn't support an $and operator to be wrapped in a $not operator.
755+
* Creates a criteria using the {@code $and} operator for all of the provided criteria.
719756
* <p>
757+
* Note that MongoDB doesn't support an {@code $and} operator to be wrapped in a {@code $not} operator.
720758
*
721-
* @throws IllegalArgumentException if {@link #andOperator(Criteria...)} follows a not() call directly.
759+
* @throws IllegalArgumentException if this method follows a {@link #not()} call directly.
722760
* @param criteria must not be {@literal null}.
723761
* @return this.
724762
*/
725763
public Criteria andOperator(Criteria... criteria) {
764+
765+
Assert.notNull(criteria, "Criteria must not be null!");
766+
726767
return andOperator(Arrays.asList(criteria));
727768
}
728769

729770
/**
730-
* {@link #andOperator(Criteria...)}
771+
* Creates a criteria using the {@code $and} operator for all of the provided criteria.
772+
* <p>
773+
* Note that MongoDB doesn't support an {@code $and} operator to be wrapped in a {@code $not} operator.
774+
*
775+
* @throws IllegalArgumentException if this method follows a {@link #not()} call directly.
776+
* @param criteria must not be {@literal null}.
777+
* @return this.
778+
* @since 3.2
731779
*/
732780
public Criteria andOperator(Collection<Criteria> criteria) {
781+
782+
Assert.notNull(criteria, "Criteria must not be null!");
783+
733784
BasicDBList bsonList = createCriteriaList(criteria);
734785
return registerCriteriaChainElement(new Criteria("$and").is(bsonList));
735786
}
@@ -832,11 +883,13 @@ private BasicDBList createCriteriaList(Collection<Criteria> criteria) {
832883
}
833884

834885
private void setValue(Document document, String key, Object value) {
886+
835887
Object existing = document.get(key);
888+
836889
if (existing == null) {
837890
document.put(key, value);
838891
} else {
839-
throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDocument, "
892+
throw new InvalidMongoDbApiUsageException("Due to limitations of the org.bson.Document, "
840893
+ "you can't add a second '" + key + "' expression specified as '" + key + " : " + value + "'. "
841894
+ "Criteria already contains '" + key + " : " + existing + "'.");
842895
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java

+14-21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
3232

3333
/**
34+
* Unit tests for {@link Criteria}.
35+
*
3436
* @author Oliver Gierke
3537
* @author Thomas Darimont
3638
* @author Christoph Strobl
@@ -82,52 +84,43 @@ public void equalIfCriteriaMatches() {
8284
assertThat(right).isNotEqualTo(left);
8385
}
8486

85-
@Test
87+
@Test // GH-3286
8688
public void shouldBuildCorrectAndOperator() {
87-
//given
89+
8890
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
8991
Criteria.where("y").is(42),
9092
Criteria.where("z").is("value"));
91-
Document expectedResult = Document
92-
.parse("{\"$and\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
9393

94-
//when
9594
Criteria criteria = Criteria.where("foo").is("bar").andOperator(operatorCriteria);
9695

97-
//then
98-
assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult);
96+
assertThat(criteria.getCriteriaObject())
97+
.isEqualTo("{\"$and\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
9998
}
10099

101-
@Test
100+
@Test // GH-3286
102101
public void shouldBuildCorrectOrOperator() {
103-
//given
102+
104103
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
105104
Criteria.where("y").is(42),
106105
Criteria.where("z").is("value"));
107-
Document expectedResult = Document
108-
.parse("{\"$or\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
109106

110-
//when
111107
Criteria criteria = Criteria.where("foo").is("bar").orOperator(operatorCriteria);
112108

113-
//then
114-
assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult);
109+
assertThat(criteria.getCriteriaObject())
110+
.isEqualTo("{\"$or\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
115111
}
116112

117-
@Test
113+
@Test // GH-3286
118114
public void shouldBuildCorrectNorOperator() {
119-
//given
115+
120116
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
121117
Criteria.where("y").is(42),
122118
Criteria.where("z").is("value"));
123-
Document expectedResult = Document
124-
.parse("{\"$nor\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
125119

126-
//when
127120
Criteria criteria = Criteria.where("foo").is("bar").norOperator(operatorCriteria);
128121

129-
//then
130-
assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult);
122+
assertThat(criteria.getCriteriaObject())
123+
.isEqualTo("{\"$nor\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
131124
}
132125

133126
@Test // DATAMONGO-507

0 commit comments

Comments
 (0)