Skip to content

Commit a999e15

Browse files
authored
Support decoding to Collection and Map subclasses (#1019)
JAVA-4742
1 parent d19de19 commit a999e15

23 files changed

+1162
-226
lines changed

bson/src/main/org/bson/Document.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import org.bson.codecs.BsonValueCodecProvider;
2020
import org.bson.codecs.Codec;
21+
import org.bson.codecs.CollectionCodecProvider;
2122
import org.bson.codecs.Decoder;
2223
import org.bson.codecs.DecoderContext;
2324
import org.bson.codecs.DocumentCodec;
2425
import org.bson.codecs.DocumentCodecProvider;
2526
import org.bson.codecs.Encoder;
2627
import org.bson.codecs.EncoderContext;
27-
import org.bson.codecs.IterableCodecProvider;
2828
import org.bson.codecs.MapCodecProvider;
2929
import org.bson.codecs.ValueCodecProvider;
3030
import org.bson.codecs.configuration.CodecRegistry;
@@ -60,8 +60,10 @@
6060
* @since 3.0.0
6161
*/
6262
public class Document implements Map<String, Object>, Serializable, Bson {
63+
@SuppressWarnings("deprecation")
6364
private static final Codec<Document> DEFAULT_CODEC =
64-
withUuidRepresentation(fromProviders(asList(new ValueCodecProvider(), new IterableCodecProvider(),
65+
withUuidRepresentation(fromProviders(asList(new ValueCodecProvider(),
66+
new CollectionCodecProvider(), new org.bson.codecs.IterableCodecProvider(),
6567
new BsonValueCodecProvider(), new DocumentCodecProvider(), new MapCodecProvider())), UuidRepresentation.STANDARD)
6668
.get(Document.class);
6769

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs;
18+
19+
import org.bson.BsonReader;
20+
import org.bson.BsonType;
21+
import org.bson.BsonWriter;
22+
import org.bson.codecs.configuration.CodecConfigurationException;
23+
24+
import java.lang.reflect.Constructor;
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.util.AbstractCollection;
27+
import java.util.AbstractList;
28+
import java.util.AbstractSet;
29+
import java.util.ArrayList;
30+
import java.util.Collection;
31+
import java.util.HashSet;
32+
import java.util.List;
33+
import java.util.NavigableSet;
34+
import java.util.Set;
35+
import java.util.SortedSet;
36+
import java.util.TreeSet;
37+
import java.util.function.Supplier;
38+
39+
import static java.lang.String.format;
40+
import static org.bson.assertions.Assertions.notNull;
41+
42+
abstract class AbstractCollectionCodec<T, C extends Collection<T>> implements Codec<C> {
43+
44+
private final Class<C> clazz;
45+
private final Supplier<C> supplier;
46+
47+
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable", "rawtypes"})
48+
AbstractCollectionCodec(final Class<C> clazz) {
49+
this.clazz = notNull("clazz", clazz);
50+
Class rawClass = clazz;
51+
if (rawClass == Collection.class || rawClass == List.class || rawClass == AbstractCollection.class || rawClass == AbstractList.class
52+
|| rawClass == ArrayList.class) {
53+
supplier = () -> (C) new ArrayList<T>();
54+
} else if (rawClass == Set.class || rawClass == AbstractSet.class || rawClass == HashSet.class) {
55+
supplier = () -> (C) new HashSet<T>();
56+
} else if (rawClass == NavigableSet.class || rawClass == SortedSet.class || rawClass == TreeSet.class) {
57+
//noinspection SortedCollectionWithNonComparableKeys
58+
supplier = () -> (C) new TreeSet<T>();
59+
} else {
60+
Constructor<? extends Collection<?>> constructor;
61+
Supplier<C> supplier;
62+
try {
63+
constructor = clazz.getDeclaredConstructor();
64+
supplier = () -> {
65+
try {
66+
return (C) constructor.newInstance();
67+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
68+
throw new CodecConfigurationException(format("Can not invoke no-args constructor for Collection class %s", clazz),
69+
e);
70+
}
71+
};
72+
} catch (NoSuchMethodException e) {
73+
supplier = () -> {
74+
throw new CodecConfigurationException(format("No no-args constructor for Collection class %s", clazz), e);
75+
};
76+
}
77+
this.supplier = supplier;
78+
}
79+
}
80+
81+
abstract T readValue(BsonReader reader, DecoderContext decoderContext);
82+
83+
abstract void writeValue(BsonWriter writer, T cur, EncoderContext encoderContext);
84+
85+
@Override
86+
public C decode(final BsonReader reader, final DecoderContext decoderContext) {
87+
reader.readStartArray();
88+
89+
C collection = supplier.get();
90+
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
91+
if (reader.getCurrentBsonType() == BsonType.NULL) {
92+
reader.readNull();
93+
collection.add(null);
94+
} else {
95+
collection.add(readValue(reader, decoderContext));
96+
}
97+
}
98+
99+
reader.readEndArray();
100+
101+
return collection;
102+
}
103+
104+
@Override
105+
public void encode(final BsonWriter writer, final C value, final EncoderContext encoderContext) {
106+
writer.writeStartArray();
107+
for (final T cur : value) {
108+
if (cur == null) {
109+
writer.writeNull();
110+
} else {
111+
writeValue(writer, cur, encoderContext);
112+
}
113+
}
114+
writer.writeEndArray();
115+
}
116+
117+
@Override
118+
public Class<C> getEncoderClass() {
119+
return clazz;
120+
}
121+
}

bson/src/main/org/bson/codecs/AbstractIterableCodec.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

bson/src/main/org/bson/codecs/AbstractMapCodec.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,61 @@
1919
import org.bson.BsonReader;
2020
import org.bson.BsonType;
2121
import org.bson.BsonWriter;
22+
import org.bson.codecs.configuration.CodecConfigurationException;
2223

24+
import javax.annotation.Nullable;
25+
import java.lang.reflect.Constructor;
26+
import java.lang.reflect.InvocationTargetException;
27+
import java.util.AbstractMap;
2328
import java.util.HashMap;
2429
import java.util.Map;
30+
import java.util.NavigableMap;
31+
import java.util.TreeMap;
32+
import java.util.function.Supplier;
2533

26-
abstract class AbstractMapCodec<T> implements Codec<Map<String, T>> {
34+
import static java.lang.String.format;
35+
import static org.bson.assertions.Assertions.notNull;
36+
37+
abstract class AbstractMapCodec<T, M extends Map<String, T>> implements Codec<M> {
38+
39+
private final Supplier<M> supplier;
40+
private final Class<M> clazz;
41+
42+
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable", "rawtypes"})
43+
AbstractMapCodec(@Nullable final Class<M> clazz) {
44+
this.clazz = notNull("clazz", clazz);
45+
Class rawClass = clazz;
46+
if (rawClass == Map.class || rawClass == AbstractMap.class || rawClass == HashMap.class) {
47+
supplier = () -> (M) new HashMap<String, T>();
48+
} else if (rawClass == NavigableMap.class || rawClass == TreeMap.class) {
49+
supplier = () -> (M) new TreeMap<String, T>();
50+
} else {
51+
Constructor<? extends Map<?, ?>> constructor;
52+
Supplier<M> supplier;
53+
try {
54+
constructor = clazz.getDeclaredConstructor();
55+
supplier = () -> {
56+
try {
57+
return (M) constructor.newInstance();
58+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
59+
throw new CodecConfigurationException("Can not invoke no-args constructor for Map class %s", e);
60+
}
61+
};
62+
} catch (NoSuchMethodException e) {
63+
supplier = () -> {
64+
throw new CodecConfigurationException(format("Map class %s has no public no-args constructor", clazz), e);
65+
};
66+
}
67+
this.supplier = supplier;
68+
}
69+
}
2770

2871
abstract T readValue(BsonReader reader, DecoderContext decoderContext);
2972

3073
abstract void writeValue(BsonWriter writer, T value, EncoderContext encoderContext);
3174

3275
@Override
33-
public void encode(final BsonWriter writer, final Map<String, T> map, final EncoderContext encoderContext) {
76+
public void encode(final BsonWriter writer, final M map, final EncoderContext encoderContext) {
3477
writer.writeStartDocument();
3578
for (final Map.Entry<String, T> entry : map.entrySet()) {
3679
writer.writeName(entry.getKey());
@@ -46,8 +89,8 @@ public void encode(final BsonWriter writer, final Map<String, T> map, final Enco
4689

4790

4891
@Override
49-
public Map<String, T> decode(final BsonReader reader, final DecoderContext decoderContext) {
50-
Map<String, T> map = new HashMap<>();
92+
public M decode(final BsonReader reader, final DecoderContext decoderContext) {
93+
M map = supplier.get();
5194

5295
reader.readStartDocument();
5396
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
@@ -64,9 +107,8 @@ public Map<String, T> decode(final BsonReader reader, final DecoderContext decod
64107
return map;
65108
}
66109

67-
@SuppressWarnings({"unchecked", "rawtypes"})
68110
@Override
69-
public Class<Map<String, T>> getEncoderClass() {
70-
return (Class<Map<String, T>>) ((Class) Map.class);
111+
public Class<M> getEncoderClass() {
112+
return clazz;
71113
}
72114
}

0 commit comments

Comments
 (0)