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

https://github.com/foursquare/mongo-java-driver · Java · 55 lines · 19 code · 7 blank · 29 comment · 2 complexity · 289d5b80cb954ab521d24eb3a2c97bca MD5 · raw file

  1. /*
  2. * Copyright 2014-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. * 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;
  17. import org.bson.UuidRepresentation;
  18. import org.bson.codecs.configuration.CodecProvider;
  19. import org.bson.codecs.configuration.CodecRegistry;
  20. import java.util.UUID;
  21. /**
  22. * A {@code CodecProvider} for UUID Codecs with custom UUID representations
  23. *
  24. * @since 3.0
  25. */
  26. public class UuidCodecProvider implements CodecProvider {
  27. private UuidRepresentation uuidRepresentation;
  28. /**
  29. * Set the UUIDRepresentation to be used in the codec
  30. * default is JAVA_LEGACY to be compatible with existing documents
  31. *
  32. * @param uuidRepresentation the representation of UUID
  33. *
  34. * @since 3.0
  35. * @see org.bson.UuidRepresentation
  36. */
  37. public UuidCodecProvider(final UuidRepresentation uuidRepresentation) {
  38. this.uuidRepresentation = uuidRepresentation;
  39. }
  40. @Override
  41. @SuppressWarnings("unchecked")
  42. public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
  43. if (clazz == UUID.class) {
  44. return (Codec<T>) (new UuidCodec(uuidRepresentation));
  45. }
  46. return null;
  47. }
  48. }