Implemented Table#preload

Implemented Table#preload
Removed Table#initialize() call from within Table#initialize(ParserInput)
This commit is contained in:
Collin Smith 2020-12-27 20:01:47 -08:00
parent 138c62c225
commit cc807f60fa
2 changed files with 13 additions and 5 deletions

View File

@ -42,6 +42,7 @@ public class Tables {
T loadTsv(T table, TsvParser parser) {
parser.primaryKey(table.primaryKey());
table.initialize(parser);
table.initialize();
return table;
}

View File

@ -97,21 +97,28 @@ public abstract class Table<R> implements Iterable<R> {
}
/**
* Initializes this table for loading records via a
* {@link ParserInput parser}.
* Initializes this table for loading records via a {@link ParserInput parser}.
*/
protected final void initialize(ParserInput in) {
if (parser != null) throw new IllegalStateException("parser already set");
this.parser = newParser(in).parseFields();
initialize();
if (preload()) {
for (int i = 0, s = in.numRecords(); i < s; i++) {
final R record = newRecord();
parser.parseRecord(i, record);
inject(record);
put(i, record);
}
}
}
public R get(int id) {
R record = records.get(id);
if (record == null && parser != null) {
if (record == null && !preload() && parser != null) {
if (id < 0 || id >= parser.parser().numRecords()) return null;
record = parser.parseRecord(id, newRecord());
record = inject(record);
records.put(id, record);
put(id, record);
}
return record;