/driver-core/src/main/com/mongodb/client/model/geojson/codecs/GeometryCollectionCodec.java

https://github.com/foursquare/mongo-java-driver · Java · 79 lines · 44 code · 12 blank · 23 comment · 1 complexity · 7b859432a5e6d4b0e81bf4e828ae7017 MD5 · raw file

  1. /*
  2. * Copyright 2015 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. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.mongodb.client.model.geojson.codecs;
  15. import com.mongodb.client.model.geojson.Geometry;
  16. import com.mongodb.client.model.geojson.GeometryCollection;
  17. import org.bson.BsonReader;
  18. import org.bson.BsonWriter;
  19. import org.bson.codecs.Codec;
  20. import org.bson.codecs.DecoderContext;
  21. import org.bson.codecs.EncoderContext;
  22. import org.bson.codecs.configuration.CodecRegistry;
  23. import static com.mongodb.assertions.Assertions.notNull;
  24. import static com.mongodb.client.model.geojson.codecs.GeometryCodecHelper.encodeCoordinateReferenceSystem;
  25. import static com.mongodb.client.model.geojson.codecs.GeometryCodecHelper.encodeType;
  26. /**
  27. * A Codec for a GeoJSON GeometryCollection.
  28. *
  29. * @since 3.1
  30. */
  31. public class GeometryCollectionCodec implements Codec<GeometryCollection> {
  32. private final CodecRegistry registry;
  33. /**
  34. * Constructs an instance.
  35. *
  36. * @param registry the registry
  37. */
  38. public GeometryCollectionCodec(final CodecRegistry registry) {
  39. this.registry = notNull("registry", registry);
  40. }
  41. @Override
  42. public void encode(final BsonWriter writer, final GeometryCollection value, final EncoderContext encoderContext) {
  43. writer.writeStartDocument();
  44. encodeType(writer, value);
  45. writer.writeName("geometries");
  46. writer.writeStartArray();
  47. for (Geometry geometry : value.getGeometries()) {
  48. encodeGeometry(writer, geometry, encoderContext);
  49. }
  50. writer.writeEndArray();
  51. encodeCoordinateReferenceSystem(writer, value, encoderContext, registry);
  52. writer.writeEndDocument();
  53. }
  54. @SuppressWarnings({"unchecked", "rawtypes"})
  55. private void encodeGeometry(final BsonWriter writer, final Geometry geometry, final EncoderContext encoderContext) {
  56. Codec codec = registry.get(geometry.getClass());
  57. encoderContext.encodeWithChildContext(codec, writer, geometry);
  58. }
  59. @Override
  60. public Class<GeometryCollection> getEncoderClass() {
  61. return GeometryCollection.class;
  62. }
  63. @Override
  64. public GeometryCollection decode(final BsonReader reader, final DecoderContext decoderContext) {
  65. throw new UnsupportedOperationException("Not implemented yet!");
  66. }
  67. }