Ejemplo n.º 1
0
  /**
   * Compares two {@link KeyedValue} instances and returns an <code>int</code> that indicates the
   * relative order of the two objects.
   *
   * @param o1 object 1.
   * @param o2 object 2.
   * @return An int indicating the relative order of the objects.
   */
  @Override
  public int compare(Object o1, Object o2) {

    if (o2 == null) {
      return -1;
    }
    if (o1 == null) {
      return 1;
    }

    int result;

    KeyedValue kv1 = (KeyedValue) o1;
    KeyedValue kv2 = (KeyedValue) o2;

    if (this.type == KeyedValueComparatorType.BY_KEY) {
      if (this.order.equals(SortOrder.ASCENDING)) {
        result = kv1.getKey().compareTo(kv2.getKey());
      } else if (this.order.equals(SortOrder.DESCENDING)) {
        result = kv2.getKey().compareTo(kv1.getKey());
      } else {
        throw new IllegalArgumentException("Unrecognised sort order.");
      }
    } else if (this.type == KeyedValueComparatorType.BY_VALUE) {
      Number n1 = kv1.getValue();
      Number n2 = kv2.getValue();
      if (n2 == null) {
        return -1;
      }
      if (n1 == null) {
        return 1;
      }
      double d1 = n1.doubleValue();
      double d2 = n2.doubleValue();
      if (this.order.equals(SortOrder.ASCENDING)) {
        if (d1 > d2) {
          result = 1;
        } else if (d1 < d2) {
          result = -1;
        } else {
          result = 0;
        }
      } else if (this.order.equals(SortOrder.DESCENDING)) {
        if (d1 > d2) {
          result = -1;
        } else if (d1 < d2) {
          result = 1;
        } else {
          result = 0;
        }
      } else {
        throw new IllegalArgumentException("Unrecognised sort order.");
      }
    } else {
      throw new IllegalArgumentException("Unrecognised type.");
    }

    return result;
  }
Ejemplo n.º 2
0
 /**
  * @param a_index the index of the item to return the value for
  * @return the value at given index
  * @author Klaus Meffert
  * @since 2.3
  */
 public Number getValue(final int a_index) {
   Number result;
   final KeyedValue kval = (KeyedValue) m_data.get(a_index);
   if (kval != null) {
     result = kval.getValue();
   } else {
     result = null;
   }
   return result;
 }