public class DataTypeUtils extends Object
Constructor and Description |
---|
DataTypeUtils() |
Modifier and Type | Method and Description |
---|---|
static DataType |
chooseDataType(Object value,
ChoiceDataType choiceType) |
static Date |
convertDateToLocalTZ(Date dateUTC)
Converts a java.sql.Date object in UTC normalized form
to local time zone (storing the epoch corresponding to the local time with the same date/time as the input).
|
static Date |
convertDateToUTC(Date dateLocalTZ)
Converts a java.sql.Date object in local time zone (typically coming from a java.sql.ResultSet and having 00:00:00 time part)
to UTC normalized form (storing the epoch corresponding to the UTC time with the same date/time as the input).
|
static Object[] |
convertRecordArrayToJavaArray(Object[] array,
DataType elementDataType) |
static Object |
convertRecordFieldtoObject(Object value,
DataType dataType)
Creates a native Java object from a given object of a specified type.
|
static Map<String,Object> |
convertRecordMapToJavaMap(Map<String,Object> map,
DataType valueDataType) |
static Object |
convertType(Object value,
DataType dataType,
String fieldName) |
static Object |
convertType(Object value,
DataType dataType,
String fieldName,
Charset charset) |
static Object |
convertType(Object value,
DataType dataType,
Supplier<DateFormat> dateFormat,
Supplier<DateFormat> timeFormat,
Supplier<DateFormat> timestampFormat,
String fieldName) |
static Object |
convertType(Object value,
DataType dataType,
Supplier<DateFormat> dateFormat,
Supplier<DateFormat> timeFormat,
Supplier<DateFormat> timestampFormat,
String fieldName,
Charset charset) |
static <T> Optional<T> |
findMostSuitableType(Object value,
List<T> types,
Function<T,DataType> dataTypeMapper) |
static <T> Optional<T> |
findMostSuitableTypeByStringValue(String valueAsString,
List<T> types,
Function<T,DataType> dataTypeMapper) |
private static String |
formatDate(Date date,
Supplier<DateFormat> formatSupplier) |
static Charset |
getCharset(String charsetName) |
static DataType |
getDataTypeFromSQLTypeValue(int sqlType)
Converts the specified java.sql.Types constant field data type (INTEGER = 4, e.g.) into a DataType
|
static DateFormat |
getDateFormat(RecordFieldType fieldType,
Supplier<DateFormat> dateFormat,
Supplier<DateFormat> timeFormat,
Supplier<DateFormat> timestampFormat) |
static DateFormat |
getDateFormat(String format) |
static DateFormat |
getDateFormat(String format,
String timezoneID) |
private static int |
getIntegerTypeValue(RecordFieldType fieldType) |
static int |
getSQLTypeValue(DataType dataType)
Converts the specified field data type into a java.sql.Types constant (INTEGER = 4, e.g.)
|
static Optional<DataType> |
getWiderType(DataType thisDataType,
DataType otherDataType) |
static DataType |
inferDataType(Object value,
DataType defaultType) |
private static DataType |
inferRecordDataType(Map<String,?> map) |
static RecordSchema |
inferSchema(Map<String,Object> values,
String fieldName,
Charset charset) |
static boolean |
isArrayTypeCompatible(Object value,
DataType elementDataType) |
static boolean |
isArrayTypeCompatible(Object value,
DataType elementDataType,
boolean strict) |
static boolean |
isBigIntFitsToDouble(Object value)
Returns true if the given value is a BigInteger value and fits into a double variable without precision loss.
|
static boolean |
isBigIntFitsToFloat(Object value)
Returns true if the given value is a BigInteger value and fits into a float variable without precision loss.
|
static boolean |
isBigIntTypeCompatible(Object value) |
static boolean |
isBooleanTypeCompatible(Object value) |
static boolean |
isByteTypeCompatible(Object value) |
static boolean |
isCharacterTypeCompatible(Object value) |
static boolean |
isCompatibleDataType(Object value,
DataType dataType) |
static boolean |
isCompatibleDataType(Object value,
DataType dataType,
boolean strict) |
static boolean |
isDateTypeCompatible(Object value,
String format) |
private static boolean |
isDecimal(String value) |
static boolean |
isDecimalTypeCompatible(Object value) |
private static boolean |
isDouble(String value) |
static boolean |
isDoubleTypeCompatible(Object value) |
static boolean |
isDoubleWithinFloatInterval(Object value)
Returns true in case the incoming value is a double which is within the range of float variable type.
|
static boolean |
isEnumTypeCompatible(Object value,
EnumDataType enumType) |
static boolean |
isFittingNumberType(Object value,
RecordFieldType fieldType)
Checks if an incoming value satisfies the requirements of a given (numeric) type or any of it's narrow data type.
|
private static boolean |
isFloatingPoint(String value) |
static boolean |
isFloatTypeCompatible(Object value) |
private static boolean |
isInteger(String value) |
static boolean |
isIntegerFitsToFloat(Object value)
Returns true if the given value is an integer value and fits into a float variable without precision loss.
|
static boolean |
isIntegerTypeCompatible(Object value) |
private static boolean |
isIntegral(String value)
Check if the value is an integral.
|
private static boolean |
isIntegral(String value,
long minValue,
long maxValue)
Check if the value is an integral within a value range.
|
static boolean |
isLongFitsToDouble(Object value)
Returns true if the given value is a long value and fits into a double variable without precision loss.
|
static boolean |
isLongFitsToFloat(Object value)
Returns true if the given value is a long value and fits into a float variable without precision loss.
|
static boolean |
isLongTypeCompatible(Object value) |
static boolean |
isMapTypeCompatible(Object value) |
private static boolean |
isMergeRequired(RecordField thisField,
RecordField otherField) |
private static boolean |
isNumberTypeCompatible(Object value,
Predicate<String> stringPredicate) |
private static boolean |
isRecordTypeCompatible(RecordSchema schema,
Object value,
boolean strict)
Check if the given record structured object compatible with the schema.
|
static boolean |
isScalarValue(DataType dataType,
Object value) |
static boolean |
isShortTypeCompatible(Object value) |
static boolean |
isStringTypeCompatible(Object value) |
static boolean |
isTimestampTypeCompatible(Object value,
String format) |
static boolean |
isTimeTypeCompatible(Object value,
String format) |
static RecordField |
merge(RecordField thisField,
RecordField otherField) |
static RecordSchema |
merge(RecordSchema thisSchema,
RecordSchema otherSchema) |
static DataType |
mergeDataTypes(DataType thisDataType,
DataType otherDataType) |
static Object[] |
toArray(Object value,
String fieldName,
DataType elementDataType) |
static Object[] |
toArray(Object value,
String fieldName,
DataType elementDataType,
Charset charset) |
static BigDecimal |
toBigDecimal(Object value,
String fieldName) |
static BigInteger |
toBigInt(Object value,
String fieldName) |
static Boolean |
toBoolean(Object value,
String fieldName) |
static Byte |
toByte(Object value,
String fieldName) |
static Character |
toCharacter(Object value,
String fieldName) |
static Date |
toDate(Object value,
Supplier<DateFormat> format,
String fieldName) |
static Double |
toDouble(Object value,
String fieldName) |
private static Object |
toEnum(Object value,
EnumDataType dataType,
String fieldName) |
static Float |
toFloat(Object value,
String fieldName) |
static Integer |
toInteger(Object value,
String fieldName) |
static Long |
toLong(Object value,
String fieldName) |
static Map<String,Object> |
toMap(Object value,
String fieldName) |
static Record |
toRecord(Object value,
RecordSchema recordSchema,
String fieldName) |
static Record |
toRecord(Object value,
RecordSchema recordSchema,
String fieldName,
Charset charset) |
static Record |
toRecord(Object value,
String fieldName) |
static Record |
toRecord(Object value,
String fieldName,
Charset charset) |
static Short |
toShort(Object value,
String fieldName) |
static String |
toString(Object value,
String format) |
static String |
toString(Object value,
String format,
Charset charset) |
static String |
toString(Object value,
Supplier<DateFormat> format) |
static String |
toString(Object value,
Supplier<DateFormat> format,
Charset charset) |
static Time |
toTime(Object value,
Supplier<DateFormat> format,
String fieldName) |
static Timestamp |
toTimestamp(Object value,
Supplier<DateFormat> format,
String fieldName) |
private static final org.slf4j.Logger logger
private static final String OptionalSign
private static final String Infinity
private static final String NotANumber
private static final String Base10Digits
private static final String Base10Decimal
private static final String OptionalBase10Decimal
private static final String Base10Exponent
private static final String OptionalBase10Exponent
private static final String doubleRegex
private static final String decimalRegex
private static final Pattern FLOATING_POINT_PATTERN
private static final Pattern DECIMAL_PATTERN
private static final TimeZone gmt
private static final Supplier<DateFormat> DEFAULT_DATE_FORMAT
private static final Supplier<DateFormat> DEFAULT_TIME_FORMAT
private static final Supplier<DateFormat> DEFAULT_TIMESTAMP_FORMAT
private static final int FLOAT_SIGNIFICAND_PRECISION
private static final int DOUBLE_SIGNIFICAND_PRECISION
private static final Long MAX_GUARANTEED_PRECISE_WHOLE_IN_FLOAT
private static final Long MIN_GUARANTEED_PRECISE_WHOLE_IN_FLOAT
private static final Long MAX_GUARANTEED_PRECISE_WHOLE_IN_DOUBLE
private static final Long MIN_GUARANTEED_PRECISE_WHOLE_IN_DOUBLE
private static final BigInteger MAX_FLOAT_VALUE_IN_BIGINT
private static final BigInteger MIN_FLOAT_VALUE_IN_BIGINT
private static final BigInteger MAX_DOUBLE_VALUE_IN_BIGINT
private static final BigInteger MIN_DOUBLE_VALUE_IN_BIGINT
private static final double MAX_FLOAT_VALUE_IN_DOUBLE
private static final double MIN_FLOAT_VALUE_IN_DOUBLE
private static final Map<RecordFieldType,Predicate<Object>> NUMERIC_VALIDATORS
public static Object convertType(Object value, DataType dataType, String fieldName, Charset charset)
public static DateFormat getDateFormat(RecordFieldType fieldType, Supplier<DateFormat> dateFormat, Supplier<DateFormat> timeFormat, Supplier<DateFormat> timestampFormat)
public static Object convertType(Object value, DataType dataType, Supplier<DateFormat> dateFormat, Supplier<DateFormat> timeFormat, Supplier<DateFormat> timestampFormat, String fieldName)
public static Object convertType(Object value, DataType dataType, Supplier<DateFormat> dateFormat, Supplier<DateFormat> timeFormat, Supplier<DateFormat> timestampFormat, String fieldName, Charset charset)
public static boolean isCompatibleDataType(Object value, DataType dataType, boolean strict)
public static DataType chooseDataType(Object value, ChoiceDataType choiceType)
public static <T> Optional<T> findMostSuitableType(Object value, List<T> types, Function<T,DataType> dataTypeMapper)
public static <T> Optional<T> findMostSuitableTypeByStringValue(String valueAsString, List<T> types, Function<T,DataType> dataTypeMapper)
public static Record toRecord(Object value, RecordSchema recordSchema, String fieldName)
public static Record toRecord(Object value, RecordSchema recordSchema, String fieldName, Charset charset)
public static RecordSchema inferSchema(Map<String,Object> values, String fieldName, Charset charset)
private static boolean isRecordTypeCompatible(RecordSchema schema, Object value, boolean strict)
schema
- record schema, schema validation will not be performed if schema is nullvalue
- the record structured object, i.e. Record or Mapstrict
- check for a strict match, i.e. all fields in the record should have a corresponding entry in the schemapublic static Object[] toArray(Object value, String fieldName, DataType elementDataType, Charset charset)
public static boolean isArrayTypeCompatible(Object value, DataType elementDataType)
public static boolean isArrayTypeCompatible(Object value, DataType elementDataType, boolean strict)
public static Object convertRecordFieldtoObject(Object value, DataType dataType)
value
- The object to be converteddataType
- The type of the provided objectpublic static Map<String,Object> convertRecordMapToJavaMap(Map<String,Object> map, DataType valueDataType)
public static Object[] convertRecordArrayToJavaArray(Object[] array, DataType elementDataType)
public static boolean isMapTypeCompatible(Object value)
public static String toString(Object value, Supplier<DateFormat> format)
public static String toString(Object value, Supplier<DateFormat> format, Charset charset)
private static String formatDate(Date date, Supplier<DateFormat> formatSupplier)
public static boolean isStringTypeCompatible(Object value)
public static boolean isEnumTypeCompatible(Object value, EnumDataType enumType)
private static Object toEnum(Object value, EnumDataType dataType, String fieldName)
public static Date toDate(Object value, Supplier<DateFormat> format, String fieldName)
public static Date convertDateToUTC(Date dateLocalTZ)
dateLocalTZ
- java.sql.Date in local time zonepublic static Date convertDateToLocalTZ(Date dateUTC)
dateUTC
- java.sql.Date in UTC normalized formprivate static boolean isInteger(String value)
public static Time toTime(Object value, Supplier<DateFormat> format, String fieldName)
public static DateFormat getDateFormat(String format)
public static DateFormat getDateFormat(String format, String timezoneID)
public static Timestamp toTimestamp(Object value, Supplier<DateFormat> format, String fieldName)
public static boolean isTimestampTypeCompatible(Object value, String format)
public static BigInteger toBigInt(Object value, String fieldName)
public static boolean isBigIntTypeCompatible(Object value)
public static boolean isDecimalTypeCompatible(Object value)
public static boolean isBooleanTypeCompatible(Object value)
public static BigDecimal toBigDecimal(Object value, String fieldName)
public static boolean isDoubleTypeCompatible(Object value)
private static boolean isNumberTypeCompatible(Object value, Predicate<String> stringPredicate)
public static boolean isFloatTypeCompatible(Object value)
private static boolean isDecimal(String value)
private static boolean isFloatingPoint(String value)
private static boolean isDouble(String value)
public static boolean isLongTypeCompatible(Object value)
private static boolean isIntegral(String value)
private static boolean isIntegral(String value, long minValue, long maxValue)
public static boolean isIntegerTypeCompatible(Object value)
public static boolean isShortTypeCompatible(Object value)
public static boolean isByteTypeCompatible(Object value)
public static boolean isCharacterTypeCompatible(Object value)
public static RecordSchema merge(RecordSchema thisSchema, RecordSchema otherSchema)
private static boolean isMergeRequired(RecordField thisField, RecordField otherField)
public static RecordField merge(RecordField thisField, RecordField otherField)
public static Optional<DataType> getWiderType(DataType thisDataType, DataType otherDataType)
private static int getIntegerTypeValue(RecordFieldType fieldType)
public static int getSQLTypeValue(DataType dataType)
dataType
- the DataType to be convertedpublic static DataType getDataTypeFromSQLTypeValue(int sqlType)
sqlType
- the DataType to be convertedpublic static boolean isIntegerFitsToFloat(Object value)
value
- The value to check.public static boolean isLongFitsToFloat(Object value)
value
- The value to check.public static boolean isLongFitsToDouble(Object value)
value
- The value to check.public static boolean isBigIntFitsToFloat(Object value)
value
- The value to check.public static boolean isBigIntFitsToDouble(Object value)
value
- The value to check.public static boolean isDoubleWithinFloatInterval(Object value)
Note: the method only considers the covered range but not precision. The reason for this is that at this point the double representation might already slightly differs from the original text value.
value
- The value to check.public static boolean isFittingNumberType(Object value, RecordFieldType fieldType)
value
- Incoming value.fieldType
- The expected field type.Copyright © 2021 Apache NiFi Project. All rights reserved.