157
157
* @author Roman Puchkovskiy
158
158
* @author Yadhukrishna S Pai
159
159
* @author Anton Barkan
160
+ * @author Bartłomiej Mazur
160
161
*/
161
162
public class MongoTemplate implements MongoOperations , ApplicationContextAware , IndexOperationsProvider {
162
163
163
164
private static final Logger LOGGER = LoggerFactory .getLogger (MongoTemplate .class );
164
165
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking .NONE ;
165
- private static final Collection <String > ITERABLE_CLASSES ;
166
-
167
- static {
168
-
169
- Set <String > iterableClasses = new HashSet <>();
170
- iterableClasses .add (List .class .getName ());
171
- iterableClasses .add (Collection .class .getName ());
172
- iterableClasses .add (Iterator .class .getName ());
173
-
174
- ITERABLE_CLASSES = Collections .unmodifiableCollection (iterableClasses );
175
- }
176
166
177
167
private final MongoConverter mongoConverter ;
178
168
private final MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > mappingContext ;
@@ -1164,7 +1154,7 @@ public <T> T insert(T objectToSave) {
1164
1154
1165
1155
Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
1166
1156
1167
- ensureNotIterable (objectToSave );
1157
+ ensureNotAnArrayOrCollection (objectToSave );
1168
1158
return insert (objectToSave , getCollectionName (ClassUtils .getUserClass (objectToSave )));
1169
1159
}
1170
1160
@@ -1179,13 +1169,13 @@ public <T> T insert(T objectToSave, String collectionName) {
1179
1169
Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
1180
1170
Assert .notNull (collectionName , "CollectionName must not be null!" );
1181
1171
1182
- ensureNotIterable (objectToSave );
1172
+ ensureNotAnArrayOrCollection (objectToSave );
1183
1173
return (T ) doInsert (collectionName , objectToSave , this .mongoConverter );
1184
1174
}
1185
1175
1186
- protected void ensureNotIterable (@ Nullable Object o ) {
1187
- if (o != null ) {
1188
- if (o .getClass ().isArray () || ITERABLE_CLASSES . contains ( o . getClass (). getName () )) {
1176
+ protected void ensureNotAnArrayOrCollection (@ Nullable Object o ) {
1177
+ if (null != o ) {
1178
+ if (o .getClass ().isArray () || ( o instanceof Collection ) || ( o instanceof Iterator )) {
1189
1179
throw new IllegalArgumentException ("Cannot use a collection here." );
1190
1180
}
1191
1181
}
0 commit comments