15
15
*/
16
16
package org .springframework .data .mongodb .core .query ;
17
17
18
- import java .util .Arrays ;
19
18
import java .util .HashMap ;
20
19
import java .util .Map ;
21
20
import java .util .Map .Entry ;
22
21
23
- import lombok .EqualsAndHashCode ;
24
-
25
22
import org .bson .Document ;
23
+
26
24
import org .springframework .lang .Nullable ;
27
25
import org .springframework .util .Assert ;
28
26
import org .springframework .util .ObjectUtils ;
29
27
30
28
/**
29
+ * Field projection.
30
+ *
31
31
* @author Thomas Risberg
32
32
* @author Oliver Gierke
33
33
* @author Patryk Wasik
37
37
*/
38
38
public class Field {
39
39
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 <>();
43
43
private @ Nullable String positionKey ;
44
44
private int positionValue ;
45
45
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
+
48
58
return this ;
49
59
}
50
60
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
+ }
54
75
55
- Arrays .asList (keys ).stream ().forEach (this ::include );
56
76
return this ;
57
77
}
58
78
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
+
61
91
return this ;
62
92
}
63
93
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
+ }
67
108
68
- Arrays .asList (keys ).stream ().forEach (this ::exclude );
69
109
return this ;
70
110
}
71
111
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
+
74
125
return this ;
75
126
}
76
127
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 });
79
140
return this ;
80
141
}
81
142
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 );
84
146
return this ;
85
147
}
86
148
@@ -90,7 +152,7 @@ public Field elemMatch(String key, Criteria elemMatchCriteria) {
90
152
*
91
153
* @param field query array field, must not be {@literal null} or empty.
92
154
* @param value
93
- * @return
155
+ * @return {@code this} field projection instance.
94
156
*/
95
157
public Field position (String field , int value ) {
96
158
0 commit comments