Package io.bytestreams.codec.core.util
Class Converters
java.lang.Object
io.bytestreams.codec.core.util.Converters
Factory methods for creating
Converter instances.-
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 <T extends TemporalAccessor>
Converter<String, T> temporal(String format, TemporalQuery<T> query) toInt(int digits) toLong(int digits)
-
Method Details
-
of
Creates a converter from two functions.- Type Parameters:
V- the source typeU- the target type- Parameters:
to- the forward conversion functionfrom- the reverse conversion function- Returns:
- a new converter
-
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
Returns a converter that left-pads strings to an even length onfromand strips leading pad characters onto.- Parameters:
padChar- the character to pad with- Returns:
- a left-even-padding converter
-
rightEvenPad
Returns a converter that right-pads strings to an even length onfromand strips trailing pad characters onto.- Parameters:
padChar- the character to pad with- Returns:
- a right-even-padding converter
-
toInt
Returns a converter that parses strings to integers ontoand formats integers to zero-padded strings onfrom.- 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.- 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.- 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
-