151
151
* @author Borislav Rangelov
152
152
* @author duozhilin
153
153
* @author Andreas Zink
154
+ * @author Bartłomiej Mazur
154
155
*/
155
156
@ SuppressWarnings ("deprecation" )
156
157
public class MongoTemplate implements MongoOperations , ApplicationContextAware , IndexOperationsProvider {
157
158
158
159
private static final Logger LOGGER = LoggerFactory .getLogger (MongoTemplate .class );
159
160
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking .NONE ;
160
- private static final Collection <String > ITERABLE_CLASSES ;
161
-
162
- static {
163
-
164
- Set <String > iterableClasses = new HashSet <>();
165
- iterableClasses .add (List .class .getName ());
166
- iterableClasses .add (Collection .class .getName ());
167
- iterableClasses .add (Iterator .class .getName ());
168
-
169
- ITERABLE_CLASSES = Collections .unmodifiableCollection (iterableClasses );
170
- }
171
161
172
162
private final MongoConverter mongoConverter ;
173
163
private final MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > mappingContext ;
@@ -1129,7 +1119,7 @@ public <T> T insert(T objectToSave) {
1129
1119
1130
1120
Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
1131
1121
1132
- ensureNotIterable (objectToSave );
1122
+ ensureNotAnArrayOrCollection (objectToSave );
1133
1123
return insert (objectToSave , operations .determineEntityCollectionName (objectToSave ));
1134
1124
}
1135
1125
@@ -1144,13 +1134,13 @@ public <T> T insert(T objectToSave, String collectionName) {
1144
1134
Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
1145
1135
Assert .notNull (collectionName , "CollectionName must not be null!" );
1146
1136
1147
- ensureNotIterable (objectToSave );
1137
+ ensureNotAnArrayOrCollection (objectToSave );
1148
1138
return (T ) doInsert (collectionName , objectToSave , this .mongoConverter );
1149
1139
}
1150
1140
1151
- protected void ensureNotIterable (@ Nullable Object o ) {
1141
+ protected void ensureNotAnArrayOrCollection (@ Nullable Object o ) {
1152
1142
if (null != o ) {
1153
- if (o .getClass ().isArray () || ITERABLE_CLASSES . contains ( o . getClass (). getName () )) {
1143
+ if (o .getClass ().isArray () || ( o instanceof Collection ) || ( o instanceof Iterator )) {
1154
1144
throw new IllegalArgumentException ("Cannot use a collection here." );
1155
1145
}
1156
1146
}
0 commit comments