/hazelcast-integration/spring-data-integration/src/main/java/com/hazelcast/spring/mongodb/SpringMongoDBConverter.java

https://gitlab.com/metamorphiccode/hazelcast-code-samples · Java · 85 lines · 61 code · 9 blank · 15 comment · 23 complexity · eb472ac9c8d963020078b031ba78ca3e MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  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 com.hazelcast.spring.mongodb;
  17. import com.mongodb.BasicDBObject;
  18. import com.mongodb.DBObject;
  19. import com.mongodb.Mongo;
  20. import org.bson.BSONObject;
  21. import org.bson.types.ObjectId;
  22. import org.springframework.data.mongodb.core.MongoTemplate;
  23. import java.util.Date;
  24. import java.util.UUID;
  25. import java.util.regex.Pattern;
  26. @SuppressWarnings("unused")
  27. class SpringMongoDBConverter implements MongoDBConverter {
  28. private MongoTemplate mongoTemplate;
  29. public SpringMongoDBConverter(Mongo mongo, String dbname) {
  30. this.mongoTemplate = new MongoTemplate(mongo, dbname);
  31. }
  32. public SpringMongoDBConverter(MongoTemplate mongoTemplate) {
  33. this.mongoTemplate = mongoTemplate;
  34. }
  35. public DBObject toDBObject(Object obj) {
  36. DBObject dbObject = new BasicDBObject();
  37. if (isStandardClass(obj.getClass())) {
  38. obj = new ValueWrapper(obj);
  39. }
  40. mongoTemplate.getConverter().write(obj, dbObject);
  41. return dbObject;
  42. }
  43. public Object toObject(Class clazz, DBObject dbObject) {
  44. if (clazz.equals(ValueWrapper.class)) {
  45. return dbObject.get("value");
  46. }
  47. return mongoTemplate.getConverter().read(clazz, dbObject);
  48. }
  49. @SuppressWarnings("checkstyle:returncount")
  50. private static boolean isStandardClass(Class clazz) {
  51. if (clazz.isAssignableFrom(Date.class)) {
  52. return true;
  53. } else if (clazz.isAssignableFrom(Number.class)) {
  54. return true;
  55. } else if (clazz.isAssignableFrom(String.class)) {
  56. return true;
  57. } else if (clazz.isAssignableFrom(ObjectId.class)) {
  58. return true;
  59. } else if (clazz.isAssignableFrom(BSONObject.class)) {
  60. return true;
  61. } else if (clazz.isAssignableFrom(Boolean.class)) {
  62. return true;
  63. } else if (clazz.isAssignableFrom(Double.class)) {
  64. return true;
  65. } else if (clazz.isAssignableFrom(Integer.class)) {
  66. return true;
  67. } else if (clazz.isAssignableFrom(Long.class)) {
  68. return true;
  69. } else if (clazz.isAssignableFrom(Pattern.class)) {
  70. return true;
  71. } else if (clazz.isAssignableFrom(UUID.class)) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. }