package dev.plex.toml; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public abstract class Container { abstract boolean accepts(String key); abstract void put(String key, Object value); abstract Object get(String key); abstract boolean isImplicit(); static class Table extends Container { private final Map values = new HashMap(); final String name; final boolean implicit; Table() { this(null, false); } public Table(String name) { this(name, false); } public Table(String tableName, boolean implicit) { this.name = tableName; this.implicit = implicit; } @Override boolean accepts(String key) { return !values.containsKey(key) || values.get(key) instanceof TableArray; } @Override void put(String key, Object value) { values.put(key, value); } @Override Object get(String key) { return values.get(key); } boolean isImplicit() { return implicit; } /** * This modifies the Table's internal data structure, such that it is no longer usable. *

* Therefore, this method must only be called when all data has been gathered. * * @return A Map-and-List-based of the TOML data */ Map consume() { for (Map.Entry entry : values.entrySet()) { if (entry.getValue() instanceof Table) { entry.setValue(((Table) entry.getValue()).consume()); } else if (entry.getValue() instanceof TableArray) { entry.setValue(((TableArray) entry.getValue()).getValues()); } } return values; } @Override public String toString() { return values.toString(); } } static class TableArray extends Container { private final List values = new ArrayList
(); TableArray() { values.add(new Table()); } @Override boolean accepts(String key) { return getCurrent().accepts(key); } @Override void put(String key, Object value) { values.add((Table) value); } @Override Object get(String key) { throw new UnsupportedOperationException(); } boolean isImplicit() { return false; } List> getValues() { ArrayList> unwrappedValues = new ArrayList>(); for (Table table : values) { unwrappedValues.add(table.consume()); } return unwrappedValues; } Table getCurrent() { return values.get(values.size() - 1); } @Override public String toString() { return values.toString(); } } private Container() { } }