Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hu.proofit.ace.datasource.common.metamodel.datatype;
- import lombok.Getter;
- import lombok.Setter;
- import lombok.extern.slf4j.Slf4j;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.Instant;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- import java.time.format.DateTimeParseException;
- import java.util.*;
- @Getter
- @Setter
- @Slf4j
- public class PrimitiveDataType extends DataType {
- public static final String FIELD_NAME = "value";
- public static final PrimitiveDataType integerType = new PrimitiveDataType(UUID.fromString("aef44ad0-fb45-4325-b3db-b5bd49ecebbf"), "IntegerType", Integer.class);
- public static final PrimitiveDataType longType = new PrimitiveDataType(UUID.fromString("59ffb190-75e8-4c86-a5bb-db2188822914"), "LongType", Long.class);
- public static final PrimitiveDataType floatType = new PrimitiveDataType(UUID.fromString("b51174a1-fb9e-4dcf-8397-6472e408dfe4"), "FloatType", Float.class);
- public static final PrimitiveDataType doubleType = new PrimitiveDataType(UUID.fromString("392f541a-75ad-4550-b6ed-444a608e45d2"), "DoubleType", Double.class);
- public static final PrimitiveDataType stringType = new PrimitiveDataType(UUID.fromString("0ebceabe-72ad-4846-ace4-66ccb51d4f2c"), "StringType", String.class);
- public static final PrimitiveDataType dateType = new PrimitiveDataType(UUID.fromString("13866202-d7ca-4685-b648-d557a46e60ba"), "DateType", Date.class);
- public static final PrimitiveDataType booleanType = new PrimitiveDataType(UUID.fromString("a2f2b333-5c78-4d7d-a907-1ce8b71e031d"), "BooleanType", Boolean.class);
- private static final SimpleDateFormat dateToStringFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
- private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
- private Class mappedType;
- private Set allowedValues = Collections.emptySet();
- public PrimitiveDataType() {
- }
- public PrimitiveDataType(String name, Class mappedType) {
- this(UUID.randomUUID(), name, mappedType);
- }
- public PrimitiveDataType(UUID id, String name, Class mappedType) {
- super(id, name);
- this.mappedType = mappedType;
- }
- public PrimitiveDataType(UUID id, String name, Class mappedType, Set allowedValues) {
- this(id, name, mappedType);
- this.allowedValues = allowedValues;
- }
- public static Object getNativeValue(PrimitiveDataType primitiveDataType, String value) {
- if (value == null || value.isEmpty()) {
- return null;
- }
- Class fieldClass = primitiveDataType.getMappedType();
- if(fieldClass.isAssignableFrom(Integer.class)) {
- return Integer.parseInt(value);
- } else if(fieldClass.isAssignableFrom(Long.class)) {
- return Long.parseLong(value);
- } else if(fieldClass.isAssignableFrom(Float.class)) {
- return Float.parseFloat(value);
- } else if(fieldClass.isAssignableFrom(Double.class)) {
- return Double.parseDouble(value);
- } else if(fieldClass.isAssignableFrom(String.class)) {
- return value;
- } if (fieldClass.isAssignableFrom(Boolean.class)) {
- return Boolean.valueOf(value);
- } else if(fieldClass.isAssignableFrom(Date.class)) {
- try {
- return Date.from(Instant.parse(value));
- } catch (DateTimeParseException e) {
- log.debug("Failed to parse native value as instant, assuming older version.", e);
- }
- try {
- return dateToStringFormat.parse(value);
- } catch (ParseException e) {
- log.debug("Failed to parse as simple date time format", e);
- }
- try {
- return Date.from(ZonedDateTime.parse(value, dtf).toInstant());
- } catch (DateTimeParseException e) {
- throw new IllegalArgumentException("Failed to parse as simple date time format", e);
- }
- } else {
- throw new IllegalArgumentException();
- }
- }
- public static PrimitiveDataType forClass(Class<?> clazz) {
- if(clazz.isAssignableFrom(Integer.class)) {
- return PrimitiveDataType.integerType;
- } else if(clazz.isAssignableFrom(Long.class)) {
- return PrimitiveDataType.longType;
- } else if(clazz.isAssignableFrom(Float.class)) {
- return PrimitiveDataType.floatType;
- } else if(clazz.isAssignableFrom(Double.class)) {
- return PrimitiveDataType.doubleType;
- } else if(clazz.isAssignableFrom(String.class)) {
- return PrimitiveDataType.stringType;
- } if (clazz.isAssignableFrom(Boolean.class)) {
- return PrimitiveDataType.booleanType;
- } else if(clazz.isAssignableFrom(Date.class)) {
- return PrimitiveDataType.dateType;
- } else {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement