/bson/src/main/org/bson/codecs/pojo/ClassModel.java

http://github.com/mongodb/mongo-java-driver · Java · 232 lines · 136 code · 25 blank · 71 comment · 27 complexity · 92c9608873b97548f48f40ee6320cddc 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.pojo;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * This model represents the metadata for a class and all its properties.
  24. *
  25. * @param <T> The type of the class the ClassModel represents
  26. * @since 3.5
  27. */
  28. public final class ClassModel<T> {
  29. private final String name;
  30. private final Class<T> type;
  31. private final boolean hasTypeParameters;
  32. private final InstanceCreatorFactory<T> instanceCreatorFactory;
  33. private final boolean discriminatorEnabled;
  34. private final String discriminatorKey;
  35. private final String discriminator;
  36. private final IdPropertyModelHolder<?> idPropertyModelHolder;
  37. private final List<PropertyModel<?>> propertyModels;
  38. private final Map<String, TypeParameterMap> propertyNameToTypeParameterMap;
  39. ClassModel(final Class<T> clazz, final Map<String, TypeParameterMap> propertyNameToTypeParameterMap,
  40. final InstanceCreatorFactory<T> instanceCreatorFactory, final Boolean discriminatorEnabled, final String discriminatorKey,
  41. final String discriminator, final IdPropertyModelHolder<?> idPropertyModelHolder,
  42. final List<PropertyModel<?>> propertyModels) {
  43. this.name = clazz.getSimpleName();
  44. this.type = clazz;
  45. this.hasTypeParameters = clazz.getTypeParameters().length > 0;
  46. this.propertyNameToTypeParameterMap = Collections.unmodifiableMap(
  47. new HashMap<String, TypeParameterMap>(propertyNameToTypeParameterMap));
  48. this.instanceCreatorFactory = instanceCreatorFactory;
  49. this.discriminatorEnabled = discriminatorEnabled;
  50. this.discriminatorKey = discriminatorKey;
  51. this.discriminator = discriminator;
  52. this.idPropertyModelHolder = idPropertyModelHolder;
  53. this.propertyModels = Collections.unmodifiableList(new ArrayList<PropertyModel<?>>(propertyModels));
  54. }
  55. /**
  56. * Creates a new Class Model builder instance using reflection.
  57. *
  58. * @param type the POJO class to reflect and configure the builder with.
  59. * @param <S> the type of the class
  60. * @return a new Class Model builder instance using reflection on the {@code clazz}.
  61. */
  62. public static <S> ClassModelBuilder<S> builder(final Class<S> type) {
  63. return new ClassModelBuilder<S>(type);
  64. }
  65. /**
  66. * @return a new InstanceCreator instance for the ClassModel
  67. */
  68. InstanceCreator<T> getInstanceCreator() {
  69. return instanceCreatorFactory.create();
  70. }
  71. /**
  72. * @return the backing class for the ClassModel
  73. */
  74. public Class<T> getType() {
  75. return type;
  76. }
  77. /**
  78. * @return true if the underlying type has type parameters.
  79. */
  80. public boolean hasTypeParameters() {
  81. return hasTypeParameters;
  82. }
  83. /**
  84. * @return true if a discriminator should be used when storing the data.
  85. */
  86. public boolean useDiscriminator() {
  87. return discriminatorEnabled;
  88. }
  89. /**
  90. * Gets the value for the discriminator.
  91. *
  92. * @return the discriminator value or null if not set
  93. */
  94. public String getDiscriminatorKey() {
  95. return discriminatorKey;
  96. }
  97. /**
  98. * Returns the discriminator key.
  99. *
  100. * @return the discriminator key or null if not set
  101. */
  102. public String getDiscriminator() {
  103. return discriminator;
  104. }
  105. /**
  106. * Gets a {@link PropertyModel} by the property name.
  107. *
  108. * @param propertyName the PropertyModel's property name
  109. * @return the PropertyModel or null if the property is not found
  110. */
  111. public PropertyModel<?> getPropertyModel(final String propertyName) {
  112. for (PropertyModel<?> propertyModel : propertyModels) {
  113. if (propertyModel.getName().equals(propertyName)) {
  114. return propertyModel;
  115. }
  116. }
  117. return null;
  118. }
  119. /**
  120. * Returns all the properties on this model
  121. *
  122. * @return the list of properties
  123. */
  124. public List<PropertyModel<?>> getPropertyModels() {
  125. return propertyModels;
  126. }
  127. /**
  128. * Returns the {@link PropertyModel} mapped as the id property for this ClassModel
  129. *
  130. * @return the PropertyModel for the id
  131. */
  132. public PropertyModel<?> getIdPropertyModel() {
  133. return idPropertyModelHolder != null ? idPropertyModelHolder.getPropertyModel() : null;
  134. }
  135. IdPropertyModelHolder<?> getIdPropertyModelHolder() {
  136. return idPropertyModelHolder;
  137. }
  138. /**
  139. * Returns the name of the class represented by this ClassModel
  140. *
  141. * @return the name
  142. */
  143. public String getName() {
  144. return name;
  145. }
  146. @Override
  147. public String toString() {
  148. return "ClassModel{"
  149. + "type=" + type
  150. + "}";
  151. }
  152. @Override
  153. public boolean equals(final Object o) {
  154. if (this == o) {
  155. return true;
  156. }
  157. if (o == null || getClass() != o.getClass()) {
  158. return false;
  159. }
  160. ClassModel<?> that = (ClassModel<?>) o;
  161. if (discriminatorEnabled != that.discriminatorEnabled) {
  162. return false;
  163. }
  164. if (!getType().equals(that.getType())) {
  165. return false;
  166. }
  167. if (!getInstanceCreatorFactory().equals(that.getInstanceCreatorFactory())) {
  168. return false;
  169. }
  170. if (getDiscriminatorKey() != null ? !getDiscriminatorKey().equals(that.getDiscriminatorKey())
  171. : that.getDiscriminatorKey() != null) {
  172. return false;
  173. }
  174. if (getDiscriminator() != null ? !getDiscriminator().equals(that.getDiscriminator()) : that.getDiscriminator() != null) {
  175. return false;
  176. }
  177. if (idPropertyModelHolder != null ? !idPropertyModelHolder.equals(that.idPropertyModelHolder)
  178. : that.idPropertyModelHolder != null) {
  179. return false;
  180. }
  181. if (!getPropertyModels().equals(that.getPropertyModels())) {
  182. return false;
  183. }
  184. if (!getPropertyNameToTypeParameterMap().equals(that.getPropertyNameToTypeParameterMap())) {
  185. return false;
  186. }
  187. return true;
  188. }
  189. @Override
  190. public int hashCode() {
  191. int result = getType().hashCode();
  192. result = 31 * result + getInstanceCreatorFactory().hashCode();
  193. result = 31 * result + (discriminatorEnabled ? 1 : 0);
  194. result = 31 * result + (getDiscriminatorKey() != null ? getDiscriminatorKey().hashCode() : 0);
  195. result = 31 * result + (getDiscriminator() != null ? getDiscriminator().hashCode() : 0);
  196. result = 31 * result + (getIdPropertyModelHolder() != null ? getIdPropertyModelHolder().hashCode() : 0);
  197. result = 31 * result + getPropertyModels().hashCode();
  198. result = 31 * result + getPropertyNameToTypeParameterMap().hashCode();
  199. return result;
  200. }
  201. InstanceCreatorFactory<T> getInstanceCreatorFactory() {
  202. return instanceCreatorFactory;
  203. }
  204. Map<String, TypeParameterMap> getPropertyNameToTypeParameterMap() {
  205. return propertyNameToTypeParameterMap;
  206. }
  207. }