|
| 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 | +} |
0 commit comments