/** * Convert the specified value to an {@link Type#STRING} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a string, or null if the supplied value was null */ public static String convertToString(Schema schema, Object value) { return (String) convertTo(Schema.OPTIONAL_STRING_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT8} byte 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. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Byte convertToByte(Schema schema, Object value) throws DataException { return (Byte) convertTo(Schema.OPTIONAL_INT8_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#FLOAT64} double 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. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Double convertToDouble(Schema schema, Object value) throws DataException { return (Double) convertTo(Schema.OPTIONAL_FLOAT64_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Timestamp#SCHEMA timestamp} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static java.util.Date convertToTimestamp(Schema schema, Object value) { return (java.util.Date) convertTo(Timestamp.SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#BOOLEAN} 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 boolean. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Boolean convertToBoolean(Schema schema, Object value) throws DataException { return (Boolean) convertTo(Schema.OPTIONAL_BOOLEAN_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT32} int 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. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Integer convertToInteger(Schema schema, Object value) throws DataException { return (Integer) convertTo(Schema.OPTIONAL_INT32_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT64} long 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. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Long convertToLong(Schema schema, Object value) throws DataException { return (Long) convertTo(Schema.OPTIONAL_INT64_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Time#SCHEMA time} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static java.util.Date convertToTime(Schema schema, Object value) { return (java.util.Date) convertTo(Time.SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Date#SCHEMA date} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static java.util.Date convertToDate(Schema schema, Object value) { return (java.util.Date) convertTo(Date.SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT16} short 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. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Short convertToShort(Schema schema, Object value) throws DataException { return (Short) convertTo(Schema.OPTIONAL_INT16_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#FLOAT32} float 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. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Float convertToFloat(Schema schema, Object value) throws DataException { return (Float) convertTo(Schema.OPTIONAL_FLOAT32_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#STRUCT} value. 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. * * <p>This method currently does not use the schema, though it may be used in the future.</p> * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a struct, or null if the supplied value was null * @throws DataException if the value is not a struct */ public static Struct convertToStruct(Schema schema, Object value) { return (Struct) convertTo(STRUCT_SELECTOR_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#MAP} value. 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. * * <p>This method currently does not use the schema, though it may be used in the future.</p> * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static Map<?, ?> convertToMap(Schema schema, Object value) { return (Map<?, ?>) convertTo(MAP_SELECTOR_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#ARRAY} value. 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. * * <p>This method currently does not use the schema, though it may be used in the future.</p> * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static List<?> convertToList(Schema schema, Object value) { return (List<?>) convertTo(ARRAY_SELECTOR_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Decimal decimal} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return 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 */ public static BigDecimal convertToDecimal(Schema schema, Object value, int scale) { return (BigDecimal) convertTo(Decimal.schema(scale), schema, value); }
protected static List<Object> alignListEntriesWithSchema(Schema schema, List<Object> input) { if (schema == null) { return input; } Schema valueSchema = schema.valueSchema(); List<Object> result = new ArrayList<>(); for (Object value : input) { Object newValue = convertTo(valueSchema, null, value); result.add(newValue); } return result; }
protected static Map<Object, Object> alignMapKeysAndValuesWithSchema(Schema mapSchema, Map<Object, Object> input) { if (mapSchema == null) { return input; } Schema keySchema = mapSchema.keySchema(); Schema valueSchema = mapSchema.valueSchema(); Map<Object, Object> result = new LinkedHashMap<>(); for (Map.Entry<?, ?> entry : input.entrySet()) { Object newKey = convertTo(keySchema, null, entry.getKey()); Object newValue = convertTo(valueSchema, null, entry.getValue()); result.put(newKey, newValue); } return result; }