All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.grouplens.lenskit.util.table.TableLayoutBuilder Maven / Gradle / Ivy

There is a newer version: 3.0-T5
Show newest version
/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.util.table;

import org.apache.commons.lang3.builder.Builder;
import org.apache.commons.lang3.exception.CloneFailedException;

import java.util.LinkedHashSet;

/**
 * Construct a layout for a table.
 *
 * @author GroupLens Research
 * @since 0.10
 */
public class TableLayoutBuilder implements Builder, Cloneable {
    private LinkedHashSet columns = new LinkedHashSet();

    /**
     * Construct a new builder that is a copy of an existing layout.
     *
     * @param layout The layout to copy.
     * @return A new builder containing all columns in the layout.
     */
    public static TableLayoutBuilder copy(TableLayout layout) {
        TableLayoutBuilder bld = new TableLayoutBuilder();
        for (String col: layout.getColumns()) {
            bld.addColumn(col);
        }
        return bld;
    }

    /**
     * Add a column to the table layout.  Each column must have a unique, non-null
     * name.
     *
     * @param name The column name.
     * @return The builder, for chaining.
     * @throws IllegalArgumentException if the column already exists.
     */
    public TableLayoutBuilder addColumn(String name) {
        if (name == null) {
            throw new NullPointerException("column name");
        }
        if (columns.contains(name)) {
            throw new IllegalArgumentException("column " + name + " already exists");
        }
        columns.add(name);
        return this;
    }

    /**
     * Get the number of columns currently in the layout.
     *
     * @return The number of columns in the layout.
     */
    public int getColumnCount() {
        return columns.size();
    }

    /**
     * Clone this layout command. Used to build multiple layouts from the same initial
     * columns.
     *
     * @return An independent copy of this table layout command.
     */
    @Override
    public TableLayoutBuilder clone() {
        TableLayoutBuilder copy = null;
        try {
            copy = (TableLayoutBuilder) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new CloneFailedException(e);
        }
        copy.columns = new LinkedHashSet(columns);
        return copy;
    }

    @Override
    public TableLayout build() {
        return new TableLayout(columns);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy