Class Converters
-
Method Summary
Modifier and TypeMethodDescriptionleftEvenPad(char padChar) leftFitPad(char padChar, int length) leftPad(char padChar, int length) static <V,U> Converter <V, U> Creates a converter from two functions.rightEvenPad(char padChar) rightFitPad(char padChar, int length) rightPad(char padChar, int length) static Converter<Long, BigDecimal> scaled(int scale) Returns a converter between an integer of minor units and aBigDecimalwith an implied decimal point.static <T extends TemporalAccessor>
Converter<String, T> temporal(String format, TemporalQuery<T> query) static <T extends TemporalAccessor>
Converter<String, T> temporal(DateTimeFormatter formatter, TemporalQuery<T> query) static Converter<String, BigInteger> toBigInt(int digits) Returns a converter that parses strings toBigIntegers ontoand formats them to zero-padded strings onfrom.toInt(int digits) toLong(int digits)
-
Method Details
-
of
-
rightPad
Returns a converter that right-pads strings to a fixed length onfromand strips trailing pad characters onto.- Parameters:
padChar- the character to pad withlength- the target length- Returns:
- a right-padding converter
- Throws:
IllegalArgumentException- if length is not positive
-
leftPad
Returns a converter that left-pads strings to a fixed length onfromand strips leading pad characters onto.- Parameters:
padChar- the character to pad withlength- the target length- Returns:
- a left-padding converter
- Throws:
IllegalArgumentException- if length is not positive
-
rightFitPad
Returns a converter that right-pads or right-truncates strings to a fixed length onfromand strips trailing pad characters onto.- Parameters:
padChar- the character to pad withlength- the target length- Returns:
- a right-fit-padding converter
- Throws:
IllegalArgumentException- if length is not positive
-
leftFitPad
Returns a converter that left-pads or left-truncates strings to a fixed length onfromand strips leading pad characters onto.- Parameters:
padChar- the character to pad withlength- the target length- Returns:
- a left-fit-padding converter
- Throws:
IllegalArgumentException- if length is not positive
-
leftEvenPad
-
rightEvenPad
-
toBigInt
Returns a converter that parses strings toBigIntegers ontoand formats them to zero-padded strings onfrom.For numeric fields wider than a
longholds. COBOL allowsPIC 9(31), andtoLong(int)stops at eighteen digits.Codec<BigInteger> reference = Codecs.ascii(25).xmap(Converters.toBigInt(25));Zero-padded numerics are unsigned. A negative value is rejected on
from, since padding it would put the minus sign inside the zeros. Use a zoned or packed decimal codec for a field that carries a sign.Decoding is more permissive and accepts a leading sign, so a non-conforming field can be read but not written back unchanged.
- Parameters:
digits- the number of digits for zero-padded formatting- Returns:
- a string-to-BigInteger converter
- Throws:
IllegalArgumentException- if digits is not positive
-
scaled
Returns a converter between an integer of minor units and aBigDecimalwith an implied decimal point.Many formats store a number as an integer whose decimal point position comes from the specification rather than the data: ISO 8583 field 4 is twelve digits of minor units, and COBOL writes
PIC S9(7)V99with theVmarking a point that occupies no storage.Codec<BigDecimal> amount = Codecs.packedLong(9).xmap(Converters.scaled(2)); Codec<BigDecimal> field4 = Codecs.asciiLong(12).xmap(Converters.scaled(2));Conversion is exact in both directions and never goes through
double, which cannot represent most decimal fractions.fromrejects a value carrying more precision than the scale allows rather than discarding it:123.456at scale 2 is an error, not123.45. Round explicitly beforehand if that is what you want.- Parameters:
scale- the number of implied decimal places- Returns:
- a converter between minor units and a decimal amount
- Throws:
IllegalArgumentException- if scale is negative
-
toInt
Returns a converter that parses strings to integers ontoand formats integers to zero-padded strings onfrom.Zero-padded numerics are unsigned. A negative value is rejected on
from, since padding it would put the minus sign inside the zeros. Use a zoned or packed decimal codec for a field that carries a sign.Decoding is more permissive and accepts a leading sign, so a non-conforming field can be read but not written back unchanged.
- Parameters:
digits- the number of digits for zero-padded formatting- Returns:
- a string-to-integer converter
- Throws:
IllegalArgumentException- if digits is not positive
-
toLong
Returns a converter that parses strings to longs ontoand formats longs to zero-padded strings onfrom.Zero-padded numerics are unsigned. A negative value is rejected on
from, since padding it would put the minus sign inside the zeros. Use a zoned or packed decimal codec for a field that carries a sign.Decoding is more permissive and accepts a leading sign, so a non-conforming field can be read but not written back unchanged.
- Parameters:
digits- the number of digits for zero-padded formatting- Returns:
- a string-to-long converter
- Throws:
IllegalArgumentException- if digits is not positive
-
temporal
public static <T extends TemporalAccessor> Converter<String,T> temporal(String format, TemporalQuery<T> query) Returns a converter that parses strings to temporal values ontoand formats temporal values to strings onfrom.The pattern is compiled with
DateTimeFormatter.ofPattern(String), which uses theSMARTresolver style. Under SMART resolution an out-of-range date such as02/31is adjusted to the last valid day of the month rather than rejected. Usetemporal(DateTimeFormatter, TemporalQuery)to supply a formatter with different behaviour, such asSTRICTresolution or a specific locale.- Type Parameters:
T- the temporal type- Parameters:
format- the date-time format pattern (seeDateTimeFormatter)query- the temporal query for parsing (e.g.LocalDate::from)- Returns:
- a string-to-temporal converter
- Throws:
ConverterException- if the string cannot be parsed
-
temporal
public static <T extends TemporalAccessor> Converter<String,T> temporal(DateTimeFormatter formatter, TemporalQuery<T> query) Returns a converter that parses strings to temporal values ontoand formats temporal values to strings onfromusing the given formatter.Supplying a pre-built formatter gives full control over parsing behaviour, including
resolver styleand locale. For example, a formatter withSTRICTresolution rejects an invalid date such as02/31instead of adjusting it.- Type Parameters:
T- the temporal type- Parameters:
formatter- the date-time formatterquery- the temporal query for parsing (e.g.LocalDate::from)- Returns:
- a string-to-temporal converter
- Throws:
ConverterException- if the string cannot be parsed
-