public class CodecUtils extends Object
| Constructor and Description |
|---|
CodecUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <N extends Number> |
convertNumber(Number value,
Class<? extends N> targetClass)
Converts the given number to the given class, applying loss-less conversions.
|
static <T extends TemporalAccessor> |
convertTemporal(TemporalAccessor value,
Class<? extends T> targetClass,
ZoneId timeZone,
LocalDate epoch)
Converts the given temporal into a temporal of the target class.
|
static String |
formatNumber(Number value,
NumberFormat format)
Formats the given number using the given format.
|
static NumberFormat |
getNumberFormat(String pattern,
Locale locale,
RoundingMode roundingMode,
boolean formatNumbers) |
static FastThreadLocal<NumberFormat> |
getNumberFormatThreadLocal(String pattern,
Locale locale,
RoundingMode roundingMode,
boolean formatNumbers) |
static TemporalFormat |
getTemporalFormat(String pattern,
ZoneId timeZone,
Locale locale,
TimeUnit timeUnit,
ZonedDateTime epoch,
FastThreadLocal<NumberFormat> numberFormat,
boolean useZonedParser) |
static long |
instantToNumber(Instant instant,
TimeUnit timeUnit,
Instant epoch)
|
static <N extends Number> |
narrowNumber(Number value,
Class<? extends N> targetClass,
OverflowStrategy overflowStrategy,
RoundingMode roundingMode)
Attempts to convert the given number to the target class, using loss-less conversions.
|
static Instant |
numberToInstant(Number n,
TimeUnit timeUnit,
Instant epoch)
Converts the given number into an
Instant, expressed in the given time unit, and
relative to the given epoch. |
static ByteBuffer |
parseByteBuffer(String s)
Parses the given string as a
ByteBuffer. |
static DateRange |
parseDateRange(String s)
Parses the given string as a
DateRange. |
static LineString |
parseLineString(String s)
Parses the given string as a
LineString. |
static Locale |
parseLocale(String s) |
static Number |
parseNumber(String s,
NumberFormat format)
Parses the given string using the given
NumberFormat. |
static Number |
parseNumber(String s,
NumberFormat numberFormat,
TemporalFormat temporalFormat,
ZoneId timeZone,
TimeUnit timeUnit,
ZonedDateTime epoch,
Map<String,Boolean> booleanStrings,
List<? extends Number> booleanNumbers)
Parses the given string as a number.
|
static Point |
parsePoint(String s)
Parses the given string as a
Point. |
static Polygon |
parsePolygon(String s)
Parses the given string as a
Polygon. |
static UUID |
parseUUID(String s,
ConvertingCodec<String,Instant> instantCodec,
TimeUUIDGenerator generator)
Parses the given string as a
UUID. |
static BigDecimal |
toBigDecimal(Number value)
Converts the given number into a BigDecimal.
|
static BigInteger |
toBigIntegerExact(Number value)
Converts the given number into a BigInteger, throwing an exception in case of overflow.
|
static Byte |
toByteValueExact(Number value)
Converts the given number into a Byte, throwing an exception in case of overflow.
|
static Double |
toDoubleValueExact(Number value)
Converts the given number into a Double, throwing an exception in case of overflow.
|
static Float |
toFloatValueExact(Number value)
Converts the given number into a Float, throwing an exception in case of overflow.
|
static Instant |
toInstant(TemporalAccessor value,
ZoneId timeZone,
LocalDate epoch)
Converts the given temporal into an
Instant. |
static Integer |
toIntValueExact(Number value)
Converts the given number into an Integer, throwing an exception in case of overflow.
|
static LocalDate |
toLocalDate(TemporalAccessor value,
ZoneId timeZone)
Converts the given temporal into a
LocalDate. |
static LocalDateTime |
toLocalDateTime(TemporalAccessor value,
ZoneId timeZone,
LocalDate epoch)
Converts the given temporal into a
LocalDateTime. |
static LocalTime |
toLocalTime(TemporalAccessor value,
ZoneId timeZone)
Converts the given temporal into a
LocalTime. |
static Long |
toLongValueExact(Number value)
Converts the given number into a Long, throwing an exception in case of overflow.
|
static Short |
toShortValueExact(Number value)
Converts the given number into a Short, throwing an exception in case of overflow.
|
static ZonedDateTime |
toZonedDateTime(TemporalAccessor value,
ZoneId timeZone,
LocalDate epoch)
Converts the given temporal into a
ZonedDateTime. |
public static Number parseNumber(String s, @NonNull NumberFormat numberFormat, @NonNull TemporalFormat temporalFormat, @NonNull ZoneId timeZone, @NonNull TimeUnit timeUnit, @NonNull ZonedDateTime epoch, @NonNull Map<String,Boolean> booleanStrings, @NonNull List<? extends Number> booleanNumbers)
This method tries first to parse the string as a numeric value, using the given decimal formatter; then, if that fails, it tries to parse it as an alphanumeric temporal, using the given parser, and converts it to a numeric timestamp using the given time unit and the given epoch; and if that fails too, it tries to convert it to a boolean number.
s - the string to parse, may be null.numberFormat - the NumberFormat to use to parse numbers; cannot be null.temporalFormat - the parser to use if the string is an alphanumeric temporal; cannot be
null.timeZone - the time zone to use; cannot be null.timeUnit - the time unit to use to convert the alphanumeric temporal to a numeric
timestamp; cannot be null.epoch - the epoch to use to convert the alphanumeric temporal to a numeric timestamp;
cannot be null.booleanStrings - A mapping between booleans and words.booleanNumbers - A mapping between booleans and numbers.Number or null if the string was null or empty.IllegalArgumentException - if the string cannot be parsed.public static <N extends Number> N narrowNumber(Number value, @NonNull Class<? extends N> targetClass, @NonNull OverflowStrategy overflowStrategy, @NonNull RoundingMode roundingMode)
N - The target type.value - the value to convert.targetClass - the target class; cannot be null.overflowStrategy - the overflow strategy; cannot be null.roundingMode - the rounding mode; cannot be null.ArithmeticException - if the number cannot be converted.public static Instant numberToInstant(Number n, @NonNull TimeUnit timeUnit, @NonNull Instant epoch)
Instant, expressed in the given time unit, and
relative to the given epoch.n - the number to parse, may be null.timeUnit - the time unit to use; cannot be null.epoch - the epoch to use; cannot be null.Instant or null if the string was null or empty.ArithmeticException - if the number cannot be converted to a Long.DateTimeException - if the number is too large to be converted to an Instant.public static long instantToNumber(@NonNull
Instant instant,
@NonNull
TimeUnit timeUnit,
@NonNull
Instant epoch)
Instant into a scalar timestamp expressed in the given time unit and
relative to the Unix Epoch.instant - the instant to convert; cannot be null.timeUnit - the time unit to use; cannot be null.epoch - the epoch to use; cannot be null.public static Number parseNumber(String s, @NonNull NumberFormat format) throws ParseException
NumberFormat.s - the string to parse, may be null.format - the format to use; cannot be null.BigDecimal, or null if the input was null or empty.ParseException - if the string cannot be parsed.public static String formatNumber(Number value, @NonNull NumberFormat format) throws NumberFormatException
value - the value to format.format - the format to use; cannot be null.NumberFormatException - if the number cannot be formatted.public static <N extends Number> N convertNumber(Number value, @NonNull Class<? extends N> targetClass) throws ArithmeticException
N - the target type.value - the value to convert.targetClass - the target class; cannot be null.ArithmeticException - if the number cannot be converted to the target class.public static Byte toByteValueExact(@NonNull Number value) throws ArithmeticException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.public static Short toShortValueExact(@NonNull Number value) throws ArithmeticException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.public static Integer toIntValueExact(@NonNull Number value) throws ArithmeticException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.public static Long toLongValueExact(@NonNull Number value) throws ArithmeticException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.public static BigInteger toBigIntegerExact(@NonNull Number value) throws ArithmeticException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.public static Float toFloatValueExact(@NonNull Number value) throws ArithmeticException, NumberFormatException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.NumberFormatException - if the number cannot be converted to a BigDecimal.public static Double toDoubleValueExact(@NonNull Number value) throws ArithmeticException, NumberFormatException
value - the number to convert; cannot be null.ArithmeticException - in case of overflow.NumberFormatException - if the number cannot be converted to a BigDecimal.public static BigDecimal toBigDecimal(@NonNull Number value) throws ArithmeticException
value - the number to convert; cannot be null.ArithmeticException - if the number cannot be converted to a BigDecimal.public static <T extends TemporalAccessor> T convertTemporal(TemporalAccessor value, @NonNull Class<? extends T> targetClass, @NonNull ZoneId timeZone, @NonNull LocalDate epoch) throws DateTimeException
T - the target type.value - the value to convert.targetClass - the target class; cannot be null.timeZone - the time zone to use; cannot be null.epoch - the epoch to use; cannot be null.DateTimeException - if the temporal cannot be converted to the target class.public static ZonedDateTime toZonedDateTime(@NonNull TemporalAccessor value, @NonNull ZoneId timeZone, @NonNull LocalDate epoch) throws DateTimeException
ZonedDateTime.value - the value to convert; cannot be null.timeZone - the time zone to use; cannot be null.epoch - the epoch to use; cannot be null.DateTimeException - if the temporal cannot be converted.public static Instant toInstant(@NonNull TemporalAccessor value, @NonNull ZoneId timeZone, @NonNull LocalDate epoch) throws DateTimeException
Instant.value - the value to convert; cannot be null.timeZone - the time zone to use; cannot be null.epoch - the epoch to use; cannot be null.DateTimeException - if the temporal cannot be converted.public static LocalDateTime toLocalDateTime(@NonNull TemporalAccessor value, @NonNull ZoneId timeZone, @NonNull LocalDate epoch) throws DateTimeException
LocalDateTime.value - the value to convert; cannot be null.timeZone - the time zone to use; cannot be null.epoch - the epoch to use; cannot be null.DateTimeException - if the temporal cannot be converted.public static LocalDate toLocalDate(@NonNull TemporalAccessor value, @NonNull ZoneId timeZone) throws DateTimeException
LocalDate.value - the value to convert; cannot be null.timeZone - the time zone to use; cannot be null.DateTimeException - if the temporal cannot be converted.public static LocalTime toLocalTime(@NonNull TemporalAccessor value, @NonNull ZoneId timeZone) throws DateTimeException
LocalTime.value - the value to convert; cannot be null.timeZone - the time zone to use; cannot be null.DateTimeException - if the temporal cannot be converted.public static UUID parseUUID(String s, @NonNull ConvertingCodec<String,Instant> instantCodec, @NonNull TimeUUIDGenerator generator)
UUID.
First, if the string is a UUID in canonical representation, parses the string using UUID.fromString(String). If that fails, then tries to parse the string as a temporal, then
converts the temporal into a time UUID.
s - the string to parse; may be null.instantCodec - the codec to use to parse temporals; cannot be null.generator - the generator to use to create a time UUIDl cannot be null.@Nullable public static ByteBuffer parseByteBuffer(@Nullable String s)
ByteBuffer.
First, tries to parse the string as a CQL blob literal in hexadecimal notation; if that fails, then tries to parse the string as a Base64-encoded byte array.
s - the string to parse; may be null.ByteBuffer.public static Point parsePoint(String s)
Point.
This method tries the following string formats, and returns the first one that is able to parse the string:
s - the string to parse; may be null.Point, or null if the string was null or empty.public static LineString parseLineString(String s)
LineString.
This method tries the following string formats, and returns the first one that is able to parse the string:
s - the string to parse; may be null.LineString, or null if the string was null or empty.public static Polygon parsePolygon(String s)
Polygon.
This method tries the following string formats, and returns the first one that is able to parse the string:
s - the string to parse; may be null.Polygon, or null if the string was null or empty.public static DateRange parseDateRange(String s)
DateRange.
This method simply delegates to DateRange.parse(String), except when the string is
null or empty, in which case it returns null.
s - the string to parse; may be null.DateRange, or null if the string was null or empty.public static FastThreadLocal<NumberFormat> getNumberFormatThreadLocal(String pattern, Locale locale, RoundingMode roundingMode, boolean formatNumbers)
public static NumberFormat getNumberFormat(String pattern, Locale locale, RoundingMode roundingMode, boolean formatNumbers)
public static TemporalFormat getTemporalFormat(@NonNull String pattern, @NonNull ZoneId timeZone, @NonNull Locale locale, @NonNull TimeUnit timeUnit, @NonNull ZonedDateTime epoch, @NonNull FastThreadLocal<NumberFormat> numberFormat, boolean useZonedParser)
Copyright © 2017–2021 DataStax. All rights reserved.