PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/PersisterService/src/main/java/org/pasut/persister/operators/Operator.java

https://github.com/LaMaldicionDeMandos/PersisterService
Java | 66 lines | 55 code | 11 blank | 0 comment | 4 complexity | b3604a895eb7d6714a7a3d9f6a0e4080 MD5 | raw file
  1. package org.pasut.persister.operators;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import org.codehaus.jackson.map.ObjectMapper;
  7. import org.pasut.persister.GsonMongoMapper;
  8. import com.mongodb.BasicDBObject;
  9. import com.mongodb.BasicDBObjectBuilder;
  10. public abstract class Operator {
  11. public abstract void perform(BasicDBObjectBuilder builder);
  12. private static final GsonMongoMapper wrapper = new GsonMongoMapper();
  13. private final ObjectMapper mapper = new ObjectMapper();
  14. private final static Set<Class<?>> WRAPPER_TYPES = getWrapperTypes();
  15. protected static boolean isWrapperType(Class<?> clazz){
  16. return WRAPPER_TYPES.contains(clazz);
  17. }
  18. @SuppressWarnings("unchecked")
  19. protected String getId(Object value){
  20. Map<String, Object> map = mapper.convertValue(value,HashMap.class);
  21. return (String)map.remove("id");
  22. }
  23. protected BasicDBObject build(String key, Object value){
  24. BasicDBObject res = new BasicDBObject();
  25. res.append(key + "._id", getId(value));
  26. return res;
  27. }
  28. protected void build(String key, Object value, BasicDBObjectBuilder builder) {
  29. builder.append(key + "._id", getId(value));
  30. }
  31. protected Object wrap(Object value){
  32. if(isWrapperType(value.getClass())) return value;
  33. if(value.getClass().isEnum()) return value.toString();
  34. return wrapper.toDbObject(value);
  35. }
  36. protected boolean isComplex(Object value){
  37. if(isWrapperType(value.getClass())) return false;
  38. if(value.getClass().isEnum()) return false;
  39. return true;
  40. }
  41. private static HashSet<Class<?>> getWrapperTypes(){
  42. HashSet<Class<?>> ret = new HashSet<Class<?>>();
  43. ret.add(Boolean.class);
  44. ret.add(Character.class);
  45. ret.add(Byte.class);
  46. ret.add(Short.class);
  47. ret.add(Integer.class);
  48. ret.add(Long.class);
  49. ret.add(Float.class);
  50. ret.add(Double.class);
  51. ret.add(Void.class);
  52. ret.add(String.class);
  53. return ret;
  54. }
  55. }