Skip to content

Commit 3f5f18e

Browse files
authored
JAVA-5319: Allow decoding into property of type SortedSet or NavigableSet (#1306)
1 parent 52603c0 commit 3f5f18e

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

bson/src/main/org/bson/codecs/pojo/CollectionPropertyCodecProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.bson.codecs.pojo;
1717

18+
import java.util.TreeSet;
19+
1820
import org.bson.BsonReader;
1921
import org.bson.BsonType;
2022
import org.bson.BsonWriter;
@@ -89,6 +91,8 @@ private Collection<T> getInstance() {
8991
return new ArrayList<>();
9092
} else if (encoderClass.isAssignableFrom(HashSet.class)) {
9193
return new HashSet<>();
94+
} else if (encoderClass.isAssignableFrom(TreeSet.class)) {
95+
return new TreeSet<>();
9296
} else {
9397
throw new CodecConfigurationException(format("Unsupported Collection interface of %s!", encoderClass.getName()));
9498
}

bson/src/test/unit/org/bson/codecs/pojo/ClassModelTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.bson.codecs.pojo;
1818

19+
import java.util.SortedSet;
20+
1921
import org.bson.codecs.pojo.entities.CollectionNestedPojoModel;
2022
import org.bson.codecs.pojo.entities.ConcreteAndNestedAbstractInterfaceModel;
2123
import org.bson.codecs.pojo.entities.GenericHolderModel;
@@ -80,6 +82,7 @@ public void testCollectionNestedPojoModelPropertyTypes() {
8082
TypeData<List> listList = createBuilder(List.class).addTypeParameter(list).build();
8183
TypeData<Set> set = createBuilder(Set.class).addTypeParameter(simple).build();
8284
TypeData<Set> setSet = createBuilder(Set.class).addTypeParameter(set).build();
85+
TypeData<SortedSet> sortedSet = createBuilder(SortedSet.class).addTypeParameter(simple).build();
8386
TypeData<Map> map = createBuilder(Map.class).addTypeParameter(string).addTypeParameter(simple).build();
8487
TypeData<List> listMap = createBuilder(List.class).addTypeParameter(map).build();
8588
TypeData<Map> mapMap = createBuilder(Map.class).addTypeParameter(string).addTypeParameter(map).build();
@@ -91,13 +94,15 @@ public void testCollectionNestedPojoModelPropertyTypes() {
9194

9295
ClassModel<?> classModel = ClassModel.builder(CollectionNestedPojoModel.class).build();
9396

94-
assertEquals(12, classModel.getPropertyModels().size());
97+
assertEquals(13, classModel.getPropertyModels().size());
9598
assertEquals(classModel.getPropertyModel("listSimple").getTypeData(), list);
9699
assertEquals(classModel.getPropertyModel("listListSimple").getTypeData(), listList);
97100

98101
assertEquals(classModel.getPropertyModel("setSimple").getTypeData(), set);
99102
assertEquals(classModel.getPropertyModel("setSetSimple").getTypeData(), setSet);
100103

104+
assertEquals(classModel.getPropertyModel("sortedSetSimple").getTypeData(), sortedSet);
105+
101106
assertEquals(classModel.getPropertyModel("mapSimple").getTypeData(), map);
102107
assertEquals(classModel.getPropertyModel("mapMapSimple").getTypeData(), mapMap);
103108

bson/src/test/unit/org/bson/codecs/pojo/PojoRoundTripTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ private static List<TestData> testCases() {
218218
+ "'listListSimple': [[" + SIMPLE_MODEL_JSON + "]],"
219219
+ "'setSimple': [" + SIMPLE_MODEL_JSON + "],"
220220
+ "'setSetSimple': [[" + SIMPLE_MODEL_JSON + "]],"
221+
+ "'sortedSetSimple': [" + SIMPLE_MODEL_JSON + "],"
221222
+ "'mapSimple': {'s': " + SIMPLE_MODEL_JSON + "},"
222223
+ "'mapMapSimple': {'ms': {'s': " + SIMPLE_MODEL_JSON + "}},"
223224
+ "'mapListSimple': {'ls': [" + SIMPLE_MODEL_JSON + "]},"

bson/src/test/unit/org/bson/codecs/pojo/PojoTestCase.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.bson.codecs.pojo;
1818

19+
import java.util.SortedSet;
20+
import java.util.TreeSet;
21+
1922
import org.bson.BsonBinaryReader;
2023
import org.bson.BsonBinaryWriter;
2124
import org.bson.BsonDocument;
@@ -268,16 +271,19 @@ static CollectionNestedPojoModel getCollectionNestedPojoModelWithNulls() {
268271
private static CollectionNestedPojoModel getCollectionNestedPojoModel(final boolean useNulls) {
269272
List<SimpleModel> listSimple;
270273
Set<SimpleModel> setSimple;
274+
SortedSet<SimpleModel> sortedSetSimple;
271275
Map<String, SimpleModel> mapSimple;
272276

273277
if (useNulls) {
274278
listSimple = null;
275279
setSimple = null;
280+
sortedSetSimple = null;
276281
mapSimple = null;
277282
} else {
278283
SimpleModel simpleModel = getSimpleModel();
279284
listSimple = singletonList(simpleModel);
280285
setSimple = new HashSet<>(listSimple);
286+
sortedSetSimple = new TreeSet<>(listSimple);
281287
mapSimple = new HashMap<>();
282288
mapSimple.put("s", simpleModel);
283289
}
@@ -301,8 +307,8 @@ private static CollectionNestedPojoModel getCollectionNestedPojoModel(final bool
301307
List<Map<String, List<SimpleModel>>> listMapListSimple = singletonList(mapListSimple);
302308
List<Map<String, Set<SimpleModel>>> listMapSetSimple = singletonList(mapSetSimple);
303309

304-
return new CollectionNestedPojoModel(listSimple, listListSimple, setSimple, setSetSimple, mapSimple, mapMapSimple, mapListSimple,
305-
mapListMapSimple, mapSetSimple, listMapSimple, listMapListSimple, listMapSetSimple);
310+
return new CollectionNestedPojoModel(listSimple, listListSimple, setSimple, setSetSimple, sortedSetSimple,
311+
mapSimple, mapMapSimple, mapListSimple, mapListMapSimple, mapSetSimple, listMapSimple, listMapListSimple, listMapSetSimple);
306312
}
307313

308314
static ConventionModel getConventionModel() {

bson/src/test/unit/org/bson/codecs/pojo/entities/CollectionNestedPojoModel.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121
import java.util.Set;
22+
import java.util.SortedSet;
2223

2324
import static java.util.Collections.singletonList;
2425

@@ -32,6 +33,8 @@ public final class CollectionNestedPojoModel {
3233
private Set<SimpleModel> setSimple;
3334
private Set<Set<SimpleModel>> setSetSimple;
3435

36+
private SortedSet<SimpleModel> sortedSetSimple;
37+
3538
private Map<String, SimpleModel> mapSimple;
3639
private Map<String, Map<String, SimpleModel>> mapMapSimple;
3740

@@ -47,7 +50,8 @@ public CollectionNestedPojoModel() {
4750
}
4851

4952
public CollectionNestedPojoModel(final List<SimpleModel> listSimple, final List<List<SimpleModel>> listListSimple, final
50-
Set<SimpleModel> setSimple, final Set<Set<SimpleModel>> setSetSimple, final Map<String, SimpleModel> mapSimple, final Map<String,
53+
Set<SimpleModel> setSimple, final Set<Set<SimpleModel>> setSetSimple, final SortedSet<SimpleModel> sortedSetSimple,
54+
final Map<String, SimpleModel> mapSimple, final Map<String,
5155
Map<String, SimpleModel>> mapMapSimple, final Map<String, List<SimpleModel>> mapListSimple, final Map<String,
5256
List<Map<String, SimpleModel>>> mapListMapSimple, final Map<String, Set<SimpleModel>> mapSetSimple, final List<Map<String,
5357
SimpleModel>> listMapSimple, final List<Map<String, List<SimpleModel>>> listMapListSimple, final List<Map<String,
@@ -56,6 +60,7 @@ public CollectionNestedPojoModel(final List<SimpleModel> listSimple, final List<
5660
this.listListSimple = listListSimple;
5761
this.setSimple = setSimple;
5862
this.setSetSimple = setSetSimple;
63+
this.sortedSetSimple = sortedSetSimple;
5964
this.mapSimple = mapSimple;
6065
this.mapMapSimple = mapMapSimple;
6166
this.mapListSimple = mapListSimple;
@@ -98,6 +103,14 @@ public void setSetSimple(final Set<SimpleModel> setSimple) {
98103
this.setSimple = setSimple;
99104
}
100105

106+
public SortedSet<SimpleModel> getSortedSetSimple() {
107+
return sortedSetSimple;
108+
}
109+
110+
public void setSortedSetSimple(final SortedSet<SimpleModel> sortedSetSimple) {
111+
this.sortedSetSimple = sortedSetSimple;
112+
}
113+
101114
public Set<Set<SimpleModel>> getSetSetSimple() {
102115
return setSetSimple;
103116
}
@@ -193,6 +206,9 @@ public boolean equals(final Object o) {
193206
if (getSetSetSimple() != null ? !getSetSetSimple().equals(that.getSetSetSimple()) : that.getSetSetSimple() != null) {
194207
return false;
195208
}
209+
if (getSortedSetSimple() != null ? !getSortedSetSimple().equals(that.getSortedSetSimple()) : that.getSortedSetSimple() != null) {
210+
return false;
211+
}
196212
if (getMapSimple() != null ? !getMapSimple().equals(that.getMapSimple()) : that.getMapSimple() != null) {
197213
return false;
198214
}
@@ -230,6 +246,7 @@ public int hashCode() {
230246
result = 31 * result + (getListListSimple() != null ? getListListSimple().hashCode() : 0);
231247
result = 31 * result + (getSetSimple() != null ? getSetSimple().hashCode() : 0);
232248
result = 31 * result + (getSetSetSimple() != null ? getSetSetSimple().hashCode() : 0);
249+
result = 31 * result + (getSortedSetSimple() != null ? getSortedSetSimple().hashCode() : 0);
233250
result = 31 * result + (getMapSimple() != null ? getMapSimple().hashCode() : 0);
234251
result = 31 * result + (getMapMapSimple() != null ? getMapMapSimple().hashCode() : 0);
235252
result = 31 * result + (getMapListSimple() != null ? getMapListSimple().hashCode() : 0);
@@ -248,6 +265,7 @@ public String toString() {
248265
+ ", listListSimple=" + listListSimple
249266
+ ", setSimple=" + setSimple
250267
+ ", setSetSimple=" + setSetSimple
268+
+ ", setSortedSimple=" + sortedSetSimple
251269
+ ", mapSimple=" + mapSimple
252270
+ ", mapMapSimple=" + mapMapSimple
253271
+ ", mapListSimple=" + mapListSimple

bson/src/test/unit/org/bson/codecs/pojo/entities/SimpleModel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.bson.codecs.pojo.entities;
1818

19-
public final class SimpleModel {
19+
public final class SimpleModel implements Comparable<SimpleModel> {
2020
private Integer integerField;
2121
private String stringField;
2222

@@ -79,4 +79,10 @@ public String toString() {
7979
+ ", stringField='" + stringField + "'"
8080
+ "}";
8181
}
82+
83+
@Override
84+
public int compareTo(final SimpleModel o) {
85+
int integerFieldCompareResult = this.integerField.compareTo(o.integerField);
86+
return integerFieldCompareResult == 0 ? this.stringField.compareTo(o.stringField) : integerFieldCompareResult;
87+
}
8288
}

0 commit comments

Comments
 (0)