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

at.spardat.xma.datasource.TabularDataRow Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: TabularDataRow.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.datasource;

import java.util.ArrayList;

import at.spardat.xma.mdl.Atom;

/**
 * A TabularDataRow stores the data for a row of a 
 * TabularData.
 */
public class TabularDataRow {

    TabularData tabularData;
    ArrayList data;

    /**
     * Creates a TabularDataRow for a TabularData.
     * 
     * @param aTabularData - The TabularData for which the row is 
     * created.
     */
    public TabularDataRow(TabularData aTabularData) {

        tabularData = aTabularData;
    }

    /**
     * Adds a column and sets the value for this column.
     * 
     * @param value - The value for the column.
     */
    public void add(Atom value) {

        if (value == null) {
            throw new IllegalArgumentException("Null not allowed.");
        }

        if (data == null) {
            data = new ArrayList();
        }

        if (data.size() >= tabularData.header.size()) {
            throw new IndexOutOfBoundsException("Column does not exist.");
        }

        data.add(value);
    }

    /**
     * Fill empty columns of this row with values. All columns which have
     * already values are not modified. These columns filled are with Atoms 
     * of the type "Types.T_STRING" and the values are empty strings.
     *
     */
    private void expandToFullSize() {

        if (data == null) {
            data = new ArrayList();
        }

        for (int col = data.size(); col < tabularData.numCols(); col++) {
            data.add (Atom.EMPTY_STRING);
        }
    }

    /**
     * Sets the value for the column with a given index.
     * 
     * @param col - The index of the column.
     * @param value - The value to be set.
     */
    public void set(int col, Atom value) {

        if (value == null) {
            throw new IllegalArgumentException("Null not allowed.");
        }

        if (data == null || data.size() <= col) {
            expandToFullSize();
        }

        data.set(col, value);
    }

    /**
     * Sets the value for a column with a given name.
     * 
     * @param colName - The name of the column.
     * @param value - The value for the column.
     */
    public void set(String colName, Atom value) {
        
        int col = tabularData.getColumnIndex(colName);
        set(col, value);
    }

    /**
     * Returns the value of this row for the column with a given column-index.
     * 
     * @param col - The index of the column. 
     * @return non null Atom.
     * @exception IllegalArgumentException if col greater than
     *             or equal to the number of columns in the table.
     */
    public Atom get (int col) {
        if (col < 0) throw new IllegalArgumentException();
        if (col < data.size()) return (Atom) data.get(col);
        if (col < tabularData.numCols()) return Atom.EMPTY_STRING;
        throw new IllegalArgumentException();
    }

    /**
     * Returns the value for a column with a given name.
     * 
     * @param colName - The name of the column.
     */
    public Atom get(String colName) {

        int col = tabularData.getColumnIndex(colName);
        return get(col);
    }

    /**
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        if (data == null) return 5;
        return data.hashCode();
    }

    /**
     * Returns the number of Atoms in this.
     */    
    public int size () {
        return data == null ? 0 : data.size(); 
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy