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

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

/*******************************************************************************
 * 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: TabularDataHeader.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.datasource;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A TabularDataHeader stores header information for a 
 * TabularData. It maps column-names to column-indexes.
 */
public class TabularDataHeader {

    /**
     * Holds the names of the columns as Strings.
     */
    private ArrayList           columns = new ArrayList();
    
    /**
     * Maps from column names (Strings) to column indexes (Integers).
     */
    private HashMap             columnsMap = new HashMap();
    
    

    /**
     * Returns the number of columns.
     * 
     * @see at.spardat.xma.datasource.ITabularData#size()
     */
    public int size() {
        return columns.size();
    }

    /**
     * Adds a column.
     * 
     * @param name the name of the columne to be added
     * @exception IllegalArgumentException if the name is not valid or a column with that name already exists.
     */
    public void addColumn (String name) {
        if (name == null || name.length() == 0) throw new IllegalArgumentException();

        if (columnsMap.get(name) != null) throw new IllegalArgumentException("duplicate col " + name);
        columns.add(name);
        columnsMap.put(name, new Integer(columns.size()-1));
    }

    /**
     * Returns the index of the column with a given name.
     * 
     * @param colName the name of the column.
     * @return index of the column or -1, if there is no column with that name.
     */
    public int getColumnIndex (String colName) {
        if (colName == null) throw new IllegalArgumentException();
        Integer index = (Integer) columnsMap.get(colName);
        if (index == null) return -1;
        return index.intValue();
    }

    /**
     * Returns the name of a column with a given index.
     * 
     * @param col the index of the column.
     * @return name of the column
     * @exception IllegalArgumentException if the provided index is out of range
     */
    public String getColumnName (int col) {
        if (col < 0 || col >= columns.size()) throw new IllegalArgumentException();
        return (String) columns.get(col);
    }
    

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy