Example usage for org.jfree.chart.util SortOrder ASCENDING

List of usage examples for org.jfree.chart.util SortOrder ASCENDING

Introduction

In this page you can find the example usage for org.jfree.chart.util SortOrder ASCENDING.

Prototype

SortOrder ASCENDING

To view the source code for org.jfree.chart.util SortOrder ASCENDING.

Click Source Link

Document

Ascending order.

Usage

From source file:org.jfree.data.KeyedObjectComparator.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./*from ww w  .  j  a v  a  2 s  .  c  om*/
 * @param o2  object 2.
 *
 * @return An int indicating the relative order of the objects.
 */
public int compare(Object o1, Object o2) {

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

    KeyedObject ko1 = (KeyedObject) o1;
    KeyedObject ko2 = (KeyedObject) o2;

    if (this.type == KeyedObjectComparatorType.BY_KEY) {
        if (this.order.equals(SortOrder.ASCENDING)) {
            return ko1.getKey().compareTo(ko2.getKey());
        } else if (this.order.equals(SortOrder.DESCENDING)) {
            return ko2.getKey().compareTo(ko1.getKey());
        } else {
            throw new IllegalArgumentException("Unrecognised sort order.");
        }
    } else if (this.type == KeyedObjectComparatorType.BY_VALUE) {
        Object n1 = ko1.getObject();
        Object n2 = ko2.getObject();
        Comparable c1 = "FALLBACK";
        if (n1 instanceof Comparable) {
            c1 = (Comparable) n1;
        }
        Comparable c2 = "FALLBACK";
        if (n2 instanceof Comparable) {
            c2 = (Comparable) n2;
        }
        if (n2 == null) {
            return -1;
        }
        if (n1 == null) {
            return 1;
        }
        if (this.order.equals(SortOrder.ASCENDING)) {
            return c1.compareTo(c2);
        } else if (this.order.equals(SortOrder.DESCENDING)) {
            return c2.compareTo(c1);
        } else {
            throw new IllegalArgumentException("Unrecognised sort order.");
        }
    } else {
        throw new IllegalArgumentException("Unrecognised type.");
    }
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTests.java

/**
 * Tests sorting of data by key (ascending).
 *///w  w  w.jav a2s. c  o m
public void testSortByKeyAscending() {
    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByKeys(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "A");
    assertEquals(data.getKey(1), "B");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "D");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(2.0));
    assertEquals(data.getValue(1), null);
    assertEquals(data.getValue(2), new Double(1.0));
    assertEquals(data.getValue(3), new Double(3.0));
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java

/**
 * Tests sorting of data by key (ascending).
 *//*from  w  w  w. j ava2 s.  c o m*/
public void testSortByKeyAscending() {

    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByKeys(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "A");
    assertEquals(data.getKey(1), "B");
    assertEquals(data.getKey(2), "C");
    assertEquals(data.getKey(3), "D");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(2.0));
    assertEquals(data.getValue(1), null);
    assertEquals(data.getValue(2), new Double(1.0));
    assertEquals(data.getValue(3), new Double(3.0));

}

From source file:org.jfree.data.junit.KeyedObjectsTests.java

/**
 * Tests sorting of data by key (ascending).
 *///from w  w w. ja v a2  s . c  o m
public void testSortByKeyAscending() {
    KeyedObjects data = new KeyedObjects();
    data.addObject("C", new Double(1.0));
    data.addObject("B", null);
    data.addObject("D", new Double(3.0));
    data.addObject("A", new Double(2.0));

    data.sortByKeys(SortOrder.ASCENDING);

    // check key order
    assertEquals("A", data.getKey(0));
    assertEquals("B", data.getKey(1));
    assertEquals("C", data.getKey(2));
    assertEquals("D", data.getKey(3));

    // check retrieve value by key
    assertEquals(data.getObject("A"), new Double(2.0));
    assertEquals(data.getObject("B"), null);
    assertEquals(data.getObject("C"), new Double(1.0));
    assertEquals(data.getObject("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getObject(0), new Double(2.0));
    assertEquals(data.getObject(1), null);
    assertEquals(data.getObject(2), new Double(1.0));
    assertEquals(data.getObject(3), new Double(3.0));
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTests.java

/**
 * Tests sorting of data by value (ascending).
 *///from   ww w . jav  a2s  . co  m
public void testSortByValueAscending() {
    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByValues(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "C");
    assertEquals(data.getKey(1), "A");
    assertEquals(data.getKey(2), "D");
    assertEquals(data.getKey(3), "B");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(1.0));
    assertEquals(data.getValue(1), new Double(2.0));
    assertEquals(data.getValue(2), new Double(3.0));
    assertEquals(data.getValue(3), null);
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java

/**
 * Tests sorting of data by value (ascending).
 *//*from   ww w  . j  av a2  s.com*/
public void testSortByValueAscending() {

    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("C", new Double(1.0));
    data.addValue("B", null);
    data.addValue("D", new Double(3.0));
    data.addValue("A", new Double(2.0));

    data.sortByValues(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "C");
    assertEquals(data.getKey(1), "A");
    assertEquals(data.getKey(2), "D");
    assertEquals(data.getKey(3), "B");

    // check retrieve value by key
    assertEquals(data.getValue("A"), new Double(2.0));
    assertEquals(data.getValue("B"), null);
    assertEquals(data.getValue("C"), new Double(1.0));
    assertEquals(data.getValue("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getValue(0), new Double(1.0));
    assertEquals(data.getValue(1), new Double(2.0));
    assertEquals(data.getValue(2), new Double(3.0));
    assertEquals(data.getValue(3), null);

}

From source file:org.jfree.data.junit.KeyedObjectsTests.java

/**
 * Tests sorting of data by value (ascending).
 *//*from w w  w .  j a va 2s  .c  om*/
public void testSortByValueAscending() {
    KeyedObjects data = new KeyedObjects();
    data.addObject("C", new Double(1.0));
    data.addObject("B", null);
    data.addObject("D", new Double(3.0));
    data.addObject("A", new Double(2.0));

    data.sortByObjects(SortOrder.ASCENDING);

    // check key order
    assertEquals(data.getKey(0), "C");
    assertEquals(data.getKey(1), "A");
    assertEquals(data.getKey(2), "D");
    assertEquals(data.getKey(3), "B");

    // check retrieve value by key
    assertEquals(data.getObject("A"), new Double(2.0));
    assertEquals(data.getObject("B"), null);
    assertEquals(data.getObject("C"), new Double(1.0));
    assertEquals(data.getObject("D"), new Double(3.0));

    // check retrieve value by index
    assertEquals(data.getObject(0), new Double(1.0));
    assertEquals(data.getObject(1), new Double(2.0));
    assertEquals(data.getObject(2), new Double(3.0));
    assertEquals(data.getObject(3), null);
}