/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2003-2006, Geotools Project Managment Committee (PMC)
 *
 *    This library 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 library 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
 *    Lesser General Public License for more details.
 */
package org.geotools.data.vpf.io;

import java.util.Map;

import org.geotools.data.vpf.ifc.VPFRow;


/**
 * TableRow.java Created: Thu Jan 02 23:58:39 2003
 *
 * @author <a href="mailto:kobit@users.fs.net">Artur Hefczyc</a>
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/vpf/src/main/java/org/geotools/data/vpf/io/TableRow.java $
 * @version 1.0
 */
public class TableRow implements VPFRow {
    /**
     * Describe variable <code>fieldsArr</code> here.
     *
     */
    private RowField[] fieldsArr = null;

    /**
     * Describe variable <code>fieldsMap</code> here.
     *
     */
    private Map fieldsMap = null;

    /**
     * Creates a new <code>TableRow</code> instance.
     *
     * @param fieldsArr a <code>RowField[]</code> value
     * @param fieldsMap a <code>HashMap</code> value
     */
    public TableRow(RowField[] fieldsArr, Map fieldsMap) {
        this.fieldsArr = fieldsArr;
        this.fieldsMap = fieldsMap;
    }

    // TableRow constructor

    /**
     * Describe <code>toString</code> method here.
     *
     * @return a <code>String</code> value
     */
    public String toString() {
        //     StringBuffer buff = new StringBuffer(" ["+getClass().getName());
        //     buff.append(" (fieldsMap=");
        //     if (fieldsMap == null)
        //     {
        //       buff.append("null)");
        //     } // end of if (columnDefs == null)
        //     else
        //     {
        //       Iterator it = fieldsMap.entrySet().iterator();
        //       while (it.hasNext())
        //       {
        //         Map.Entry entry = (Map.Entry)it.next();
        //         buff.append("\n"+
        //                     entry.getKey().toString()+"="+
        //                     entry.getValue().toString());
        //       } // end of while (it.hasNext())
        //       buff.append("\n)");
        //     } // end of if (columnDefs == null) else
        //     buff.append("]");
        StringBuffer buff = new StringBuffer();

        if (fieldsMap == null) {
            buff.append("null)");
        } else {
            for (int i = 0; i < fieldsArr.length; i++) {
                buff.append(fieldsArr[i].toString() + ":");
            }

            buff.append(";");
        }

        return buff.toString();
    }

    /**
     * Describe <code>fieldsCount</code> method here.
     *
     * @return an <code>int</code> value
     */
    public int fieldsCount() {
        return fieldsArr.length;
    }

    /**
     * Describe <code>get</code> method here.
     *
     * @param name a <code>String</code> value
     * @return a <code>RowField</code> value
     */
    public RowField get(String name) {
        return (RowField) fieldsMap.get(name);
    }

    /**
     * Describe <code>get</code> method here.
     *
     * @param idx an <code>int</code> value
     * @return a <code>RowField</code> value
     */
    public RowField get(int idx) {
        return fieldsArr[idx];
    }

    /**
     * Describe <code>equals</code> method here.
     *
     * @param obj an <code>Object</code> value
     * @return a <code>boolean</code> value
     */
    public boolean equals(Object obj) {
        if ((obj == null) || !(obj instanceof TableRow)) {
            return false;
        }

        TableRow row = (TableRow) obj;

        if ((fieldsArr == null) && (row.fieldsArr == null)) {
            return true;
        }

        if ((fieldsArr == null) || (row.fieldsArr == null)) {
            return false;
        }

        if (fieldsArr.length != row.fieldsArr.length) {
            return false;
        }

        for (int i = 0; i < fieldsArr.length; i++) {
            if (!fieldsArr[i].equals(row.fieldsArr[i])) {
                return false;
            }
        }

        return true;
    }

    public int hashCode() {
        int code = 0;

        if ((fieldsArr == null) || (fieldsArr.length == 0)) {
            code = super.hashCode();
        } else {
            for (int i = 0; i < fieldsArr.length; i++) {
                code += fieldsArr[i].hashCode();
            }
        }

        return code;
    }
}
