Example usage for org.jfree.data KeyedValue getKey

List of usage examples for org.jfree.data KeyedValue getKey

Introduction

In this page you can find the example usage for org.jfree.data KeyedValue getKey.

Prototype

public Comparable getKey();

Source Link

Document

Returns the key associated with the value.

Usage

From source file:org.jfree.data.KeyedValueComparator.java

/**
 * Compares two {@link KeyedValue} instances and returns an
 * <code>int</code> that indicates the relative order of the two objects.
 *
 * @param o1  object 1.// ww w.j a  v a2s.c  om
 * @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;
}