/bson/src/main/org/bson/codecs/configuration/CodecRegistries.java

https://github.com/jyemin/mongo-java-driver · Java · 156 lines · 43 code · 13 blank · 100 comment · 4 complexity · 49c51dad6302f75212cf9235fdd0fe19 MD5 · raw file

  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. package org.bson.codecs.configuration;
  17. import org.bson.UuidRepresentation;
  18. import org.bson.codecs.Codec;
  19. import org.bson.internal.OverridableUuidRepresentationCodecRegistry;
  20. import org.bson.internal.ProvidersCodecRegistry;
  21. import java.util.List;
  22. import static java.util.Arrays.asList;
  23. /**
  24. * A helper class for creating and combining codecs, codec providers, and codec registries
  25. *
  26. * @since 3.0
  27. */
  28. public final class CodecRegistries {
  29. /**
  30. * Apply given {@link UuidRepresentation} to the given {@link CodecRegistry}.
  31. *
  32. * @param codecRegistry the code registry
  33. * @param uuidRepresentation the uuid representation
  34. * @return a {@code CodecRegistry} with the given {@code UuidRepresentation} applied to the given {@code CodecRegistry}
  35. * @since 4.5
  36. */
  37. public static CodecRegistry withUuidRepresentation(final CodecRegistry codecRegistry, final UuidRepresentation uuidRepresentation) {
  38. if (codecRegistry instanceof OverridableUuidRepresentationCodecRegistry) {
  39. OverridableUuidRepresentationCodecRegistry overridableUuidRepresentationCodecRegistry =
  40. (OverridableUuidRepresentationCodecRegistry) codecRegistry;
  41. if (overridableUuidRepresentationCodecRegistry.getUuidRepresentation().equals(uuidRepresentation)) {
  42. return codecRegistry;
  43. } else {
  44. return new OverridableUuidRepresentationCodecRegistry(overridableUuidRepresentationCodecRegistry.getWrapped(),
  45. uuidRepresentation);
  46. }
  47. } else {
  48. return new OverridableUuidRepresentationCodecRegistry(codecRegistry, uuidRepresentation);
  49. }
  50. }
  51. /**
  52. * Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances.
  53. *
  54. * <p>This registry can then be used alongside other registries. Typically used when adding extra codecs to existing codecs with the
  55. * {@link #fromRegistries(CodecRegistry...)} )} helper.</p>
  56. *
  57. * @param codecs the {@code Codec} to create a registry for
  58. * @return a {@code CodecRegistry} for the given list of {@code Codec} instances.
  59. */
  60. public static CodecRegistry fromCodecs(final Codec<?>... codecs) {
  61. return fromCodecs(asList(codecs));
  62. }
  63. /**
  64. * Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances.
  65. *
  66. * <p>This registry can then be used alongside other registries. Typically used when adding extra codecs to existing codecs with the
  67. * {@link #fromRegistries(CodecRegistry...)} )} helper.</p>
  68. *
  69. * @param codecs the {@code Codec} to create a registry for
  70. * @return a {@code CodecRegistry} for the given list of {@code Codec} instances.
  71. */
  72. public static CodecRegistry fromCodecs(final List<? extends Codec<?>> codecs) {
  73. return fromProviders(new MapOfCodecsProvider(codecs));
  74. }
  75. /**
  76. * Creates a {@code CodecRegistry} from the provided list of {@code CodecProvider} instances.
  77. *
  78. * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A
  79. * requires the construction of a {@code Codec} for class B, and vice versa.</p>
  80. *
  81. * @param providers the codec provider
  82. * @return a {@code CodecRegistry} with the ordered list of {@code CodecProvider} instances. The registry is also guaranteed to be an
  83. * instance of {code CodecProvider}, so that when one is passed to {@link #fromRegistries(CodecRegistry...)} or {@link
  84. * #fromRegistries(java.util.List)} it will be treated as a {@code CodecProvider} and properly resolve any dependencies between
  85. * registries.
  86. */
  87. public static CodecRegistry fromProviders(final CodecProvider... providers) {
  88. return fromProviders(asList(providers));
  89. }
  90. /**
  91. * Creates a {@code CodecRegistry} from the provided list of {@code CodecProvider} instances.
  92. *
  93. * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A
  94. * requires the construction of a {@code Codec} for class B, and vice versa.</p>
  95. *
  96. * @param providers the codec provider
  97. * @return a {@code CodecRegistry} with the ordered list of {@code CodecProvider} instances. The registry is also guaranteed to be an
  98. * instance of {code CodecProvider}, so that when one is passed to {@link #fromRegistries(CodecRegistry...)} or {@link
  99. * #fromRegistries(java.util.List)} it will be treated as a {@code CodecProvider} and properly resolve any dependencies between
  100. * registries.
  101. */
  102. public static CodecRegistry fromProviders(final List<? extends CodecProvider> providers) {
  103. return new ProvidersCodecRegistry(providers);
  104. }
  105. /**
  106. * A {@code CodecRegistry} that combines the given {@code CodecRegistry} instances into a single registry.
  107. *
  108. * <p>The registries are checked in order until one returns a {@code Codec} for the requested {@code Class}.</p>
  109. *
  110. * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A
  111. * requires the construction of a {@code Codec} for class B, and vice versa.</p>
  112. * <p>Any of the given registries that also implement {@code CodecProvider} will be treated as a {@code CodecProvider} instead of a
  113. * {@code CodecRegistry}, which will ensure proper resolution of any dependencies between registries.</p>
  114. *
  115. * @param registries the preferred registry for {@code Codec} lookups
  116. *
  117. * @return a {@code CodecRegistry} that combines the list of {@code CodecRegistry} instances into a single one
  118. */
  119. public static CodecRegistry fromRegistries(final CodecRegistry... registries) {
  120. return fromRegistries(asList(registries));
  121. }
  122. /**
  123. * A {@code CodecRegistry} that combines the given {@code CodecRegistry} instances into a single registry.
  124. *
  125. * <p>The registries are checked in order until one returns a {@code Codec} for the requested {@code Class}.</p>
  126. *
  127. * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A
  128. * requires the construction of a {@code Codec} for class B, and vice versa.</p>
  129. * <p>Any of the given registries that also implement {@code CodecProvider} will be treated as a {@code CodecProvider} instead of a
  130. * {@code CodecRegistry}, which will ensure proper resolution of any dependencies between registries.</p>
  131. *
  132. * @param registries the preferred registry for {@code Codec} lookups
  133. *
  134. * @return a {@code CodecRegistry} that combines the list of {@code CodecRegistry} instances into a single one
  135. */
  136. public static CodecRegistry fromRegistries(final List<? extends CodecRegistry> registries) {
  137. return new ProvidersCodecRegistry(registries);
  138. }
  139. private CodecRegistries() {
  140. }
  141. }