|
| 1 | +/* |
| 2 | + * Copyright 2017 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 | +package org.bson.codecs.pojo; |
| 17 | + |
| 18 | +import org.bson.BsonReader; |
| 19 | +import org.bson.BsonType; |
| 20 | +import org.bson.BsonWriter; |
| 21 | +import org.bson.codecs.Codec; |
| 22 | +import org.bson.codecs.DecoderContext; |
| 23 | +import org.bson.codecs.EncoderContext; |
| 24 | +import org.bson.codecs.configuration.CodecConfigurationException; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.HashSet; |
| 29 | + |
| 30 | +import static java.lang.String.format; |
| 31 | + |
| 32 | +final class CollectionPropertyCodecProvider implements PropertyCodecProvider { |
| 33 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 34 | + @Override |
| 35 | + public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) { |
| 36 | + if (Collection.class.isAssignableFrom(type.getType()) && type.getTypeParameters().size() == 1) { |
| 37 | + return new CollectionCodec(type.getType(), registry.get(type.getTypeParameters().get(0))); |
| 38 | + } else { |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static class CollectionCodec<T> implements Codec<Collection<T>> { |
| 44 | + private final Class<Collection<T>> encoderClass; |
| 45 | + private final Codec<T> codec; |
| 46 | + |
| 47 | + CollectionCodec(final Class<Collection<T>> encoderClass, final Codec<T> codec) { |
| 48 | + this.encoderClass = encoderClass; |
| 49 | + this.codec = codec; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void encode(final BsonWriter writer, final Collection<T> collection, final EncoderContext encoderContext) { |
| 54 | + writer.writeStartArray(); |
| 55 | + for (final T value : collection) { |
| 56 | + codec.encode(writer, value, encoderContext); |
| 57 | + } |
| 58 | + writer.writeEndArray(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Collection<T> decode(final BsonReader reader, final DecoderContext context) { |
| 63 | + Collection<T> collection = getInstance(); |
| 64 | + reader.readStartArray(); |
| 65 | + while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { |
| 66 | + collection.add(codec.decode(reader, context)); |
| 67 | + } |
| 68 | + reader.readEndArray(); |
| 69 | + return collection; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Class<Collection<T>> getEncoderClass() { |
| 74 | + return encoderClass; |
| 75 | + } |
| 76 | + |
| 77 | + private Collection<T> getInstance() { |
| 78 | + if (encoderClass.isInterface()) { |
| 79 | + if (encoderClass.isAssignableFrom(ArrayList.class)) { |
| 80 | + return new ArrayList<T>(); |
| 81 | + } else if (encoderClass.isAssignableFrom(HashSet.class)) { |
| 82 | + return new HashSet<T>(); |
| 83 | + } else { |
| 84 | + throw new CodecConfigurationException(format("Unsupported Collection interface of %s!", encoderClass.getName())); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + return encoderClass.newInstance(); |
| 90 | + } catch (final Exception e) { |
| 91 | + throw new CodecConfigurationException(e.getMessage(), e); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments