Interface TomlArray
- All Known Subinterfaces:
MutableTomlArray
An array that can be edited is a MutableTomlArray; the arrays of a parse result are.
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> TBind this array to a Java type, with the default options.default <T> Tas(Class<T> type, TomlBindOptions options) Bind this array to a Java type.default <T> Tas(GenericType<T> type) Bind this array to a generic Java type, with the default options.default <T> Tas(GenericType<T> type, TomlBindOptions options) Bind this array to a generic Java type.default @Nullable TomlCommentcomment(int index, TomlComment.Placement placement) Get the comment attached to a value at a placement.default List<TomlComment> comments(int index) Get the comments attached to a value.elements()Get the elements written in this array, in document order.entry(int index) Get the entry at an index.default Objectget(int index) Get a value at a specified index.default TomlArraygetArray(int index) Get an array at a specified index.default booleangetBoolean(int index) Get a boolean at a specified index.default doublegetDouble(int index) Get a double at a specified index.default LocalDategetLocalDate(int index) Get a local date at a specified index.default LocalDateTimegetLocalDateTime(int index) Get a local date time at a specified index.default LocalTimegetLocalTime(int index) Get a local time at a specified index.default longgetLong(int index) Get a long at a specified index.default OffsetDateTimegetOffsetDateTime(int index) Get an offset date time at a specified index.default StringgetString(int index) Get a string at a specified index.default TomlTablegetTable(int index) Get a table at a specified index.default @Nullable TomlPositioninputPositionOf(int index) Get the position where a value is defined in the TOML document.default booleanisArray(int index) trueif the value at an index is an array.default booleanisBoolean(int index) trueif the value at an index is a boolean.default booleanisDouble(int index) trueif the value at an index is a double.booleanisEmpty()trueif the array is empty.default booleanisLocalDate(int index) trueif the value at an index is a local date.default booleanisLocalDateTime(int index) trueif the value at an index is a local date-time.default booleanisLocalTime(int index) trueif the value at an index is a local time.default booleanisLong(int index) trueif the value at an index is a long.default booleanisOffsetDateTime(int index) trueif the value at an index is an offset date-time.default booleanisString(int index) trueif the value at an index is a string.default booleanisTable(int index) trueif the value at an index is a table.intsize()The size of the array.default voidtoJson(Appendable appendable, EnumSet<JsonOptions> options) Append a JSON representation of this array to the appendable output.default voidtoJson(Appendable appendable, JsonOptions... options) Append a JSON representation of this array to the appendable output.default StringtoJson(EnumSet<JsonOptions> options) Return a representation of this array using JSON.default StringtoJson(JsonOptions... options) Return a representation of this array using JSON.toList()Get the elements of this array as aList.default StringtoToml()Return a representation of this array using TOML, written with the default options.default voidtoToml(Appendable appendable) Append a TOML representation of this array to the appendable output, written with the default options.default voidtoToml(Appendable appendable, TomlWriteOptions options) Append a TOML representation of this array to the appendable output.default StringtoToml(TomlWriteOptions options) Return a representation of this array using TOML.
-
Method Details
-
size
int size()The size of the array.- Returns:
- The size of the array.
-
isEmpty
boolean isEmpty()trueif the array is empty.- Returns:
trueif the array is empty.
-
get
Get a value at a specified index.This is a shortcut for
entry(int), returning its value.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
isString
default boolean isString(int index) trueif the value at an index is a string.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a string.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getString
Get a string at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not a string.
-
isLong
default boolean isLong(int index) trueif the value at an index is a long.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a long.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getLong
default long getLong(int index) Get a long at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not a long.
-
isDouble
default boolean isDouble(int index) trueif the value at an index is a double.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a double.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getDouble
default double getDouble(int index) Get a double at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not a double.
-
isBoolean
default boolean isBoolean(int index) trueif the value at an index is a boolean.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a boolean.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getBoolean
default boolean getBoolean(int index) Get a boolean at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not a boolean.
-
isOffsetDateTime
default boolean isOffsetDateTime(int index) trueif the value at an index is an offset date-time.- Parameters:
index- The array index.- Returns:
trueif the value at the index is an offset date-time.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getOffsetDateTime
Get an offset date time at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not anOffsetDateTime.
-
isLocalDateTime
default boolean isLocalDateTime(int index) trueif the value at an index is a local date-time.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a local date-time.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getLocalDateTime
Get a local date time at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not anLocalDateTime.
-
isLocalDate
default boolean isLocalDate(int index) trueif the value at an index is a local date.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a local date.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getLocalDate
Get a local date at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not anLocalDate.
-
isLocalTime
default boolean isLocalTime(int index) trueif the value at an index is a local time.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a local time.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getLocalTime
Get a local time at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not anLocalTime.
-
isArray
default boolean isArray(int index) trueif the value at an index is an array.- Parameters:
index- The array index.- Returns:
trueif the value at the index is an array.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getArray
Get an array at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not an array.
-
isTable
default boolean isTable(int index) trueif the value at an index is a table.- Parameters:
index- The array index.- Returns:
trueif the value at the index is a table.- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getTable
Get a table at a specified index.- Parameters:
index- The array index.- Returns:
- The value.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.TomlInvalidTypeException- If the value is not a table.
-
inputPositionOf
Get the position where a value is defined in the TOML document.This is a shortcut for
entry(int), returning its position.- Parameters:
index- The array index.- Returns:
- The input position, or
nullif the entry was not read from a document. - Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
comments
Get the comments attached to a value.Returns the comments in document order: the run directly above the value, if any, then the comment on its line, if any, so at most two, each with its
TomlComment.placement().In an array of tables, the comments on each
[[x]]header are attached to the table it opens:comments(0)for the first header, and so on.This is a shortcut for
entry(int), returning its comments.- Parameters:
index- The array index.- Returns:
- The attached comments, in document order. Unmodifiable.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
comment
Get the comment attached to a value at a placement.This is a shortcut for
entry(int), returningTomlEntry.comment(TomlComment.Placement).- Parameters:
index- The array index.placement-TomlComment.Placement.ABOVEfor the run directly above the value, orTomlComment.Placement.AFTERfor the comment on its line.- Returns:
- The comment at that placement, or
nullif there is none. - Throws:
IndexOutOfBoundsException- If the index is out of bounds.NullPointerException- Ifplacementisnull.IllegalArgumentException- IfplacementisTomlComment.Placement.UNATTACHED.
-
entry
Get the entry at an index.The entry is the
TomlEntrythatelements()holds for the index.get(int),inputPositionOf(int)andcomments(int)are shortcuts reading its value, position and comments.- Parameters:
index- The array index.- Returns:
- The entry.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
elements
List<TomlElement> elements()Get the elements written in this array, in document order.Each element is a
TomlEntry, an entry of this array, or an unattachedTomlComment. The entries hold the valuesget(int)returns, in the same order. A comment is unattached when it is neither directly above a value nor on its line: a run separated by a blank line from the value below it, a run before the closing bracket, or a comment on a line with no value.- Returns:
- The elements, in document order. Unmodifiable.
-
toList
Get the elements of this array as aList.Note that this does not do a deep conversion. If this array contains tables or arrays, they will be of type
TomlTableorTomlArrayrespectively.- Returns:
- The elements of this array as a
List.
-
toJson
Return a representation of this array using JSON.- Parameters:
options- Options for the JSON encoder.- Returns:
- A JSON representation of this table.
-
toJson
Return a representation of this array using JSON.- Parameters:
options- Options for the JSON encoder.- Returns:
- A JSON representation of this table.
-
toJson
Append a JSON representation of this array to the appendable output.- Parameters:
appendable- The appendable output.options- Options for the JSON encoder.- Throws:
IOException- If an IO error occurs.
-
toJson
Append a JSON representation of this array to the appendable output.- Parameters:
appendable- The appendable output.options- Options for the JSON encoder.- Throws:
IOException- If an IO error occurs.
-
toToml
Return a representation of this array using TOML, written with the default options.An array is always written in the default style, whether or not it was parsed, keeping the literal form each of its values was parsed with.
- Returns:
- A TOML representation of this array.
- See Also:
-
toToml
Return a representation of this array using TOML.An array is always written in the default style, whether or not it was parsed; see
toToml().- Parameters:
options- The options to write with.- Returns:
- A TOML representation of this array.
- Throws:
IllegalArgumentException- If the version the options write for cannot write this array: TOML 1.0.0 and an inline table holding a comment, or text copied from a document that only TOML 1.1.0 allows; seeTomlWriteOptions.withVersion(TomlVersion).- See Also:
-
toToml
Append a TOML representation of this array to the appendable output, written with the default options.Written as
toToml()writes it.- Parameters:
appendable- The appendable output.- Throws:
IOException- If an IO error occurs.- See Also:
-
toToml
Append a TOML representation of this array to the appendable output.Written as
toToml(TomlWriteOptions)writes it.- Parameters:
appendable- The appendable output.options- The options to write with.- Throws:
IOException- If an IO error occurs.IllegalArgumentException- If the version the options write for cannot write this array: TOML 1.0.0 and an inline table holding a comment, or text copied from a document that only TOML 1.1.0 allows; seeTomlWriteOptions.withVersion(TomlVersion).- See Also:
-
as
Bind this array to a Java type, with the default options.- Type Parameters:
T- The type to bind to.- Parameters:
type- The type to bind to.- Returns:
- A new instance of the type, holding the values of this array.
- Throws:
TomlBindException- If any value of this array cannot be bound.IllegalArgumentException- If the type, or a type it holds, cannot be bound to.- See Also:
-
as
Bind this array to a Java type.record Server(String host, int port) {} Server[] servers = result.getArray("servers").as(Server[].class, TomlBindOptions.defaults());The value of each key or element is bound to the type declared for it, as follows:
- A string to
String, an enum constant, orcharfor a string of one character. An enum constant is matched by its name, or else by its name ignoring case and with-and space read as_; a constant with aTomlNameannotation is matched by the annotation's value only. - An integer to
long,int,short,byte,BigInteger, or to a floating point type if it can be represented exactly. A value out of range for the type is an error. - A float to
double,floatorBigDecimal. - A boolean to
boolean. - An offset date-time to
OffsetDateTime,ZonedDateTimeorInstant, and a local date-time, date or time toLocalDateTime,LocalDateorLocalTime. - An array to a
List,Set,Collection,Iterable, a concrete collection class, or a Java array. - A table to a record, a class, or a
MapwithStringkeys. - Any value to
Object, unchanged, and a table or array toTomlTableorTomlArray, or to a copy of it forMutableTomlTableorMutableTomlArray. - Any value to
Optional<T>, by binding it toT. - Any value to a type that has a converter in the options, by that converter.
A table is bound to a record through its canonical constructor, with a key for each component. It is bound to a class by creating an instance with its constructor without parameters, then setting a field for each key. Every field of the class and its superclasses is bound except static, transient and final fields. The key of a field or component is its name, converted by the options' key naming, or the value of its
TomlNameannotation.A key the table does not have is bound as follows:
- An
Optionalis empty. - A field of a class keeps the value it was given when the instance was created.
- A record component, or a field of a class that is
nullonce the instance is created, isnullif it is nullable, and an error if it is nevernull. A primitive is nevernull; otherwise, an annotation namedNonNull,NotNullorNonnullmarks a field or component as nevernull, and one namedNullablemarks it as nullable, from any package, as long as the annotation is kept at runtime. Without either, it is nevernullif its class, an enclosing class, its package or its module is annotated as JSpecify'sNullMarked, and nullable otherwise.
Binding does not stop at the first error. Every value is bound, and the errors, each with the path and position of the value, are thrown together in a
TomlBindException. An exception thrown by a record's constructor or a converter is reported as an error at the position of the value being bound.TomlJ binds to private fields and classes by reflection. In a named module, the package holding them must be opened to the module
org.tomlj.- Type Parameters:
T- The type to bind to.- Parameters:
type- The type to bind to.options- The options to bind with.- Returns:
- A new instance of the type, holding the values of this array.
- Throws:
TomlBindException- If any value of this array cannot be bound.IllegalArgumentException- If the type, or a type it holds, cannot be bound to.
- A string to
-
as
Bind this array to a generic Java type, with the default options.- Type Parameters:
T- The type to bind to.- Parameters:
type- The type to bind to.- Returns:
- A new instance of the type, holding the values of this array.
- Throws:
TomlBindException- If any value of this array cannot be bound.IllegalArgumentException- If the type, or a type it holds, cannot be bound to.- See Also:
-
as
Bind this array to a generic Java type.This binds as
as(Class, TomlBindOptions)does, to a type such asList<Server>that aClasscannot name.- Type Parameters:
T- The type to bind to.- Parameters:
type- The type to bind to.options- The options to bind with.- Returns:
- A new instance of the type, holding the values of this array.
- Throws:
TomlBindException- If any value of this array cannot be bound.IllegalArgumentException- If the type, or a type it holds, cannot be bound to.
-