/* * Licensed to ElasticSearch and Shay Banon under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. ElasticSearch licenses this * file to you 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 org.elasticsearch.common; import com.google.common.collect.ImmutableMap; import org.elasticsearch.ElasticSearchIllegalArgumentException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** */ public class Table { private List headers = new ArrayList(); private List> rows = new ArrayList>(); private List currentCells; private boolean inHeaders = false; public Table startHeaders() { inHeaders = true; currentCells = new ArrayList(); return this; } public Table endHeaders() { inHeaders = false; headers = currentCells; currentCells = null; return this; } public Table startRow() { if (headers.isEmpty()) { throw new ElasticSearchIllegalArgumentException("no headers added..."); } currentCells = new ArrayList(headers.size()); return this; } public Table endRow() { if (currentCells.size() != headers.size()) { throw new ElasticSearchIllegalArgumentException("mismatch on number of cells in a row compared to header"); } rows.add(currentCells); currentCells = null; return this; } public Table addCell(Object value) { return addCell(value, ""); } public Table addCell(Object value, String attributes) { if (!inHeaders) { if (currentCells.size() == headers.size()) { throw new ElasticSearchIllegalArgumentException("can't add more cells to a row than the header"); } } Map mAttr; if (attributes.length() == 0) { if (inHeaders) { mAttr = ImmutableMap.of(); } else { // get the attributes of the header cell we are going to add to mAttr = headers.get(currentCells.size()).attr; } } else { mAttr = new HashMap(); if (!inHeaders) { // get the attributes of the header cell we are going to add mAttr.putAll(headers.get(currentCells.size()).attr); } String[] sAttrs = Strings.split(attributes, ";"); for (String sAttr : sAttrs) { if (sAttr.length() == 0) { continue; } int idx = sAttr.indexOf('='); mAttr.put(sAttr.substring(0, idx), sAttr.substring(idx + 1)); } } currentCells.add(new Cell(value, mAttr)); return this; } public List getHeaders() { return this.headers; } public Iterable> getRows() { return rows; } public static class Cell { public final Object value; public final Map attr; Cell(Object value, Map attr) { this.value = value; this.attr = attr; } } }