-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add DataType so that we can differentiate between decimals and integers
- Loading branch information
Showing
2 changed files
with
58 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...g-data-rest/src/main/java/com/contentgrid/spring/data/rest/webmvc/blueprint/DataType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.contentgrid.spring.data.rest.webmvc.blueprint; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.springframework.lang.Nullable; | ||
|
||
public enum DataType { | ||
STRING, | ||
LONG, | ||
DOUBLE, | ||
BOOLEAN, | ||
DATETIME, | ||
OBJECT; | ||
|
||
private static final Collection<Class<?>> STRING_TYPES = List.of(String.class, UUID.class); | ||
private static final Collection<Class<?>> LONG_TYPES = List.of(int.class, long.class, short.class, | ||
Integer.class, Long.class, Short.class); | ||
private static final Collection<Class<?>> DOUBLE_TYPES = List.of(float.class, double.class, | ||
Float.class, Double.class, BigDecimal.class); | ||
private static final Collection<Class<?>> BOOLEAN_TYPES = List.of(boolean.class, Boolean.class); | ||
private static final Collection<Class<?>> DATETIME_TYPES = List.of(Instant.class, LocalDateTime.class, | ||
OffsetDateTime.class, ZonedDateTime.class); | ||
|
||
@Nullable | ||
public static DataType from(Class<?> type) { | ||
|
||
if (STRING_TYPES.contains(type)) { | ||
return STRING; | ||
} | ||
|
||
if (LONG_TYPES.contains(type)) { | ||
return LONG; | ||
} | ||
|
||
if (DOUBLE_TYPES.contains(type)) { | ||
return DOUBLE; | ||
} | ||
|
||
if (BOOLEAN_TYPES.contains(type)) { | ||
return BOOLEAN; | ||
} | ||
|
||
if (DATETIME_TYPES.contains(type)) { | ||
return DATETIME; | ||
} | ||
|
||
return null; | ||
} | ||
} |