Class Values
For example, a caller might expects a particular Header to contain an Schema.Type.INT64
value, when in fact that header contains a string representation of a 32-bit integer. Here, the caller can use the methods in this
class to convert the value to the desired type:
Header header = ...
long value = Values.convertToLong(header.schema(), header.value());
This class is able to convert any value to a string representation as well as parse those string representations back into most of
the types. The only exception is Struct values that require a schema and thus cannot be parsed from a simple string.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected static class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionalignListEntriesWithSchema(Schema schema, List<Object> input) alignMapKeysAndValuesWithSchema(Schema mapSchema, Map<Object, Object> input) alignMapKeysWithSchema(Schema mapSchema, Map<Object, Object> input) protected static voidappend(StringBuilder sb, Object value, boolean embedded) protected static voidappendIterable(StringBuilder sb, Iterator<?> iter) protected static doubleConvert the specified value with the desired floating point type.protected static longConvert the specified value to the desired scalar value type.protected static booleancanParseSingleTokenLiteral(Values.Parser parser, boolean embedded, String tokenLiteral) protected static SchemacommonSchemaFor(Schema previous, SchemaAndValue latest) protected static ObjectConvert the value to the desired type.static BooleanconvertToBoolean(Schema schema, Object value) Convert the specified value to anSchema.Type.BOOLEANvalue.static ByteconvertToByte(Schema schema, Object value) Convert the specified value to anSchema.Type.INT8byte value.static DateconvertToDate(Schema schema, Object value) Convert the specified value to andatevalue.static BigDecimalconvertToDecimal(Schema schema, Object value, int scale) Convert the specified value to andecimalvalue.static DoubleconvertToDouble(Schema schema, Object value) Convert the specified value to anSchema.Type.FLOAT64double value.static FloatconvertToFloat(Schema schema, Object value) Convert the specified value to anSchema.Type.FLOAT32float value.static IntegerconvertToInteger(Schema schema, Object value) Convert the specified value to anSchema.Type.INT32int value.static List<?>convertToList(Schema schema, Object value) Convert the specified value to anSchema.Type.ARRAYvalue.static LongconvertToLong(Schema schema, Object value) Convert the specified value to anSchema.Type.INT64long value.static Map<?,?> convertToMap(Schema schema, Object value) Convert the specified value to anSchema.Type.MAPvalue.static ShortconvertToShort(Schema schema, Object value) Convert the specified value to anSchema.Type.INT16short value.static StringconvertToString(Schema schema, Object value) Convert the specified value to anSchema.Type.STRINGvalue.static StructconvertToStruct(Schema schema, Object value) Convert the specified value to anSchema.Type.STRUCTvalue.static DateconvertToTime(Schema schema, Object value) Convert the specified value to antimevalue.static DateconvertToTimestamp(Schema schema, Object value) Convert the specified value to antimestampvalue.static DateFormatdateFormatFor(Date value) protected static Stringstatic SchemainferSchema(Object value) If possible infer a schema for the given value.protected static SchemaAndValueparse(Values.Parser parser, boolean embedded) static SchemaAndValueparseString(String value) Parse the specified string representation of a value into its schema and value.
-
Constructor Details
-
Values
public Values()
-
-
Method Details
-
convertToBoolean
Convert the specified value to anSchema.Type.BOOLEANvalue. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to a boolean.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a boolean, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to a boolean
-
convertToByte
Convert the specified value to anSchema.Type.INT8byte value. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to a byte.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a byte, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to a byte
-
convertToShort
Convert the specified value to anSchema.Type.INT16short value. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to a short.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a short, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to a short
-
convertToInteger
Convert the specified value to anSchema.Type.INT32int value. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to an integer.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as an integer, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to an integer
-
convertToLong
Convert the specified value to anSchema.Type.INT64long value. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to a long.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a long, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to a long
-
convertToFloat
Convert the specified value to anSchema.Type.FLOAT32float value. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to a floating point number.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a float, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to a float
-
convertToDouble
Convert the specified value to anSchema.Type.FLOAT64double value. The supplied schema is required if the value is a logical type when the schema contains critical information that might be necessary for converting to a floating point number.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a double, or null if the supplied value was null
- Throws:
DataException- if the value could not be converted to a double
-
convertToString
Convert the specified value to anSchema.Type.STRINGvalue. Not supplying a schema may limit the ability to convert to the desired type.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a string, or null if the supplied value was null
-
convertToList
Convert the specified value to anSchema.Type.ARRAYvalue. If the value is a string representation of an array, this method will parse the string and its elements to infer the schemas for those elements. Thus, this method supports arrays of other primitives and structured types. If the value is already an array (or list), this method simply casts and returns it.This method currently does not use the schema, though it may be used in the future.
- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a list, or null if the supplied value was null
- Throws:
DataException- if the value cannot be converted to a list value
-
convertToMap
Convert the specified value to anSchema.Type.MAPvalue. If the value is a string representation of a map, this method will parse the string and its entries to infer the schemas for those entries. Thus, this method supports maps with primitives and structured keys and values. If the value is already a map, this method simply casts and returns it.This method currently does not use the schema, though it may be used in the future.
- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a map, or null if the supplied value was null
- Throws:
DataException- if the value cannot be converted to a map value
-
convertToStruct
Convert the specified value to anSchema.Type.STRUCTvalue. Structs cannot be converted from other types, so this method returns a struct only if the supplied value is a struct. If not a struct, this method throws an exception.This method currently does not use the schema, though it may be used in the future.
- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a struct, or null if the supplied value was null
- Throws:
DataException- if the value is not a struct
-
convertToTime
Convert the specified value to antimevalue. Not supplying a schema may limit the ability to convert to the desired type.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a time, or null if the supplied value was null
- Throws:
DataException- if the value cannot be converted to a time value
-
convertToDate
Convert the specified value to andatevalue. Not supplying a schema may limit the ability to convert to the desired type.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a date, or null if the supplied value was null
- Throws:
DataException- if the value cannot be converted to a date value
-
convertToTimestamp
Convert the specified value to antimestampvalue. Not supplying a schema may limit the ability to convert to the desired type.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a timestamp, or null if the supplied value was null
- Throws:
DataException- if the value cannot be converted to a timestamp value
-
convertToDecimal
Convert the specified value to andecimalvalue. Not supplying a schema may limit the ability to convert to the desired type.- Parameters:
schema- the schema for the value; may be nullvalue- the value to be converted; may be null- Returns:
- the representation as a decimal, or null if the supplied value was null
- Throws:
DataException- if the value cannot be converted to a decimal value
-
inferSchema
If possible infer a schema for the given value.- Parameters:
value- the value whose schema is to be inferred; may be null- Returns:
- the inferred schema, or null if the value is null or no schema could be inferred
-
parseString
Parse the specified string representation of a value into its schema and value.- Parameters:
value- the string form of the value- Returns:
- the schema and value; never null, but whose schema and value may be null
- See Also:
-
convertTo
protected static Object convertTo(Schema toSchema, Schema fromSchema, Object value) throws DataException Convert the value to the desired type.- Parameters:
toSchema- the schema for the desired type; may not be nullfromSchema- the schema for the supplied value; may be null if not known- Returns:
- the converted value; never null
- Throws:
DataException- if the value could not be converted to the desired type
-
asLong
Convert the specified value to the desired scalar value type.- Parameters:
value- the value to be converted; may not be nullfromSchema- the schema for the current value type; may not be nullerror- any previous error that should be included in an exception message; may be null- Returns:
- the long value after conversion; never null
- Throws:
DataException- if the value could not be converted to a long
-
asDouble
Convert the specified value with the desired floating point type.- Parameters:
value- the value to be converted; may not be nullschema- the schema for the current value type; may not be nullerror- any previous error that should be included in an exception message; may be null- Returns:
- the double value after conversion; never null
- Throws:
DataException- if the value could not be converted to a double
-
append
-
appendIterable
-
escape
-
dateFormatFor
-
canParseSingleTokenLiteral
protected static boolean canParseSingleTokenLiteral(Values.Parser parser, boolean embedded, String tokenLiteral) -
parse
protected static SchemaAndValue parse(Values.Parser parser, boolean embedded) throws NoSuchElementException - Throws:
NoSuchElementException
-
commonSchemaFor
-
alignListEntriesWithSchema
-
alignMapKeysAndValuesWithSchema
-
alignMapKeysWithSchema
-