/*
 * Created on Feb 7, 2004
 *
 */
package LumiSoft.Data;

import java.util.*;

/**
 * 
 */
public class DataColumnCollection implements Iterable<DataColumn>
{
	private DataTable                    m_pOwner         = null;
	private ArrayList<DataColumn>        m_pColumns       = null;
	private Hashtable<String,DataColumn> m_pColumnsByName = null;
	
	/**
	 * Default constructor.
	 * @param owner
	 */
	protected DataColumnCollection(DataTable owner)
	{
		m_pOwner         = owner;
		m_pColumns       = new ArrayList<DataColumn>();
		m_pColumnsByName = new Hashtable<String,DataColumn>();
	}
	
	
	/**
	 * Creates and adds new data column.
	 * @param columnName
	 */
	public DataColumn Add(String columnName)
	{
		return Add(columnName,String.class);
	}
	/**
	 * Creates and adds new data column.
	 * @param columnName
	 * @param dataType
	 */
	public DataColumn Add(String columnName,Class dataType)
	{						
		DataColumn column = new DataColumn(m_pOwner,columnName,m_pColumns.size(),dataType);
		m_pColumns.add(column);
		m_pColumnsByName.put(columnName.toLowerCase(),column);
		
		return column;
	}
	
	/**
	 * Removes specified data column.
	 * @param column
	 */
	public void Remove(DataColumn column)
	{
		// TODO: Don't allow change columns, if there is data in table
		
		m_pColumnsByName.remove(column);
		m_pColumns.remove(column);		
	}
	/**
	 * Removes the column at the specified index from the collection.
	 * @param index
	 */
	public void Remove(int index)
	{
		// TODO: Don't allow change columns, if there is data in table
		
		m_pColumnsByName.remove(m_pColumns.indexOf(index));
		m_pColumns.remove(index);		
	}
	
	/**
	 * Clears the collection of any columns.
	 *
	 */
	public void Clear()
	{
		// TODO: Don't allow change columns, if there is data in table
		
		m_pColumns.clear();
		m_pColumnsByName.clear();
	}
	
	/**
	 * Checks whether the collection contains a column with the specified name.
	 * @param columnName
	 * @return
	 */
	public boolean Contains(String columnName)	
	{
		return m_pColumnsByName.containsKey(columnName.toLowerCase());
	/*	for(DataColumn dc : m_pColumns){
			if(dc.getColumnName().toLowerCase().equals(columnName.toLowerCase())){
				return true;
			}
		}
			
		return false;*/
	}
	
	
	/**
	 * Gets the DataColumn from the collection with the specified name.
	 * @param columnName
	 * @return
	 */
	public DataColumn get(String columnName)
	{
		return m_pColumnsByName.get(columnName.toLowerCase());
	/*	for(DataColumn dc : m_pColumns){			
			if(dc.getColumnName().toLowerCase().equals(columnName.toLowerCase())){
				return dc;
			}
		}
		
		return null;*/
	}
	/**
	 * Gets the DataColumn from the collection at the specified index.
	 * @param columnIndex
	 * @return
	 */
	public DataColumn get(int columnIndex)
	{
		return m_pColumns.get(columnIndex);
	}	
	
	/**
	 * Gets the total number of columns in a collection.
	 * @return
	 */
	public int getCount()
	{
		return m_pColumns.size();
	}
	
	
	/**
	 * Gets iterator.
	 */
	public Iterator<DataColumn> iterator()
	{
		return m_pColumns.iterator();
	}
	
}
