![JAR search and dependency download from the Maven repository](/logo.png)
com.tectonica.jonix.tabulate.Tabulation Maven / Gradle / Ivy
/*
* Copyright (C) 2012-2024 Zach Melamed
*
* Latest version available online at https://github.com/zach-m/jonix
* Contact me at [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tectonica.jonix.tabulate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* This class contains a collection of {@link FieldTabulator}s which together hold all the information needed to flatten
* an ONIX Product (which is a tree-like structure by its nature) into a flat-list of values (containing one or more
* logical fields). It also provides a header (i.e. list of column-names) for each of these values, in case the context
* requires a header (e.g. when writing to a file).
*
* While users are likely to define their own {@link FieldTabulator}s and {@link Tabulation}s, Jonix offers a ready-made
* set for your convenience, as part of the Unification
services.
*
* @see FieldTabulator
* @see com.tectonica.jonix.unify.BaseTabulation
*/
public class Tabulation
{
private List> tabulators = new ArrayList<>();
private List header = new ArrayList<>();
public static Tabulation
create() {
return new Tabulation<>();
}
public Tabulation
add(FieldTabulator
tabulator) {
tabulators.add(tabulator);
header.addAll(tabulator.header());
return this;
}
public List header() {
return header;
}
public List row(P product) {
List row = new ArrayList<>(Collections.nCopies(header.size(), null));
int from = 0;
for (FieldTabulator tabulator : tabulators) {
int to = from + tabulator.header().size();
tabulator.rowSupplier().setRowFromProduct(row.subList(from, to), product);
from = to;
}
return row;
}
}