Skip to content

Commit c0581c4

Browse files
committed
DATAMONGO-2294 - Polishing.
Reorganize imports after Delomboking. Use for-loop instead of Stream.forEach(…). Add Javadoc to methods. Add since tags. Simplify tests. Original pull request: #761.
1 parent 85022d2 commit c0581c4

File tree

3 files changed

+112
-61
lines changed

3 files changed

+112
-61
lines changed

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

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query;
1717

18-
import java.util.Arrays;
1918
import java.util.HashMap;
2019
import java.util.Map;
2120
import java.util.Map.Entry;
2221

23-
import lombok.EqualsAndHashCode;
24-
2522
import org.bson.Document;
23+
2624
import org.springframework.lang.Nullable;
2725
import org.springframework.util.Assert;
2826
import org.springframework.util.ObjectUtils;
2927

3028
/**
29+
* Field projection.
30+
*
3131
* @author Thomas Risberg
3232
* @author Oliver Gierke
3333
* @author Patryk Wasik
@@ -37,50 +37,112 @@
3737
*/
3838
public class Field {
3939

40-
private final Map<String, Integer> criteria = new HashMap<String, Integer>();
41-
private final Map<String, Object> slices = new HashMap<String, Object>();
42-
private final Map<String, Criteria> elemMatchs = new HashMap<String, Criteria>();
40+
private final Map<String, Integer> criteria = new HashMap<>();
41+
private final Map<String, Object> slices = new HashMap<>();
42+
private final Map<String, Criteria> elemMatchs = new HashMap<>();
4343
private @Nullable String positionKey;
4444
private int positionValue;
4545

46-
public Field include(String key) {
47-
criteria.put(key, Integer.valueOf(1));
46+
/**
47+
* Include a single {@code field} to be returned by the query operation.
48+
*
49+
* @param field the document field name to be included.
50+
* @return {@code this} field projection instance.
51+
*/
52+
public Field include(String field) {
53+
54+
Assert.notNull(field, "Key must not be null!");
55+
56+
criteria.put(field, 1);
57+
4858
return this;
4959
}
5060

51-
public Field includes(String... keys) {
52-
Assert.notNull(keys, "Keys must not be null!");
53-
Assert.notEmpty(keys, "Keys must not be empty!");
61+
/**
62+
* Include one or more {@code fields} to be returned by the query operation.
63+
*
64+
* @param fields the document field names to be included.
65+
* @return {@code this} field projection instance.
66+
* @since 3.1
67+
*/
68+
public Field include(String... fields) {
69+
70+
Assert.notNull(fields, "Keys must not be null!");
71+
72+
for (String key : fields) {
73+
criteria.put(key, 1);
74+
}
5475

55-
Arrays.asList(keys).stream().forEach(this::include);
5676
return this;
5777
}
5878

59-
public Field exclude(String key) {
60-
criteria.put(key, Integer.valueOf(0));
79+
/**
80+
* Exclude a single {@code field} from being returned by the query operation.
81+
*
82+
* @param field the document field name to be included.
83+
* @return {@code this} field projection instance.
84+
*/
85+
public Field exclude(String field) {
86+
87+
Assert.notNull(field, "Key must not be null!");
88+
89+
criteria.put(field, 0);
90+
6191
return this;
6292
}
6393

64-
public Field excludes(String... keys) {
65-
Assert.notNull(keys, "Keys must not be null!");
66-
Assert.notEmpty(keys, "Keys must not be empty!");
94+
/**
95+
* Exclude one or more {@code fields} from being returned by the query operation.
96+
*
97+
* @param fields the document field names to be included.
98+
* @return {@code this} field projection instance.
99+
* @since 3.1
100+
*/
101+
public Field exclude(String... fields) {
102+
103+
Assert.notNull(fields, "Keys must not be null!");
104+
105+
for (String key : fields) {
106+
criteria.put(key, 0);
107+
}
67108

68-
Arrays.asList(keys).stream().forEach(this::exclude);
69109
return this;
70110
}
71111

72-
public Field slice(String key, int size) {
73-
slices.put(key, Integer.valueOf(size));
112+
/**
113+
* Project a {@code $slice} of the array {@code field} using the first {@code size} elements.
114+
*
115+
* @param field the document field name to project, must be an array field.
116+
* @param size the number of elements to include.
117+
* @return {@code this} field projection instance.
118+
*/
119+
public Field slice(String field, int size) {
120+
121+
Assert.notNull(field, "Key must not be null!");
122+
123+
slices.put(field, size);
124+
74125
return this;
75126
}
76127

77-
public Field slice(String key, int offset, int size) {
78-
slices.put(key, new Integer[] { Integer.valueOf(offset), Integer.valueOf(size) });
128+
/**
129+
* Project a {@code $slice} of the array {@code field} using the first {@code size} elements starting at
130+
* {@code offset}.
131+
*
132+
* @param field the document field name to project, must be an array field.
133+
* @param offset the offset to start at.
134+
* @param size the number of elements to include.
135+
* @return {@code this} field projection instance.
136+
*/
137+
public Field slice(String field, int offset, int size) {
138+
139+
slices.put(field, new Integer[] { offset, size });
79140
return this;
80141
}
81142

82-
public Field elemMatch(String key, Criteria elemMatchCriteria) {
83-
elemMatchs.put(key, elemMatchCriteria);
143+
public Field elemMatch(String field, Criteria elemMatchCriteria) {
144+
145+
elemMatchs.put(field, elemMatchCriteria);
84146
return this;
85147
}
86148

@@ -90,7 +152,7 @@ public Field elemMatch(String key, Criteria elemMatchCriteria) {
90152
*
91153
* @param field query array field, must not be {@literal null} or empty.
92154
* @param value
93-
* @return
155+
* @return {@code this} field projection instance.
94156
*/
95157
public Field position(String field, int value) {
96158

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.assertj.core.api.Assertions.fail;
20-
import static org.hamcrest.Matchers.not;
21-
import static org.hamcrest.Matchers.*;
22-
import static org.junit.Assert.assertThat;
23-
import static org.junit.Assert.*;
24-
import static org.junit.Assume.*;
2518
import static org.assertj.core.api.Assertions.*;
2619
import static org.springframework.data.mongodb.core.query.Criteria.*;
2720
import static org.springframework.data.mongodb.core.query.Query.*;
2821
import static org.springframework.data.mongodb.core.query.Update.*;
2922

23+
import lombok.AllArgsConstructor;
24+
import lombok.Data;
25+
import lombok.EqualsAndHashCode;
26+
import lombok.NoArgsConstructor;
27+
import lombok.Value;
28+
import lombok.With;
29+
3030
import java.lang.reflect.InvocationTargetException;
3131
import java.math.BigDecimal;
3232
import java.math.BigInteger;
@@ -38,13 +38,6 @@
3838
import java.util.stream.Collectors;
3939
import java.util.stream.IntStream;
4040

41-
import lombok.AllArgsConstructor;
42-
import lombok.Data;
43-
import lombok.EqualsAndHashCode;
44-
import lombok.NoArgsConstructor;
45-
import lombok.Value;
46-
import lombok.experimental.Wither;
47-
4841
import org.bson.types.ObjectId;
4942
import org.joda.time.DateTime;
5043
import org.junit.jupiter.api.AfterEach;
@@ -98,7 +91,6 @@
9891
import com.mongodb.BasicDBObject;
9992
import com.mongodb.DBObject;
10093
import com.mongodb.DBRef;
101-
import com.mongodb.MongoClient;
10294
import com.mongodb.MongoException;
10395
import com.mongodb.ReadPreference;
10496
import com.mongodb.WriteConcern;
@@ -1847,7 +1839,7 @@ public void queryShouldSupportRealAndAliasedPropertyNamesForFieldInclusions() {
18471839
assertThat(result.property3).isEqualTo(obj.property3);
18481840
}
18491841

1850-
@Test // DATAMONGO-702
1842+
@Test // DATAMONGO-702, DATAMONGO-2294
18511843
public void queryShouldSupportRealAndAliasedPropertyNamesForFieldExclusions() {
18521844

18531845
ObjectWith3AliasedFields obj = new ObjectWith3AliasedFields();
@@ -1860,8 +1852,7 @@ public void queryShouldSupportRealAndAliasedPropertyNamesForFieldExclusions() {
18601852

18611853
Query query = new Query(Criteria.where("id").is(obj.id));
18621854
query.fields() //
1863-
.exclude("property2") // real property name
1864-
.exclude("prop3"); // aliased property name
1855+
.exclude("property2", "prop3"); // real property name, aliased property name
18651856

18661857
ObjectWith3AliasedFields result = template.findOne(query, ObjectWith3AliasedFields.class);
18671858

@@ -3688,7 +3679,7 @@ public void shouldProjectWithCollections() {
36883679
queryByChainedInclude.fields().include("id").include("name");
36893680

36903681
Query queryByCollectionInclude = query(where("name").is("Walter"));
3691-
queryByCollectionInclude.fields().includes("id", "name");
3682+
queryByCollectionInclude.fields().include("id", "name");
36923683

36933684
MyPerson first = template.findAndReplace(queryByChainedInclude, new MyPerson("Walter"));
36943685
MyPerson second = template.findAndReplace(queryByCollectionInclude, new MyPerson("Walter"));
@@ -4209,7 +4200,7 @@ public String toString() {
42094200
// DATAMONGO-1992
42104201

42114202
@AllArgsConstructor
4212-
@Wither
4203+
@With
42134204
static class ImmutableVersioned {
42144205

42154206
final @Id String id;
@@ -4222,7 +4213,7 @@ public ImmutableVersioned() {
42224213
}
42234214

42244215
@Value
4225-
@Wither
4216+
@With
42264217
static class ImmutableAudited {
42274218
@Id String id;
42284219
@LastModifiedDate Instant modified;

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,40 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.springframework.data.mongodb.test.util.Assertions.*;
1919

2020
import org.junit.jupiter.api.Test;
2121

2222
/**
23-
* Unit tests for {@link DocumentField}.
23+
* Unit tests for {@link Field}.
2424
*
2525
* @author Oliver Gierke
2626
* @author Owen Q
27+
* @author Mark Paluch
2728
*/
28-
public class FieldUnitTests {
29+
class FieldUnitTests {
2930

3031
@Test
31-
public void sameObjectSetupCreatesEqualField() {
32+
void sameObjectSetupCreatesEqualField() {
3233

3334
Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar"));
3435
Field right = new Field().elemMatch("key", Criteria.where("foo").is("bar"));
3536

3637
assertThat(left).isEqualTo(right);
3738
assertThat(right).isEqualTo(left);
39+
assertThat(left.getFieldsObject()).isEqualTo("{key: { $elemMatch: {foo:\"bar\"}}}");
3840
}
3941

4042
@Test // DATAMONGO-2294
41-
public void sameObjectSetupCreatesEqualFieldByCollections() {
43+
void rendersInclusionCorrectly() {
4244

43-
Field left = new Field().includes("foo", "bar");
44-
Field right = new Field().include("foo").include("bar");
45+
Field fields = new Field().include("foo", "bar").include("baz");
4546

46-
assertThat(left, is(right));
47-
assertThat(right, is(left));
47+
assertThat(fields.getFieldsObject()).isEqualTo("{foo:1, bar:1, baz:1}");
4848
}
4949

5050
@Test
51-
public void differentObjectSetupCreatesEqualField() {
51+
void differentObjectSetupCreatesEqualField() {
5252

5353
Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar"));
5454
Field right = new Field().elemMatch("key", Criteria.where("foo").is("foo"));
@@ -58,12 +58,10 @@ public void differentObjectSetupCreatesEqualField() {
5858
}
5959

6060
@Test // DATAMONGO-2294
61-
public void differentObjectSetupCreatesEqualFieldByCollections() {
61+
void rendersExclusionCorrectly() {
6262

63-
Field left = new Field().includes("foo", "bar");
64-
Field right = new Field().include("foo").include("zoo");
63+
Field fields = new Field().exclude("foo", "bar").exclude("baz");
6564

66-
assertThat(left, is(not(right)));
67-
assertThat(right, is(not(left)));
65+
assertThat(fields.getFieldsObject()).isEqualTo("{foo:0, bar:0, baz:0}");
6866
}
6967
}

0 commit comments

Comments
 (0)