Example usage for org.jfree.data KeyedObjects getObject

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

Introduction

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

Prototype

public Object getObject(Comparable key) 

Source Link

Document

Returns the object for a given key.

Usage

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

/**
 * Some checks for the setObject(Comparable, Object) method.
 *//* w w  w .  j av  a2  s . c o  m*/
@Test
public void testSetObject() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    assertEquals("Object 1", ko1.getObject("Key 1"));
    assertEquals(null, ko1.getObject("Key 2"));
    assertEquals("Object 2", ko1.getObject("Key 3"));

    // replace an existing value
    ko1.setObject("Key 2", "AAA");
    ko1.setObject("Key 3", "BBB");
    assertEquals("AAA", ko1.getObject("Key 2"));
    assertEquals("BBB", ko1.getObject("Key 3"));

    // try a null key - should throw an exception
    boolean pass = false;
    try {
        ko1.setObject(null, "XX");
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Simple checks for the getObject(int) method.
 *///ww w  .  j  a  va  2s.co  m
@Test
public void testGetObject() {
    // retrieve an item
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("Key 1", "Object 1");
    ko1.addObject("Key 2", null);
    ko1.addObject("Key 3", "Object 2");
    assertEquals("Object 1", ko1.getObject(0));
    assertNull(ko1.getObject(1));
    assertEquals("Object 2", ko1.getObject(2));

    // request with a negative index
    boolean pass = false;
    try {
        ko1.getObject(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    // request width index == itemCount
    pass = false;
    try {
        ko1.getObject(3);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Confirm special features of cloning./*from   w w w .  j a  va  2 s  .c  o  m*/
 */
@Test
public void testCloning2() throws CloneNotSupportedException {
    // case 1 - object is mutable but not PublicCloneable
    Object obj1 = new ArrayList();
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("K1", obj1);
    KeyedObjects ko2 = (KeyedObjects) 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("K1") == obj1);

    // CASE 2 - object is mutable AND PublicCloneable
    obj1 = new DefaultPieDataset();
    ko1 = new KeyedObjects();
    ko1.addObject("K1", obj1);
    ko2 = (KeyedObjects) 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("K1") != obj1);
}

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

/**
 * Returns the object for the given row and column keys.
 *
 * @param rowKey  the row key (<code>null</code> not permitted).
 * @param columnKey  the column key (<code>null</code> not permitted).
 *
 * @return The object (possibly <code>null</code>).
 *
 * @throws IllegalArgumentException if <code>rowKey</code> or
 *         <code>columnKey</code> is <code>null</code>.
 * @throws UnknownKeyException if <code>rowKey</code> or
 *         <code>columnKey</code> is not recognised.
 *//*from  w  ww. jav  a 2  s . co  m*/
public Object getObject(Comparable rowKey, Comparable columnKey) {
    ParamChecks.nullNotPermitted(rowKey, "rowKey");
    ParamChecks.nullNotPermitted(columnKey, "columnKey");
    int row = this.rowKeys.indexOf(rowKey);
    if (row < 0) {
        throw new UnknownKeyException("Row key (" + rowKey + ") not recognised.");
    }
    int column = this.columnKeys.indexOf(columnKey);
    if (column < 0) {
        throw new UnknownKeyException("Column key (" + columnKey + ") not recognised.");
    }
    KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
    int index = rowData.getIndex(columnKey);
    if (index >= 0) {
        return rowData.getObject(index);
    } else {
        return null;
    }
}

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

/**
 * Removes an object from the table by setting it to <code>null</code>.  If
 * all the objects in the specified row and/or column are now
 * <code>null</code>, the row and/or column is removed from the table.
 *
 * @param rowKey  the row key (<code>null</code> not permitted).
 * @param columnKey  the column key (<code>null</code> not permitted).
 *
 * @see #addObject(Object, Comparable, Comparable)
 */// w  ww  .  j a v  a2  s. c  om
public void removeObject(Comparable rowKey, Comparable columnKey) {
    int rowIndex = getRowIndex(rowKey);
    if (rowIndex < 0) {
        throw new UnknownKeyException("Row key (" + rowKey + ") not recognised.");
    }
    int columnIndex = getColumnIndex(columnKey);
    if (columnIndex < 0) {
        throw new UnknownKeyException("Column key (" + columnKey + ") not recognised.");
    }
    setObject(null, rowKey, columnKey);

    // 1. check whether the row is now empty.
    boolean allNull = true;
    KeyedObjects row = (KeyedObjects) this.rows.get(rowIndex);

    for (int item = 0, itemCount = row.getItemCount(); item < itemCount; item++) {
        if (row.getObject(item) != null) {
            allNull = false;
            break;
        }
    }

    if (allNull) {
        this.rowKeys.remove(rowIndex);
        this.rows.remove(rowIndex);
    }

    // 2. check whether the column is now empty.
    allNull = true;

    for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) {
        row = (KeyedObjects) this.rows.get(item);
        int colIndex = row.getIndex(columnKey);
        if (colIndex >= 0 && row.getObject(colIndex) != null) {
            allNull = false;
            break;
        }
    }

    if (allNull) {
        for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) {
            row = (KeyedObjects) this.rows.get(item);
            int colIndex = row.getIndex(columnKey);
            if (colIndex >= 0) {
                row.removeValue(colIndex);
            }
        }
        this.columnKeys.remove(columnKey);
    }
}

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

/**
 * Some checks for the setObject(Comparable, Object) method.
 *///w ww  .jav  a2s.co m
public void testSetObject() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    assertEquals("Object 1", ko1.getObject("Key 1"));
    assertEquals(null, ko1.getObject("Key 2"));
    assertEquals("Object 2", ko1.getObject("Key 3"));

    // replace an existing value
    ko1.setObject("Key 2", "AAA");
    ko1.setObject("Key 3", "BBB");
    assertEquals("AAA", ko1.getObject("Key 2"));
    assertEquals("BBB", ko1.getObject("Key 3"));

    // try a null key - should throw an exception
    boolean pass = false;
    try {
        ko1.setObject(null, "XX");
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Simple checks for the getObject(int) method.
 *///  w  w  w. ja  va  2  s .  c o m
public void testGetObject() {
    // retrieve an item
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("Key 1", "Object 1");
    ko1.addObject("Key 2", null);
    ko1.addObject("Key 3", "Object 2");
    assertEquals("Object 1", ko1.getObject(0));
    assertNull(ko1.getObject(1));
    assertEquals("Object 2", ko1.getObject(2));

    // request with a negative index
    boolean pass = false;
    try {
        ko1.getObject(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    // request width index == itemCount
    pass = false;
    try {
        ko1.getObject(3);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Tests this object for equality with an arbitrary object.
 *
 * @param obj  the object (<code>null</code> permitted).
 *
 * @return A boolean.//from  w  ww .  ja v  a2 s  . co  m
 */
@Override
public boolean equals(Object obj) {

    if (obj == this) {
        return true;
    }
    if (!(obj instanceof KeyedObjects)) {
        return false;
    }
    KeyedObjects that = (KeyedObjects) obj;
    int count = getItemCount();
    if (count != that.getItemCount()) {
        return false;
    }

    for (int i = 0; i < count; i++) {
        Comparable k1 = getKey(i);
        Comparable k2 = that.getKey(i);
        if (!k1.equals(k2)) {
            return false;
        }
        Object o1 = getObject(i);
        Object o2 = that.getObject(i);
        if (o1 == null) {
            if (o2 != null) {
                return false;
            }
        } else {
            if (!o1.equals(o2)) {
                return false;
            }
        }
    }
    return true;

}

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

/**
 * Returns the object for a given row and column.
 *
 * @param row  the row index (in the range 0 to getRowCount() - 1).
 * @param column  the column index (in the range 0 to getColumnCount() - 1).
 *
 * @return The object (possibly <code>null</code>).
 *
 * @see #getObject(Comparable, Comparable)
 *//*  www .j  ava 2  s  . c o m*/
public Object getObject(int row, int column) {
    Object result = null;
    KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
    if (rowData != null) {
        Comparable columnKey = (Comparable) this.columnKeys.get(column);
        if (columnKey != null) {
            int index = rowData.getIndex(columnKey);
            if (index >= 0) {
                result = rowData.getObject(columnKey);
            }
        }
    }
    return result;
}

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

/**
 * Check that inserting and retrieving values works as expected.
 *//*  ww w. j ava  2s .  c  o  m*/
@Test
public void testInsertAndRetrieve() {

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

    // 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.getObject("A"), new Double(1.0));
    assertEquals(data.getObject("B"), new Double(2.0));
    assertEquals(data.getObject("C"), new Double(3.0));
    assertEquals(data.getObject("D"), null);

    boolean pass = false;
    try {
        data.getObject("Not a key");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);

    // 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);

}