Class TomlBindings

java.lang.Object
be.imgn.toml.TomlBindings

public final class TomlBindings extends Object
The conversions that the code the annotation processor generates binds and writes values with. They are public for that code only: applications bind tables with Toml.bind(TomlTable, Class), and write records with Toml.toTable(Record).

Each conversion to a Java value takes a value and its TomlBindings.Context, which locates the value in the document, and throws a TomlMappingException at its path if the value does not convert.

Since:
0.1.0
  • Method Details

    • table

      public static TomlTable table(TomlValue value, TomlBindings.Context context, String... keys)
      Returns a value that is a table, after checking that it has no other key than keys, unless the context ignores unknown keys.
      Parameters:
      value - the value
      context - the context of the value
      keys - the keys the table may have
      Returns:
      the table
      Throws:
      TomlMappingException - if value is not a table, or has another key that the context rejects
      NullPointerException - if an argument or a key is null
      IllegalArgumentException - if keys has the same key twice
    • required

      public static <T> T required(TomlTable table, TomlBindings.Context context, String key, TomlBindings.Converter<? extends T> converter)
      Converts the value of a key that a table must have.
      Type Parameters:
      T - the type of the converted value
      Parameters:
      table - the table
      context - the context of the table
      key - the key
      converter - converts the value of key
      Returns:
      the converted value
      Throws:
      TomlMappingException - if table has no value for key, or if it does not convert
      NullPointerException - if an argument is null
    • optional

      public static <T> @Nullable T optional(TomlTable table, TomlBindings.Context context, String key, TomlBindings.Converter<? extends T> converter)
      Converts the value of a key that a table may lack.
      Type Parameters:
      T - the type of the converted value
      Parameters:
      table - the table
      context - the context of the table
      key - the key
      converter - converts the value of key
      Returns:
      the converted value, or null if table has no value for key
      Throws:
      TomlMappingException - if the value does not convert
      NullPointerException - if an argument is null
    • rejected

      public static TomlMappingException rejected(TomlBindings.Context context, RuntimeException cause)
      Returns the exception reporting that a record rejected the values it was given, by throwing an exception from its constructor.
      Parameters:
      context - the context of the table the record was bound from
      cause - the exception the constructor threw
      Returns:
      the exception to throw
      Throws:
      NullPointerException - if an argument is null
    • notOneOf

      public static TomlMappingException notOneOf(TomlValue value, TomlBindings.Context context, String... expected)
      Returns the exception reporting that a string is none of those an enum binds from.
      Parameters:
      value - the string
      context - the context of the string
      expected - the strings the enum binds from
      Returns:
      the exception to throw
      Throws:
      NullPointerException - if an argument or an expected string is null
    • as

      public static <V extends TomlValue> TomlBindings.Converter<V> as(Class<V> type)
      Returns a converter that checks the type of a value.
      Type Parameters:
      V - the type of the value
      Parameters:
      type - the type of the value
      Returns:
      the converter
      Throws:
      NullPointerException - if type is null
    • list

      public static <T> TomlBindings.Converter<List<T>> list(TomlBindings.Converter<? extends T> element)
      Returns a converter of an array to a list, which cannot be modified.
      Type Parameters:
      T - the type of the elements
      Parameters:
      element - converts each element
      Returns:
      the converter
      Throws:
      NullPointerException - if element is null
    • set

      public static <T> TomlBindings.Converter<Set<T>> set(TomlBindings.Converter<? extends T> element)
      Returns a converter of an array to a set, which keeps the order of the array and cannot be modified.
      Type Parameters:
      T - the type of the elements
      Parameters:
      element - converts each element
      Returns:
      the converter, which throws a TomlMappingException if two elements are equal once converted
      Throws:
      NullPointerException - if element is null
    • map

      public static <T> TomlBindings.Converter<Map<String,T>> map(TomlBindings.Converter<? extends T> entry)
      Returns a converter of a table to a map, which keeps the order of the table and cannot be modified.
      Type Parameters:
      T - the type of the values
      Parameters:
      entry - converts the value of each key
      Returns:
      the converter
      Throws:
      NullPointerException - if entry is null
    • asString

      public static String asString(TomlValue value, TomlBindings.Context context)
      Converts a string.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the string
      Throws:
      TomlMappingException - if value is not a string
      NullPointerException - if an argument is null
    • asLong

      public static long asLong(TomlValue value, TomlBindings.Context context)
      Converts an integer to a long.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the integer
      Throws:
      TomlMappingException - if value is not an integer
      NullPointerException - if an argument is null
    • asInt

      public static int asInt(TomlValue value, TomlBindings.Context context)
      Converts an integer to an int.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the integer
      Throws:
      TomlMappingException - if value is not an integer in the range of int
      NullPointerException - if an argument is null
    • asShort

      public static short asShort(TomlValue value, TomlBindings.Context context)
      Converts an integer to a short.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the integer
      Throws:
      TomlMappingException - if value is not an integer in the range of short
      NullPointerException - if an argument is null
    • asByte

      public static byte asByte(TomlValue value, TomlBindings.Context context)
      Converts an integer to a byte.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the integer
      Throws:
      TomlMappingException - if value is not an integer in the range of byte
      NullPointerException - if an argument is null
    • asDouble

      public static double asDouble(TomlValue value, TomlBindings.Context context)
      Converts a float, or an integer that a double represents exactly, to a double.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the number
      Throws:
      TomlMappingException - if value is neither a float nor such an integer
      NullPointerException - if an argument is null
    • asFloat

      public static float asFloat(TomlValue value, TomlBindings.Context context)
      Converts a float in the range of float, or an integer that a float represents exactly, to a float. A float is rounded to the nearest float.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the number
      Throws:
      TomlMappingException - if value is neither such a float nor such an integer
      NullPointerException - if an argument is null
    • asBoolean

      public static boolean asBoolean(TomlValue value, TomlBindings.Context context)
      Converts a boolean.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the boolean
      Throws:
      TomlMappingException - if value is not a boolean
      NullPointerException - if an argument is null
    • asOffsetDateTime

      public static OffsetDateTime asOffsetDateTime(TomlValue value, TomlBindings.Context context)
      Converts an offset date-time.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the date-time
      Throws:
      TomlMappingException - if value is not an offset date-time
      NullPointerException - if an argument is null
    • asLocalDateTime

      public static LocalDateTime asLocalDateTime(TomlValue value, TomlBindings.Context context)
      Converts a local date-time.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the date-time
      Throws:
      TomlMappingException - if value is not a local date-time
      NullPointerException - if an argument is null
    • asLocalDate

      public static LocalDate asLocalDate(TomlValue value, TomlBindings.Context context)
      Converts a local date.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the date
      Throws:
      TomlMappingException - if value is not a local date
      NullPointerException - if an argument is null
    • asLocalTime

      public static LocalTime asLocalTime(TomlValue value, TomlBindings.Context context)
      Converts a local time.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the time
      Throws:
      TomlMappingException - if value is not a local time
      NullPointerException - if an argument is null
    • asDuration

      public static Duration asDuration(TomlValue value, TomlBindings.Context context)
      Converts a string to a duration. The string is either:
      • in ISO-8601, as Duration.parse(CharSequence) reads it, such as "PT1M30S" or "-P2DT3H"; or
      • one or more amounts, each followed by its unit, the units in decreasing order and at most once each, after an optional - sign, such as "1h30m", "2.5s" or "-500ms". An amount is digits, optionally followed by a dot and more digits. The units are d (24 hours), h, m, s, ms, us and ns. The total must be a whole number of nanoseconds.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the duration
      Throws:
      TomlMappingException - if value is not a string, or not a duration
      NullPointerException - if an argument is null
    • asPath

      public static Path asPath(TomlValue value, TomlBindings.Context context)
      Converts a string to a path of the default file system.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the path
      Throws:
      TomlMappingException - if value is not a string, or not a path
      NullPointerException - if an argument is null
    • asUri

      public static URI asUri(TomlValue value, TomlBindings.Context context)
      Converts a string to a URI, as URI(String) parses it.
      Parameters:
      value - the value
      context - the context of the value
      Returns:
      the URI
      Throws:
      TomlMappingException - if value is not a string, or not a URI
      NullPointerException - if an argument is null
    • put

      public static <T> void put(TomlTable.Builder builder, String key, T value, Function<? super T, ? extends TomlValue> converter)
      Puts the value of a record component that cannot be null into a table being built.
      Type Parameters:
      T - the type of the value
      Parameters:
      builder - the builder of the table
      key - the key of the component
      value - the value of the component
      converter - converts value to a TOML value
      Throws:
      NullPointerException - if an argument is null, or a value in value
      IllegalArgumentException - if a string in value contains an unpaired surrogate
    • putIfNotNull

      public static <T> void putIfNotNull(TomlTable.Builder builder, String key, @Nullable T value, Function<? super T, ? extends TomlValue> converter)
      Puts the value of a @Nullable record component into a table being built, unless it is null.
      Type Parameters:
      T - the type of the value
      Parameters:
      builder - the builder of the table
      key - the key of the component
      value - the value of the component, or null to leave the key out
      converter - converts value to a TOML value
      Throws:
      NullPointerException - if an argument but value is null, or a value in value
      IllegalArgumentException - if a string in value contains an unpaired surrogate
    • toArray

      public static <T> TomlArray toArray(Collection<T> values, Function<? super T, ? extends TomlValue> element)
      Converts a list or a set to an array, in its iteration order.
      Type Parameters:
      T - the type of the elements
      Parameters:
      values - the elements
      element - converts each element
      Returns:
      the array
      Throws:
      NullPointerException - if an argument or an element is null
    • toTable

      public static <T> TomlTable toTable(Map<String,T> values, Function<? super T, ? extends TomlValue> entry)
      Converts a map to a table, in its iteration order.
      Type Parameters:
      T - the type of the values
      Parameters:
      values - the map
      entry - converts the value of each key
      Returns:
      the table
      Throws:
      NullPointerException - if an argument, a key or a value is null
      IllegalArgumentException - if a key contains an unpaired surrogate