Skip to content

JAVA-5319: Allow Decoding Data Into TreeSet #1306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.bson.codecs.pojo;

import java.util.TreeSet;

import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
Expand Down Expand Up @@ -89,6 +91,8 @@ private Collection<T> getInstance() {
return new ArrayList<>();
} else if (encoderClass.isAssignableFrom(HashSet.class)) {
return new HashSet<>();
} else if (encoderClass.isAssignableFrom(TreeSet.class)) {
return new TreeSet<>();
} else {
throw new CodecConfigurationException(format("Unsupported Collection interface of %s!", encoderClass.getName()));
}
Expand Down
7 changes: 6 additions & 1 deletion bson/src/test/unit/org/bson/codecs/pojo/ClassModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.bson.codecs.pojo;

import java.util.SortedSet;

import org.bson.codecs.pojo.entities.CollectionNestedPojoModel;
import org.bson.codecs.pojo.entities.ConcreteAndNestedAbstractInterfaceModel;
import org.bson.codecs.pojo.entities.GenericHolderModel;
Expand Down Expand Up @@ -80,6 +82,7 @@ public void testCollectionNestedPojoModelPropertyTypes() {
TypeData<List> listList = createBuilder(List.class).addTypeParameter(list).build();
TypeData<Set> set = createBuilder(Set.class).addTypeParameter(simple).build();
TypeData<Set> setSet = createBuilder(Set.class).addTypeParameter(set).build();
TypeData<SortedSet> sortedSet = createBuilder(SortedSet.class).addTypeParameter(simple).build();
TypeData<Map> map = createBuilder(Map.class).addTypeParameter(string).addTypeParameter(simple).build();
TypeData<List> listMap = createBuilder(List.class).addTypeParameter(map).build();
TypeData<Map> mapMap = createBuilder(Map.class).addTypeParameter(string).addTypeParameter(map).build();
Expand All @@ -91,13 +94,15 @@ public void testCollectionNestedPojoModelPropertyTypes() {

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

assertEquals(12, classModel.getPropertyModels().size());
assertEquals(13, classModel.getPropertyModels().size());
assertEquals(classModel.getPropertyModel("listSimple").getTypeData(), list);
assertEquals(classModel.getPropertyModel("listListSimple").getTypeData(), listList);

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

assertEquals(classModel.getPropertyModel("sortedSetSimple").getTypeData(), sortedSet);

assertEquals(classModel.getPropertyModel("mapSimple").getTypeData(), map);
assertEquals(classModel.getPropertyModel("mapMapSimple").getTypeData(), mapMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ private static List<TestData> testCases() {
+ "'listListSimple': [[" + SIMPLE_MODEL_JSON + "]],"
+ "'setSimple': [" + SIMPLE_MODEL_JSON + "],"
+ "'setSetSimple': [[" + SIMPLE_MODEL_JSON + "]],"
+ "'sortedSetSimple': [" + SIMPLE_MODEL_JSON + "],"
+ "'mapSimple': {'s': " + SIMPLE_MODEL_JSON + "},"
+ "'mapMapSimple': {'ms': {'s': " + SIMPLE_MODEL_JSON + "}},"
+ "'mapListSimple': {'ls': [" + SIMPLE_MODEL_JSON + "]},"
Expand Down
10 changes: 8 additions & 2 deletions bson/src/test/unit/org/bson/codecs/pojo/PojoTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.bson.codecs.pojo;

import java.util.SortedSet;
import java.util.TreeSet;

import org.bson.BsonBinaryReader;
import org.bson.BsonBinaryWriter;
import org.bson.BsonDocument;
Expand Down Expand Up @@ -268,16 +271,19 @@ static CollectionNestedPojoModel getCollectionNestedPojoModelWithNulls() {
private static CollectionNestedPojoModel getCollectionNestedPojoModel(final boolean useNulls) {
List<SimpleModel> listSimple;
Set<SimpleModel> setSimple;
SortedSet<SimpleModel> sortedSetSimple;
Map<String, SimpleModel> mapSimple;

if (useNulls) {
listSimple = null;
setSimple = null;
sortedSetSimple = null;
mapSimple = null;
} else {
SimpleModel simpleModel = getSimpleModel();
listSimple = singletonList(simpleModel);
setSimple = new HashSet<>(listSimple);
sortedSetSimple = new TreeSet<>(listSimple);
mapSimple = new HashMap<>();
mapSimple.put("s", simpleModel);
}
Expand All @@ -301,8 +307,8 @@ private static CollectionNestedPojoModel getCollectionNestedPojoModel(final bool
List<Map<String, List<SimpleModel>>> listMapListSimple = singletonList(mapListSimple);
List<Map<String, Set<SimpleModel>>> listMapSetSimple = singletonList(mapSetSimple);

return new CollectionNestedPojoModel(listSimple, listListSimple, setSimple, setSetSimple, mapSimple, mapMapSimple, mapListSimple,
mapListMapSimple, mapSetSimple, listMapSimple, listMapListSimple, listMapSetSimple);
return new CollectionNestedPojoModel(listSimple, listListSimple, setSimple, setSetSimple, sortedSetSimple,
mapSimple, mapMapSimple, mapListSimple, mapListMapSimple, mapSetSimple, listMapSimple, listMapListSimple, listMapSetSimple);
}

static ConventionModel getConventionModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;

import static java.util.Collections.singletonList;

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

private SortedSet<SimpleModel> sortedSetSimple;

private Map<String, SimpleModel> mapSimple;
private Map<String, Map<String, SimpleModel>> mapMapSimple;

Expand All @@ -47,7 +50,8 @@ public CollectionNestedPojoModel() {
}

public CollectionNestedPojoModel(final List<SimpleModel> listSimple, final List<List<SimpleModel>> listListSimple, final
Set<SimpleModel> setSimple, final Set<Set<SimpleModel>> setSetSimple, final Map<String, SimpleModel> mapSimple, final Map<String,
Set<SimpleModel> setSimple, final Set<Set<SimpleModel>> setSetSimple, final SortedSet<SimpleModel> sortedSetSimple,
final Map<String, SimpleModel> mapSimple, final Map<String,
Map<String, SimpleModel>> mapMapSimple, final Map<String, List<SimpleModel>> mapListSimple, final Map<String,
List<Map<String, SimpleModel>>> mapListMapSimple, final Map<String, Set<SimpleModel>> mapSetSimple, final List<Map<String,
SimpleModel>> listMapSimple, final List<Map<String, List<SimpleModel>>> listMapListSimple, final List<Map<String,
Expand All @@ -56,6 +60,7 @@ public CollectionNestedPojoModel(final List<SimpleModel> listSimple, final List<
this.listListSimple = listListSimple;
this.setSimple = setSimple;
this.setSetSimple = setSetSimple;
this.sortedSetSimple = sortedSetSimple;
this.mapSimple = mapSimple;
this.mapMapSimple = mapMapSimple;
this.mapListSimple = mapListSimple;
Expand Down Expand Up @@ -98,6 +103,14 @@ public void setSetSimple(final Set<SimpleModel> setSimple) {
this.setSimple = setSimple;
}

public SortedSet<SimpleModel> getSortedSetSimple() {
return sortedSetSimple;
}

public void setSortedSetSimple(final SortedSet<SimpleModel> sortedSetSimple) {
this.sortedSetSimple = sortedSetSimple;
}

public Set<Set<SimpleModel>> getSetSetSimple() {
return setSetSimple;
}
Expand Down Expand Up @@ -193,6 +206,9 @@ public boolean equals(final Object o) {
if (getSetSetSimple() != null ? !getSetSetSimple().equals(that.getSetSetSimple()) : that.getSetSetSimple() != null) {
return false;
}
if (getSortedSetSimple() != null ? !getSortedSetSimple().equals(that.getSortedSetSimple()) : that.getSortedSetSimple() != null) {
return false;
}
if (getMapSimple() != null ? !getMapSimple().equals(that.getMapSimple()) : that.getMapSimple() != null) {
return false;
}
Expand Down Expand Up @@ -230,6 +246,7 @@ public int hashCode() {
result = 31 * result + (getListListSimple() != null ? getListListSimple().hashCode() : 0);
result = 31 * result + (getSetSimple() != null ? getSetSimple().hashCode() : 0);
result = 31 * result + (getSetSetSimple() != null ? getSetSetSimple().hashCode() : 0);
result = 31 * result + (getSortedSetSimple() != null ? getSortedSetSimple().hashCode() : 0);
result = 31 * result + (getMapSimple() != null ? getMapSimple().hashCode() : 0);
result = 31 * result + (getMapMapSimple() != null ? getMapMapSimple().hashCode() : 0);
result = 31 * result + (getMapListSimple() != null ? getMapListSimple().hashCode() : 0);
Expand All @@ -248,6 +265,7 @@ public String toString() {
+ ", listListSimple=" + listListSimple
+ ", setSimple=" + setSimple
+ ", setSetSimple=" + setSetSimple
+ ", setSortedSimple=" + sortedSetSimple
+ ", mapSimple=" + mapSimple
+ ", mapMapSimple=" + mapMapSimple
+ ", mapListSimple=" + mapListSimple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.bson.codecs.pojo.entities;

public final class SimpleModel {
public final class SimpleModel implements Comparable<SimpleModel> {
private Integer integerField;
private String stringField;

Expand Down Expand Up @@ -79,4 +79,10 @@ public String toString() {
+ ", stringField='" + stringField + "'"
+ "}";
}

@Override
public int compareTo(final SimpleModel o) {
int integerFieldCompareResult = this.integerField.compareTo(o.integerField);
return integerFieldCompareResult == 0 ? this.stringField.compareTo(o.stringField) : integerFieldCompareResult;
}
}