Skip to content

Commit 5a8e4f3

Browse files
committed
DATAMONGO-1194 - Polishing.
Some missing JavaDoc and slight code polish. Original pull request: #377.
1 parent 5d50155 commit 5a8e4f3

File tree

4 files changed

+78
-45
lines changed

4 files changed

+78
-45
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,19 +440,31 @@ private synchronized Object resolve() {
440440

441441
/**
442442
* {@link Comparator} for sorting {@link DBObject} that have been loaded in random order by a predefined list of
443-
* reference ids.
443+
* reference identifiers.
444444
*
445445
* @author Christoph Strobl
446+
* @author Oliver Gierke
446447
* @since 1.10
447448
*/
448449
private static class DbRefByReferencePositionComparator implements Comparator<DBObject> {
449450

450-
List<Object> reference;
451+
private final List<Object> reference;
451452

453+
/**
454+
* Creates a new {@link DbRefByReferencePositionComparator} for the given list of reference identifiers.
455+
*
456+
* @param referenceIds must not be {@literal null}.
457+
*/
452458
public DbRefByReferencePositionComparator(List<Object> referenceIds) {
453-
reference = new ArrayList<Object>(referenceIds);
459+
460+
Assert.notNull(referenceIds, "Reference identifiers must not be null!");
461+
this.reference = new ArrayList<Object>(referenceIds);
454462
}
455463

464+
/*
465+
* (non-Javadoc)
466+
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
467+
*/
456468
@Override
457469
public int compare(DBObject o1, DBObject o2) {
458470
return Integer.compare(reference.indexOf(o1.get("_id")), reference.indexOf(o2.get("_id")));

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Map.Entry;
27+
import java.util.Set;
2728

2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
@@ -1239,8 +1240,8 @@ private void bulkReadAndConvertDBRefMapIntoTarget(TypeInformation<?> valueType,
12391240
LinkedHashMap<String, Object> referenceMap = new LinkedHashMap<String, Object>(sourceMap);
12401241
List<Object> convertedObjects = bulkReadAndConvertDBRefs((List<DBRef>) new ArrayList(referenceMap.values()),
12411242
valueType, ObjectPath.ROOT, rawValueType);
1242-
12431243
int index = 0;
1244+
12441245
for (String key : referenceMap.keySet()) {
12451246
targetMap.put(key, convertedObjects.get(index));
12461247
index++;
@@ -1255,9 +1256,9 @@ private <T> List<T> bulkReadAndConvertDBRefs(List<DBRef> dbrefs, TypeInformation
12551256
return Collections.emptyList();
12561257
}
12571258

1258-
final List<DBObject> referencedRawDocuments = dbrefs.size() == 1
1259+
List<DBObject> referencedRawDocuments = dbrefs.size() == 1
12591260
? Collections.singletonList(readRef(dbrefs.iterator().next())) : bulkReadRefs(dbrefs);
1260-
final String collectionName = dbrefs.iterator().next().getCollectionName();
1261+
String collectionName = dbrefs.iterator().next().getCollectionName();
12611262

12621263
List<T> targeList = new ArrayList<T>(dbrefs.size());
12631264

@@ -1278,27 +1279,6 @@ private <T> List<T> bulkReadAndConvertDBRefs(List<DBRef> dbrefs, TypeInformation
12781279
return targeList;
12791280
}
12801281

1281-
private boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Collection<Object> source) {
1282-
1283-
String collection = null;
1284-
1285-
for (Object dbObjItem : source) {
1286-
1287-
if (!(dbObjItem instanceof DBRef)) {
1288-
return false;
1289-
}
1290-
1291-
DBRef ref = (DBRef) dbObjItem;
1292-
1293-
if (collection != null && !collection.equals(ref.getCollectionName())) {
1294-
return false;
1295-
}
1296-
collection = ref.getCollectionName();
1297-
}
1298-
1299-
return true;
1300-
}
1301-
13021282
private void maybeEmitEvent(MongoMappingEvent<?> event) {
13031283

13041284
if (canPublishEvent()) {
@@ -1331,6 +1311,34 @@ List<DBObject> bulkReadRefs(List<DBRef> references) {
13311311
return dbRefResolver.bulkFetch(references);
13321312
}
13331313

1314+
/**
1315+
* Returns whether the given {@link Iterable} contains {@link DBRef} instances all pointing to the same collection.
1316+
*
1317+
* @param source must not be {@literal null}.
1318+
* @return
1319+
*/
1320+
private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<Object> source) {
1321+
1322+
Assert.notNull(source, "Iterable of DBRefs must not be null!");
1323+
1324+
Set<String> collectionsFound = new HashSet<String>();
1325+
1326+
for (Object dbObjItem : source) {
1327+
1328+
if (!(dbObjItem instanceof DBRef)) {
1329+
return false;
1330+
}
1331+
1332+
collectionsFound.add(((DBRef) dbObjItem).getCollectionName());
1333+
1334+
if (collectionsFound.size() > 1) {
1335+
return false;
1336+
}
1337+
}
1338+
1339+
return true;
1340+
}
1341+
13341342
/**
13351343
* Marker class used to indicate we have a non root document object here that might be used within an update - so we
13361344
* need to preserve type hints for potential nested elements but need to remove it on top level.

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -200,7 +200,8 @@ public void lazyLoadingProxyForLazyDbRefOnConcreteCollection() {
200200

201201
BasicDBObject dbo = new BasicDBObject();
202202
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs();
203-
lazyDbRefs.dbRefToConcreteCollection = new ArrayList<LazyDbRefTarget>(Arrays.asList(new LazyDbRefTarget(id, value)));
203+
lazyDbRefs.dbRefToConcreteCollection = new ArrayList<LazyDbRefTarget>(
204+
Arrays.asList(new LazyDbRefTarget(id, value)));
204205
converterSpy.write(lazyDbRefs, dbo);
205206

206207
ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, dbo);
@@ -248,8 +249,8 @@ public void lazyLoadingProxyForLazyDbRefOnConcreteTypeWithPersistenceConstructor
248249

249250
BasicDBObject dbo = new BasicDBObject();
250251
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs();
251-
lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructor = new LazyDbRefTargetWithPeristenceConstructor(
252-
(Object) id, (Object) value);
252+
lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructor = new LazyDbRefTargetWithPeristenceConstructor((Object) id,
253+
(Object) value);
253254
converterSpy.write(lazyDbRefs, dbo);
254255

255256
ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, dbo);
@@ -733,18 +734,23 @@ static class ClassWithLazyDbRefs {
733734

734735
@Id String id;
735736
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) List<LazyDbRefTarget> dbRefToInterface;
736-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) ArrayList<LazyDbRefTarget> dbRefToConcreteCollection;
737+
@org.springframework.data.mongodb.core.mapping.DBRef(
738+
lazy = true) ArrayList<LazyDbRefTarget> dbRefToConcreteCollection;
737739
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) LazyDbRefTarget dbRefToConcreteType;
738-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) LazyDbRefTargetPropertyAccess dbRefToConcreteTypeWithPropertyAccess;
739-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) LazyDbRefTargetWithPeristenceConstructor dbRefToConcreteTypeWithPersistenceConstructor;
740-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor;
740+
@org.springframework.data.mongodb.core.mapping.DBRef(
741+
lazy = true) LazyDbRefTargetPropertyAccess dbRefToConcreteTypeWithPropertyAccess;
742+
@org.springframework.data.mongodb.core.mapping.DBRef(
743+
lazy = true) LazyDbRefTargetWithPeristenceConstructor dbRefToConcreteTypeWithPersistenceConstructor;
744+
@org.springframework.data.mongodb.core.mapping.DBRef(
745+
lazy = true) LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor;
741746
}
742747

743748
static class SerializableClassWithLazyDbRefs implements Serializable {
744749

745750
private static final long serialVersionUID = 1L;
746751

747-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) SerializableLazyDbRefTarget dbRefToSerializableTarget;
752+
@org.springframework.data.mongodb.core.mapping.DBRef(
753+
lazy = true) SerializableLazyDbRefTarget dbRefToSerializableTarget;
748754
}
749755

750756
static class LazyDbRefTarget implements Serializable {
@@ -901,9 +907,12 @@ public boolean equals(Object obj) {
901907
static class WithObjectMethodOverrideLazyDbRefs {
902908

903909
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) LazyDbRefTarget dbRefToPlainObject;
904-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) ToStringObjectMethodOverrideLazyDbRefTarget dbRefToToStringObjectMethodOverride;
905-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) EqualsAndHashCodeObjectMethodOverrideLazyDbRefTarget dbRefEqualsAndHashcodeObjectMethodOverride2;
906-
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) EqualsAndHashCodeObjectMethodOverrideLazyDbRefTarget dbRefEqualsAndHashcodeObjectMethodOverride1;
910+
@org.springframework.data.mongodb.core.mapping.DBRef(
911+
lazy = true) ToStringObjectMethodOverrideLazyDbRefTarget dbRefToToStringObjectMethodOverride;
912+
@org.springframework.data.mongodb.core.mapping.DBRef(
913+
lazy = true) EqualsAndHashCodeObjectMethodOverrideLazyDbRefTarget dbRefEqualsAndHashcodeObjectMethodOverride2;
914+
@org.springframework.data.mongodb.core.mapping.DBRef(
915+
lazy = true) EqualsAndHashCodeObjectMethodOverrideLazyDbRefTarget dbRefEqualsAndHashcodeObjectMethodOverride1;
907916
}
908917

909918
class ClassWithDbRefField {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolverUnitTests.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
19-
import static org.hamcrest.collection.IsIterableWithSize.*;
18+
import static org.hamcrest.Matchers.*;
19+
import static org.hamcrest.Matchers.contains;
2020
import static org.junit.Assert.*;
2121
import static org.mockito.Matchers.*;
2222
import static org.mockito.Mockito.*;
@@ -30,6 +30,7 @@
3030
import org.junit.runner.RunWith;
3131
import org.mockito.ArgumentCaptor;
3232
import org.mockito.Mock;
33+
import org.mockito.Mockito;
3334
import org.mockito.runners.MockitoJUnitRunner;
3435
import org.springframework.dao.InvalidDataAccessApiUsageException;
3536
import org.springframework.data.mongodb.MongoDbFactory;
@@ -43,7 +44,10 @@
4344
import com.mongodb.DBRef;
4445

4546
/**
47+
* Unit tests for {@link DefaultDbRefResolver}.
48+
*
4649
* @author Christoph Strobl
50+
* @author Oliver Gierke
4751
*/
4852
@RunWith(MockitoJUnitRunner.class)
4953
public class DefaultDbRefResolverUnitTests {
@@ -59,8 +63,8 @@ public void setUp() {
5963

6064
when(factoryMock.getDb()).thenReturn(dbMock);
6165
when(dbMock.getCollection(anyString())).thenReturn(collectionMock);
62-
when(collectionMock.find(any(DBObject.class))).thenReturn(cursorMock);
63-
when(cursorMock.toArray()).thenReturn(Collections.<DBObject> emptyList());
66+
when(collectionMock.find(Mockito.any(DBObject.class))).thenReturn(cursorMock);
67+
when(cursorMock.toArray()).thenReturn(Collections.<DBObject>emptyList());
6468

6569
resolver = new DefaultDbRefResolver(factoryMock);
6670
}
@@ -105,9 +109,9 @@ public void bulkFetchShouldThrowExceptionWhenUsingDifferntCollectionsWithinSetOf
105109
@Test
106110
public void bulkFetchShouldReturnEarlyForEmptyLists() {
107111

108-
resolver.bulkFetch(Collections.<DBRef> emptyList());
112+
resolver.bulkFetch(Collections.<DBRef>emptyList());
109113

110-
verify(collectionMock, never()).find(any(DBObject.class));
114+
verify(collectionMock, never()).find(Mockito.any(DBObject.class));
111115
}
112116

113117
/**

0 commit comments

Comments
 (0)