Class TomlBindings
java.lang.Object
be.imgn.toml.TomlBindings
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordWhere a value is in the document, and how to bind it.static interfaceConverts a value. -
Method Summary
Modifier and TypeMethodDescriptionstatic <V extends TomlValue>
TomlBindings.Converter<V> Returns a converter that checks the type of a value.static booleanasBoolean(TomlValue value, TomlBindings.Context context) Converts a boolean.static byteasByte(TomlValue value, TomlBindings.Context context) Converts an integer to abyte.static doubleasDouble(TomlValue value, TomlBindings.Context context) Converts a float, or an integer that adoublerepresents exactly, to adouble.static DurationasDuration(TomlValue value, TomlBindings.Context context) Converts a string to a duration.static floatasFloat(TomlValue value, TomlBindings.Context context) Converts a float in the range offloat, or an integer that afloatrepresents exactly, to afloat.static intasInt(TomlValue value, TomlBindings.Context context) Converts an integer to anint.static LocalDateasLocalDate(TomlValue value, TomlBindings.Context context) Converts a local date.static LocalDateTimeasLocalDateTime(TomlValue value, TomlBindings.Context context) Converts a local date-time.static LocalTimeasLocalTime(TomlValue value, TomlBindings.Context context) Converts a local time.static longasLong(TomlValue value, TomlBindings.Context context) Converts an integer to along.static OffsetDateTimeasOffsetDateTime(TomlValue value, TomlBindings.Context context) Converts an offset date-time.static PathasPath(TomlValue value, TomlBindings.Context context) Converts a string to a path of the default file system.static shortasShort(TomlValue value, TomlBindings.Context context) Converts an integer to ashort.static StringasString(TomlValue value, TomlBindings.Context context) Converts a string.static URIasUri(TomlValue value, TomlBindings.Context context) Converts a string to a URI, asURI(String)parses it.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.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.static TomlMappingExceptionnotOneOf(TomlValue value, TomlBindings.Context context, String... expected) Returns the exception reporting that a string is none of those an enum binds from.static <T> @Nullable Toptional(TomlTable table, TomlBindings.Context context, String key, TomlBindings.Converter<? extends T> converter) Converts the value of a key that a table may lack.static <T> voidput(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.static <T> voidputIfNotNull(TomlTable.Builder builder, String key, @Nullable T value, Function<? super T, ? extends TomlValue> converter) Puts the value of a@Nullablerecord component into a table being built, unless it is null.static TomlMappingExceptionrejected(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.static <T> Trequired(TomlTable table, TomlBindings.Context context, String key, TomlBindings.Converter<? extends T> converter) Converts the value of a key that a table must have.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.static TomlTabletable(TomlValue value, TomlBindings.Context context, String... keys) Returns a value that is a table, after checking that it has no other key thankeys, unless the context ignores unknown keys.static <T> TomlArraytoArray(Collection<T> values, Function<? super T, ? extends TomlValue> element) Converts a list or a set to an array, in its iteration order.static <T> TomlTableConverts a map to a table, in its iteration order.
-
Method Details
-
table
Returns a value that is a table, after checking that it has no other key thankeys, unless the context ignores unknown keys.- Parameters:
value- the valuecontext- the context of the valuekeys- the keys the table may have- Returns:
- the table
- Throws:
TomlMappingException- ifvalueis not a table, or has another key that the context rejectsNullPointerException- if an argument or a key is nullIllegalArgumentException- ifkeyshas 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 tablecontext- the context of the tablekey- the keyconverter- converts the value ofkey- Returns:
- the converted value
- Throws:
TomlMappingException- iftablehas no value forkey, or if it does not convertNullPointerException- 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 tablecontext- the context of the tablekey- the keyconverter- converts the value ofkey- Returns:
- the converted value, or null if
tablehas no value forkey - Throws:
TomlMappingException- if the value does not convertNullPointerException- if an argument is null
-
rejected
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 fromcause- 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 stringcontext- the context of the stringexpected- the strings the enum binds from- Returns:
- the exception to throw
- Throws:
NullPointerException- if an argument or an expected string is null
-
as
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- iftypeis null
-
list
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- ifelementis null
-
set
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
TomlMappingExceptionif two elements are equal once converted - Throws:
NullPointerException- ifelementis 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- ifentryis null
-
asString
Converts a string.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the string
- Throws:
TomlMappingException- ifvalueis not a stringNullPointerException- if an argument is null
-
asLong
Converts an integer to along.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the integer
- Throws:
TomlMappingException- ifvalueis not an integerNullPointerException- if an argument is null
-
asInt
Converts an integer to anint.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the integer
- Throws:
TomlMappingException- ifvalueis not an integer in the range ofintNullPointerException- if an argument is null
-
asShort
Converts an integer to ashort.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the integer
- Throws:
TomlMappingException- ifvalueis not an integer in the range ofshortNullPointerException- if an argument is null
-
asByte
Converts an integer to abyte.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the integer
- Throws:
TomlMappingException- ifvalueis not an integer in the range ofbyteNullPointerException- if an argument is null
-
asDouble
Converts a float, or an integer that adoublerepresents exactly, to adouble.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the number
- Throws:
TomlMappingException- ifvalueis neither a float nor such an integerNullPointerException- if an argument is null
-
asFloat
Converts a float in the range offloat, or an integer that afloatrepresents exactly, to afloat. A float is rounded to the nearestfloat.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the number
- Throws:
TomlMappingException- ifvalueis neither such a float nor such an integerNullPointerException- if an argument is null
-
asBoolean
Converts a boolean.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the boolean
- Throws:
TomlMappingException- ifvalueis not a booleanNullPointerException- if an argument is null
-
asOffsetDateTime
Converts an offset date-time.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the date-time
- Throws:
TomlMappingException- ifvalueis not an offset date-timeNullPointerException- if an argument is null
-
asLocalDateTime
Converts a local date-time.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the date-time
- Throws:
TomlMappingException- ifvalueis not a local date-timeNullPointerException- if an argument is null
-
asLocalDate
Converts a local date.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the date
- Throws:
TomlMappingException- ifvalueis not a local dateNullPointerException- if an argument is null
-
asLocalTime
Converts a local time.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the time
- Throws:
TomlMappingException- ifvalueis not a local timeNullPointerException- if an argument is null
-
asDuration
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 ared(24 hours),h,m,s,ms,usandns. The total must be a whole number of nanoseconds.
- Parameters:
value- the valuecontext- the context of the value- Returns:
- the duration
- Throws:
TomlMappingException- ifvalueis not a string, or not a durationNullPointerException- if an argument is null
- in ISO-8601, as
-
asPath
Converts a string to a path of the default file system.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the path
- Throws:
TomlMappingException- ifvalueis not a string, or not a pathNullPointerException- if an argument is null
-
asUri
Converts a string to a URI, asURI(String)parses it.- Parameters:
value- the valuecontext- the context of the value- Returns:
- the URI
- Throws:
TomlMappingException- ifvalueis not a string, or not a URINullPointerException- 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 tablekey- the key of the componentvalue- the value of the componentconverter- convertsvalueto a TOML value- Throws:
NullPointerException- if an argument is null, or a value invalueIllegalArgumentException- if a string invaluecontains 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@Nullablerecord component into a table being built, unless it is null.- Type Parameters:
T- the type of the value- Parameters:
builder- the builder of the tablekey- the key of the componentvalue- the value of the component, or null to leave the key outconverter- convertsvalueto a TOML value- Throws:
NullPointerException- if an argument butvalueis null, or a value invalueIllegalArgumentException- if a string invaluecontains 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 elementselement- 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 mapentry- converts the value of each key- Returns:
- the table
- Throws:
NullPointerException- if an argument, a key or a value is nullIllegalArgumentException- if a key contains an unpaired surrogate
-