1515 */
1616package org .springframework .data .mongodb .core .query ;
1717
18- import java .util .Arrays ;
1918import java .util .HashMap ;
2019import java .util .Map ;
2120import java .util .Map .Entry ;
2221
23- import lombok .EqualsAndHashCode ;
24-
2522import org .bson .Document ;
23+
2624import org .springframework .lang .Nullable ;
2725import org .springframework .util .Assert ;
2826import org .springframework .util .ObjectUtils ;
2927
3028/**
29+ * Field projection.
30+ *
3131 * @author Thomas Risberg
3232 * @author Oliver Gierke
3333 * @author Patryk Wasik
3737 */
3838public 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
0 commit comments