package ModSQL;
import java.io.Serializable;

/* $Id: IndexTableEntry.java,v 1.7 2004/01/04 01:59:39 cvs Exp $
 *
 * Copyright (c) 2004 Chris Studholme <chris.studholme@utoronto.ca>
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See COPYING for more information.
 */

/**
 * Column data object and table rowset combined.
 *
 * @author chris.studholme@utoronto.ca
 */
class IndexTableEntry implements Comparable, Serializable {
  /** Column value. */
  public Object data;
  /** Rows that have the specified column value. */
  public IndexTableRowset rows;

  /**
   * Constructor.
   *
   * @param data column value
   */
  public IndexTableEntry(Object data) {
    this.data=data;
    this.rows=null;
  }
  /**
   * Constructor.
   *
   * @param data column value
   * @param rows rowset
   */
  public IndexTableEntry(Object data, IndexTableRowset rows) {
    this.data=data;
    this.rows=rows;
  }
  /**
   * Constructor.
   *
   * @param data column value
   * @param rowid used to create a new rowset
   * @param count used to create a new rowset
   */
  public IndexTableEntry(Object data, Object rowid, long count) {
    this.data=data;
    rows = new IndexTableRowset(rowid,count);
  }

  /**
   * Finish the rowset.  
   *
   * @see IndexTableRowset#finish
   */
  public void finish() {
    rows.finish();
  }

  /**
   * Check if the other IndexTableEntry has the same column value.
   *
   * @param o other entry
   * @return true if both entries have the same column value
   */
  public boolean sameData(IndexTableEntry o) {
    if (data==null || o.data==null)
      return data==o.data;
    return data.equals(o.data);
  }

  /**
   * Test if the column value is equal to another column value or the
   * column value of another IndexTableEntry.
   *
   * @param o other column value or other IndexTableEntry
   * @return true if the values are equal
   */
  public boolean equals(Object o) {
    if (o instanceof IndexTableEntry)
      o = ((IndexTableEntry)o).data;
    if (data==null || o==null)
      return data==o;
    return data.equals(o);
  }

  /**
   * Compare the column value to another column value or to the
   * column value of another IndexTableEntry.  The column value must
   * be Comparable (or null).
   *
   * @param o other column value or other IndexTableEntry
   * @return 0 if equal, -1 or +1 to indicate relative order
   */
  public int compareTo(Object o) {
    if (o instanceof IndexTableEntry)
      o = ((IndexTableEntry)o).data;
    if (data==null || o==null) {
      if (data==o) return 0;
      return data==null ? 1 : -1;
    }
    return ((Comparable)data).compareTo(o);
  }
};
