157157 * @author Roman Puchkovskiy
158158 * @author Yadhukrishna S Pai
159159 * @author Anton Barkan
160+ * @author Bartłomiej Mazur
160161 */
161162public class MongoTemplate implements MongoOperations , ApplicationContextAware , IndexOperationsProvider {
162163
163164 private static final Logger LOGGER = LoggerFactory .getLogger (MongoTemplate .class );
164165 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- }
176166
177167 private final MongoConverter mongoConverter ;
178168 private final MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > mappingContext ;
@@ -1164,7 +1154,7 @@ public <T> T insert(T objectToSave) {
11641154
11651155 Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
11661156
1167- ensureNotIterable (objectToSave );
1157+ ensureNotAnArrayOrCollection (objectToSave );
11681158 return insert (objectToSave , getCollectionName (ClassUtils .getUserClass (objectToSave )));
11691159 }
11701160
@@ -1179,13 +1169,13 @@ public <T> T insert(T objectToSave, String collectionName) {
11791169 Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
11801170 Assert .notNull (collectionName , "CollectionName must not be null!" );
11811171
1182- ensureNotIterable (objectToSave );
1172+ ensureNotAnArrayOrCollection (objectToSave );
11831173 return (T ) doInsert (collectionName , objectToSave , this .mongoConverter );
11841174 }
11851175
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 )) {
11891179 throw new IllegalArgumentException ("Cannot use a collection here." );
11901180 }
11911181 }
0 commit comments