Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. package hu.proofit.ace.datasource.common.metamodel.datatype;
  2.  
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import lombok.extern.slf4j.Slf4j;
  6.  
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.time.Instant;
  10. import java.time.ZonedDateTime;
  11. import java.time.format.DateTimeFormatter;
  12. import java.time.format.DateTimeParseException;
  13. import java.util.*;
  14.  
  15. @Getter
  16. @Setter
  17. @Slf4j
  18. public class PrimitiveDataType extends DataType {
  19.  
  20. public static final String FIELD_NAME = "value";
  21.  
  22. public static final PrimitiveDataType integerType = new PrimitiveDataType(UUID.fromString("aef44ad0-fb45-4325-b3db-b5bd49ecebbf"), "IntegerType", Integer.class);
  23. public static final PrimitiveDataType longType = new PrimitiveDataType(UUID.fromString("59ffb190-75e8-4c86-a5bb-db2188822914"), "LongType", Long.class);
  24.  
  25. public static final PrimitiveDataType floatType = new PrimitiveDataType(UUID.fromString("b51174a1-fb9e-4dcf-8397-6472e408dfe4"), "FloatType", Float.class);
  26. public static final PrimitiveDataType doubleType = new PrimitiveDataType(UUID.fromString("392f541a-75ad-4550-b6ed-444a608e45d2"), "DoubleType", Double.class);
  27.  
  28. public static final PrimitiveDataType stringType = new PrimitiveDataType(UUID.fromString("0ebceabe-72ad-4846-ace4-66ccb51d4f2c"), "StringType", String.class);
  29.  
  30. public static final PrimitiveDataType dateType = new PrimitiveDataType(UUID.fromString("13866202-d7ca-4685-b648-d557a46e60ba"), "DateType", Date.class);
  31. public static final PrimitiveDataType booleanType = new PrimitiveDataType(UUID.fromString("a2f2b333-5c78-4d7d-a907-1ce8b71e031d"), "BooleanType", Boolean.class);
  32.  
  33. private static final SimpleDateFormat dateToStringFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  34.  
  35. private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
  36.  
  37. private Class mappedType;
  38.  
  39. private Set allowedValues = Collections.emptySet();
  40.  
  41. public PrimitiveDataType() {
  42. }
  43.  
  44. public PrimitiveDataType(String name, Class mappedType) {
  45. this(UUID.randomUUID(), name, mappedType);
  46. }
  47.  
  48. public PrimitiveDataType(UUID id, String name, Class mappedType) {
  49. super(id, name);
  50. this.mappedType = mappedType;
  51. }
  52.  
  53. public PrimitiveDataType(UUID id, String name, Class mappedType, Set allowedValues) {
  54. this(id, name, mappedType);
  55. this.allowedValues = allowedValues;
  56. }
  57.  
  58. public static Object getNativeValue(PrimitiveDataType primitiveDataType, String value) {
  59. if (value == null || value.isEmpty()) {
  60. return null;
  61. }
  62.  
  63. Class fieldClass = primitiveDataType.getMappedType();
  64.  
  65. if(fieldClass.isAssignableFrom(Integer.class)) {
  66. return Integer.parseInt(value);
  67. } else if(fieldClass.isAssignableFrom(Long.class)) {
  68. return Long.parseLong(value);
  69. } else if(fieldClass.isAssignableFrom(Float.class)) {
  70. return Float.parseFloat(value);
  71. } else if(fieldClass.isAssignableFrom(Double.class)) {
  72. return Double.parseDouble(value);
  73. } else if(fieldClass.isAssignableFrom(String.class)) {
  74. return value;
  75. } if (fieldClass.isAssignableFrom(Boolean.class)) {
  76. return Boolean.valueOf(value);
  77. } else if(fieldClass.isAssignableFrom(Date.class)) {
  78. try {
  79. return Date.from(Instant.parse(value));
  80. } catch (DateTimeParseException e) {
  81. log.debug("Failed to parse native value as instant, assuming older version.", e);
  82. }
  83.  
  84. try {
  85. return dateToStringFormat.parse(value);
  86. } catch (ParseException e) {
  87. log.debug("Failed to parse as simple date time format", e);
  88. }
  89.  
  90. try {
  91. return Date.from(ZonedDateTime.parse(value, dtf).toInstant());
  92. } catch (DateTimeParseException e) {
  93. throw new IllegalArgumentException("Failed to parse as simple date time format", e);
  94. }
  95.  
  96.  
  97. } else {
  98. throw new IllegalArgumentException();
  99. }
  100. }
  101.  
  102. public static PrimitiveDataType forClass(Class<?> clazz) {
  103. if(clazz.isAssignableFrom(Integer.class)) {
  104. return PrimitiveDataType.integerType;
  105. } else if(clazz.isAssignableFrom(Long.class)) {
  106. return PrimitiveDataType.longType;
  107. } else if(clazz.isAssignableFrom(Float.class)) {
  108. return PrimitiveDataType.floatType;
  109. } else if(clazz.isAssignableFrom(Double.class)) {
  110. return PrimitiveDataType.doubleType;
  111. } else if(clazz.isAssignableFrom(String.class)) {
  112. return PrimitiveDataType.stringType;
  113. } if (clazz.isAssignableFrom(Boolean.class)) {
  114. return PrimitiveDataType.booleanType;
  115. } else if(clazz.isAssignableFrom(Date.class)) {
  116. return PrimitiveDataType.dateType;
  117. } else {
  118. return null;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement