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

http://github.com/mongodb/mongo-java-driver · Java · 245 lines · 148 code · 29 blank · 68 comment · 49 complexity · 460cdae6085071faee663968128d7ee9 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 org.bson.BsonType;
  18. import org.bson.codecs.Codec;
  19. /**
  20. * Represents a property on a class and stores various metadata such as generic parameters
  21. *
  22. * @param <T> the type of the property that the PropertyModel represents.
  23. * @since 3.5
  24. */
  25. public final class PropertyModel<T> {
  26. private final String name;
  27. private final String readName;
  28. private final String writeName;
  29. private final TypeData<T> typeData;
  30. private final Codec<T> codec;
  31. private final PropertySerialization<T> propertySerialization;
  32. private final Boolean useDiscriminator;
  33. private final PropertyAccessor<T> propertyAccessor;
  34. private final String error;
  35. private volatile Codec<T> cachedCodec;
  36. private final BsonType bsonRepresentation;
  37. PropertyModel(final String name, final String readName, final String writeName, final TypeData<T> typeData,
  38. final Codec<T> codec, final PropertySerialization<T> propertySerialization, final Boolean useDiscriminator,
  39. final PropertyAccessor<T> propertyAccessor, final String error, final BsonType bsonRepresentation) {
  40. this.name = name;
  41. this.readName = readName;
  42. this.writeName = writeName;
  43. this.typeData = typeData;
  44. this.codec = codec;
  45. this.cachedCodec = codec;
  46. this.propertySerialization = propertySerialization;
  47. this.useDiscriminator = useDiscriminator;
  48. this.propertyAccessor = propertyAccessor;
  49. this.error = error;
  50. this.bsonRepresentation = bsonRepresentation;
  51. }
  52. /**
  53. * Create a new {@link PropertyModelBuilder}
  54. * @param <T> the type of the property
  55. * @return the builder
  56. */
  57. public static <T> PropertyModelBuilder<T> builder() {
  58. return new PropertyModelBuilder<T>();
  59. }
  60. /**
  61. * @return the property name for the model
  62. */
  63. public String getName() {
  64. return name;
  65. }
  66. /**
  67. * @return the name of the property to use as the key when deserializing from BSON
  68. */
  69. public String getWriteName() {
  70. return writeName;
  71. }
  72. /**
  73. * @return the name of the property to use as the key when serializing into BSON
  74. */
  75. public String getReadName() {
  76. return readName;
  77. }
  78. /**
  79. * Property is writable.
  80. *
  81. * @return true if can be deserialized from BSON
  82. */
  83. public boolean isWritable() {
  84. return writeName != null;
  85. }
  86. /**
  87. * Property is readable.
  88. *
  89. * @return true if can be serialized to BSON
  90. */
  91. public boolean isReadable() {
  92. return readName != null;
  93. }
  94. /**
  95. * @return the type data for the property
  96. */
  97. public TypeData<T> getTypeData() {
  98. return typeData;
  99. }
  100. /**
  101. * @return the custom codec to use if set or null
  102. */
  103. public Codec<T> getCodec() {
  104. return codec;
  105. }
  106. /**
  107. * @return the BsonRepresentation of the field
  108. *
  109. * @since 4.2
  110. */
  111. public BsonType getBsonRepresentation() {
  112. return bsonRepresentation;
  113. }
  114. /**
  115. * Returns true if the value should be serialized.
  116. *
  117. * @param value the value to check
  118. * @return true if the value should be serialized.
  119. */
  120. public boolean shouldSerialize(final T value) {
  121. return propertySerialization.shouldSerialize(value);
  122. }
  123. /**
  124. * @return the property accessor
  125. */
  126. public PropertyAccessor<T> getPropertyAccessor() {
  127. return propertyAccessor;
  128. }
  129. /**
  130. * @return true or false if a discriminator should be used when serializing or null if not set
  131. */
  132. public Boolean useDiscriminator() {
  133. return useDiscriminator;
  134. }
  135. @Override
  136. public String toString() {
  137. return "PropertyModel{"
  138. + "propertyName='" + name + "'"
  139. + ", readName='" + readName + "'"
  140. + ", writeName='" + writeName + "'"
  141. + ", typeData=" + typeData
  142. + "}";
  143. }
  144. @Override
  145. public boolean equals(final Object o) {
  146. if (this == o) {
  147. return true;
  148. }
  149. if (o == null || getClass() != o.getClass()) {
  150. return false;
  151. }
  152. PropertyModel<?> that = (PropertyModel<?>) o;
  153. if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) {
  154. return false;
  155. }
  156. if (getReadName() != null ? !getReadName().equals(that.getReadName()) : that.getReadName() != null) {
  157. return false;
  158. }
  159. if (getWriteName() != null ? !getWriteName().equals(that.getWriteName()) : that.getWriteName() != null) {
  160. return false;
  161. }
  162. if (getTypeData() != null ? !getTypeData().equals(that.getTypeData()) : that.getTypeData() != null) {
  163. return false;
  164. }
  165. if (getCodec() != null ? !getCodec().equals(that.getCodec()) : that.getCodec() != null) {
  166. return false;
  167. }
  168. if (getPropertySerialization() != null ? !getPropertySerialization().equals(that.getPropertySerialization()) : that
  169. .getPropertySerialization() != null) {
  170. return false;
  171. }
  172. if (useDiscriminator != null ? !useDiscriminator.equals(that.useDiscriminator) : that.useDiscriminator != null) {
  173. return false;
  174. }
  175. if (getPropertyAccessor() != null ? !getPropertyAccessor().equals(that.getPropertyAccessor())
  176. : that.getPropertyAccessor() != null) {
  177. return false;
  178. }
  179. if (getError() != null ? !getError().equals(that.getError()) : that.getError() != null) {
  180. return false;
  181. }
  182. if (getCachedCodec() != null ? !getCachedCodec().equals(that.getCachedCodec()) : that.getCachedCodec() != null) {
  183. return false;
  184. }
  185. return true;
  186. }
  187. @Override
  188. public int hashCode() {
  189. int result = getName() != null ? getName().hashCode() : 0;
  190. result = 31 * result + (getReadName() != null ? getReadName().hashCode() : 0);
  191. result = 31 * result + (getWriteName() != null ? getWriteName().hashCode() : 0);
  192. result = 31 * result + (getTypeData() != null ? getTypeData().hashCode() : 0);
  193. result = 31 * result + (getCodec() != null ? getCodec().hashCode() : 0);
  194. result = 31 * result + (getPropertySerialization() != null ? getPropertySerialization().hashCode() : 0);
  195. result = 31 * result + (useDiscriminator != null ? useDiscriminator.hashCode() : 0);
  196. result = 31 * result + (getPropertyAccessor() != null ? getPropertyAccessor().hashCode() : 0);
  197. result = 31 * result + (getError() != null ? getError().hashCode() : 0);
  198. result = 31 * result + (getCachedCodec() != null ? getCachedCodec().hashCode() : 0);
  199. return result;
  200. }
  201. boolean hasError() {
  202. return error != null;
  203. }
  204. String getError() {
  205. return error;
  206. }
  207. PropertySerialization<T> getPropertySerialization() {
  208. return propertySerialization;
  209. }
  210. void cachedCodec(final Codec<T> codec) {
  211. this.cachedCodec = codec;
  212. }
  213. Codec<T> getCachedCodec() {
  214. return cachedCodec;
  215. }
  216. }