/**
   * Compared the value represented by element and the one of row number <code>pos</code>. <code>
   * elements</code> will be converted to a compatible type to this column. if element > pos returns
   * 1. if element < pos retruns -1. if the are equal returns 0. if one of the representation does
   * not hold a value, it is considered smaller than the other.
   */
  public int compareRows(Object element, int pos) {

    // validating the objects to be compared
    int val = validate(element, pos);
    if (val <= 1) {
      return val;
    }

    // retrieving the object from pos
    Object obj = getObject(pos);

    // if they are of the smae type - comparing them
    if (element.getClass().equals(obj.getClass())) {
      return compareObjects(element, obj);
    }

    // if both are numbers - comparing them as doubles.
    if (element instanceof Number && obj instanceof Number) {
      return SparseDoubleColumn.compareDoubles(
          SparseDoubleColumn.toDouble(element), SparseDoubleColumn.toDouble(obj));
    }

    // they are probably not mutually comparable...
    // if this is an object column, i.e. the type of the objects is unknown
    // converting both of the objects into strings and comparing them
    if (ColumnTypes.OBJECT == getType()) {
      return SparseStringColumn.toStringObject(element)
          .compareTo(SparseStringColumn.toStringObject(obj));
    }

    // this is not an object column - converting element to the type of this
    // column, and comparing the objects.
    switch (getType()) {
      case (ColumnTypes.BYTE_ARRAY):
        element = SparseByteArrayColumn.toByteArray(element);
        break;
      case (ColumnTypes.CHAR_ARRAY):
        element = SparseCharArrayColumn.toCharArray(element);
        break;
      case (ColumnTypes.STRING):
        element = SparseStringColumn.toStringObject(element);
        break;
      default:
        break;
    }

    return compareObjects(element, obj);
  }
  /**
   * Compares 2 values that are in this column. Retruns an int representation of the relation
   * between the values.
   *
   * @param pos1 - the row number of the first value to be compared
   * @param pos2 - the row number of the second value to be compared
   * @return int - representing the relation between the values at row # <code>pos1</code> and row #
   *     <code>pos2</code>. if pos1's value > pos2' value returns 1. if pos1's value < pos2' value
   *     returns -1. returns 0 if they are equal.
   */
  public int compareRows(int pos1, int pos2) {
    // validating the objects in the given rows.
    int val = validate(pos1, pos2);
    if (val <= 1) {
      return val;
    }

    Object obj1 = getObject(pos1);
    Object obj2 = getObject(pos2);

    // if both are of the same class - comparing them
    if (obj1.getClass().equals(obj2.getClass())) {
      return compareObjects(obj1, obj2);
    }

    // if all the elements in this column are numeric - converting
    // the objects to doubles and comparing them.
    if (isNumeric()) {
      return SparseDoubleColumn.compareDoubles(getDouble(pos1), getDouble(pos2));
    }

    // if not all of the elements are numeric - coverting the objects to strings
    // and comparing them.
    return SparseStringColumn.toStringObject(obj1)
        .compareTo(SparseStringColumn.toStringObject(obj2));
  }
  /**
   * If the item at pos is a Number, return its double value. Otherwise if the item is a char[] or
   * any other type of Object, convert the item to a String and return its double value by calling
   * Double.parseDouble()
   *
   * @param row the row number
   * @return the double value of the item at row # row. if no such item exists returns a value
   *     signifying the position is empty, as defined by SparseDoubleColumn.
   */
  public double getDouble(int row) {
    Object obj = elements.get(row);
    if (obj != null && isDataNumeric(row)) {
      return SparseDoubleColumn.toDouble(obj);
    }

    return SparseDefaultValues.getDefaultDouble();
  }