
package omaTable;

import java.util.*;

public class TableData
{

    private Vector data;
    private Hashtable keyMap;
    private int columnCount;
    private long lastChange;

    public synchronized void removeAll()
    {
        keyMap.clear();
        data.removeAllElements();
        lastChange = System.currentTimeMillis();
    }

    public boolean hasChangedSince(long l)
    {
        return lastChange >= l;
    }

    public synchronized void removeRow(int i)
    {
        data.removeElementAt(i);
        for(Enumeration enumeration = keyMap.keys(); enumeration.hasMoreElements();)
        {
            Object obj = enumeration.nextElement();
            if(((Integer)keyMap.get(obj)).intValue() == i)
                keyMap.remove(obj);
        }

        lastChange = System.currentTimeMillis();
    }

    public synchronized int getRowCount()
    {
        return data.size();
    }

    public TableData(int i)
    {
        keyMap = new Hashtable();
        data = new Vector();
        columnCount = i;
    }

    synchronized void setColumnCount(int i)
    {
        i = Math.max(1, i);
        int j = data.size();
        for(int k = 0; k < j; k++)
        {
            Object aobj[] = (Object[])data.elementAt(k);
            Object aobj1[] = new Object[i];
            System.arraycopy(((Object) (aobj)), 0, ((Object) (aobj1)), 0, Math.min(aobj.length, aobj1.length));
            data.setElementAt(((Object) (aobj1)), k);
        }

        columnCount = i;
    }

    public synchronized int getColumnCount()
    {
        return columnCount;
    }

    public synchronized int getRowIndex(Object obj)
    {
        if(obj != null)
        {
            Integer integer = (Integer)keyMap.get(obj);
            if(integer != null)
                return integer.intValue();
        }
        return -1;
    }

    public synchronized void setCell(int i, int j, Object obj)
    {
        if(j < 0 || j >= columnCount)
            throw new IllegalArgumentException("invalid column: " + j);
        if(i < 0 || i >= data.size())
            throw new IllegalArgumentException("invalid row: " + i);
        if(obj instanceof String)
        {
            Object aobj[] = (Object[])data.elementAt(i);
            aobj[j] = obj;
            lastChange = System.currentTimeMillis();
        } else
        {
            throw new ClassCastException("value is not a String");
        }
    }

    public synchronized Object getCell(int i, int j)
    {
        if(j < 0 || j >= columnCount)
            throw new IllegalArgumentException("invalid column: " + j);
        if(i < 0 || i >= data.size())
            throw new IllegalArgumentException("invalid row: " + i);
        Object aobj[] = (Object[])data.elementAt(i);
        if(aobj != null && j < aobj.length)
            return aobj[j];
        else
            return null;
    }

    public synchronized int setRow(int i, Object aobj[])
    {
        return setRow(i, aobj[0], aobj);
    }

    public synchronized Object[] getRow(int i)
    {
        return (Object[])data.elementAt(i);
    }

    public synchronized int setRow(int i, Object obj, Object aobj[])
    {
        if((aobj instanceof String[]) || aobj == null)
        {
            int j = data.size();
            i = Math.min(i, j);
            if(i < 0 || i == j)
            {
                data.addElement(((Object) (aobj)));
                i = j;
            } else
            {
                data.setElementAt(((Object) (aobj)), i);
            }
            if(obj != null)
                keyMap.put(obj, new Integer(i));
            lastChange = System.currentTimeMillis();
            return i;
        } else
        {
            throw new ClassCastException("fields not String[]");
        }
    }
}
