Example usage for org.jfree.data KeyedObject getObject

List of usage examples for org.jfree.data KeyedObject getObject

Introduction

In this page you can find the example usage for org.jfree.data KeyedObject getObject.

Prototype

public Object getObject() 

Source Link

Document

Returns the object.

Usage

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

/**
 * Confirm special features of cloning.// w w  w .  j a v  a  2  s .  co  m
 */
@Test
public void testCloning2() throws CloneNotSupportedException {
    // case 1 - object is mutable but not PublicCloneable
    Object obj1 = new ArrayList();
    KeyedObject ko1 = new KeyedObject("Test", obj1);
    KeyedObject ko2 = (KeyedObject) ko1.clone();
    assertTrue(ko1 != ko2);
    assertTrue(ko1.getClass() == ko2.getClass());
    assertTrue(ko1.equals(ko2));

    // the clone contains a reference to the original object
    assertTrue(ko2.getObject() == obj1);

    // CASE 2 - object is mutable AND PublicCloneable
    obj1 = new DefaultPieDataset();
    ko1 = new KeyedObject("Test", obj1);
    ko2 = (KeyedObject) ko1.clone();
    assertTrue(ko1 != ko2);
    assertTrue(ko1.getClass() == ko2.getClass());
    assertTrue(ko1.equals(ko2));

    // the clone contains a reference to a CLONE of the original object
    assertTrue(ko2.getObject() != obj1);
}

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

/**
 * Returns an object from the list.// w w w.  j  a v  a2 s.  c  o m
 *
 * @param item  the item index (zero-based).
 *
 * @return The object (possibly <code>null</code>).
 *
 * @throws IndexOutOfBoundsException if <code>item</code> is out of bounds.
 */
public Object getObject(int item) {
    Object result = null;
    KeyedObject kobj = (KeyedObject) this.data.get(item);
    if (kobj != null) {
        result = kobj.getObject();
    }
    return result;
}

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 w w w  .j  a  v  a 2 s. c  o  m
 * @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.");
    }
}