Class Converters

java.lang.Object
io.bytestreams.codec.core.util.Converters

public final class Converters extends Object
Factory methods for creating Converter instances.
  • Method Details

    • of

      public static <V, U> Converter<V,U> of(Function<V,U> to, Function<U,V> from)
      Creates a converter from two functions.
      Type Parameters:
      V - the source type
      U - the target type
      Parameters:
      to - the forward conversion function
      from - the reverse conversion function
      Returns:
      a new converter
    • rightPad

      public static Converter<String,String> rightPad(char padChar, int length)
      Returns a converter that right-pads strings to a fixed length on from and strips trailing pad characters on to.
      Parameters:
      padChar - the character to pad with
      length - the target length
      Returns:
      a right-padding converter
      Throws:
      IllegalArgumentException - if length is not positive
    • leftPad

      public static Converter<String,String> leftPad(char padChar, int length)
      Returns a converter that left-pads strings to a fixed length on from and strips leading pad characters on to.
      Parameters:
      padChar - the character to pad with
      length - the target length
      Returns:
      a left-padding converter
      Throws:
      IllegalArgumentException - if length is not positive
    • rightFitPad

      public static Converter<String,String> rightFitPad(char padChar, int length)
      Returns a converter that right-pads or right-truncates strings to a fixed length on from and strips trailing pad characters on to.
      Parameters:
      padChar - the character to pad with
      length - the target length
      Returns:
      a right-fit-padding converter
      Throws:
      IllegalArgumentException - if length is not positive
    • leftFitPad

      public static Converter<String,String> leftFitPad(char padChar, int length)
      Returns a converter that left-pads or left-truncates strings to a fixed length on from and strips leading pad characters on to.
      Parameters:
      padChar - the character to pad with
      length - the target length
      Returns:
      a left-fit-padding converter
      Throws:
      IllegalArgumentException - if length is not positive
    • leftEvenPad

      public static Converter<String,String> leftEvenPad(char padChar)
      Returns a converter that left-pads strings to an even length on from and strips leading pad characters on to.
      Parameters:
      padChar - the character to pad with
      Returns:
      a left-even-padding converter
    • rightEvenPad

      public static Converter<String,String> rightEvenPad(char padChar)
      Returns a converter that right-pads strings to an even length on from and strips trailing pad characters on to.
      Parameters:
      padChar - the character to pad with
      Returns:
      a right-even-padding converter
    • toInt

      public static Converter<String,Integer> toInt(int digits)
      Returns a converter that parses strings to integers on to and formats integers to zero-padded strings on from.
      Parameters:
      digits - the number of digits for zero-padded formatting
      Returns:
      a string-to-integer converter
      Throws:
      IllegalArgumentException - if digits is not positive
    • toLong

      public static Converter<String,Long> toLong(int digits)
      Returns a converter that parses strings to longs on to and formats longs to zero-padded strings on from.
      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 on to and formats temporal values to strings on from.
      Type Parameters:
      T - the temporal type
      Parameters:
      format - the date-time format pattern (see DateTimeFormatter)
      query - the temporal query for parsing (e.g. LocalDate::from)
      Returns:
      a string-to-temporal converter
      Throws:
      ConverterException - if the string cannot be parsed