Example usage for org.jfree.data KeyedObjects setObject

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

Introduction

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

Prototype

public void setObject(Comparable key, Object object) 

Source Link

Document

Replaces an existing object, or adds a new object to the collection.

Usage

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

/**
 * Some checks for the setObject(Comparable, Object) method.
 *//*from  www.  jav a 2s. com*/
@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

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

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    // try negative key index
    boolean pass = false;
    try {
        ko1.removeValue(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    // try key index == itemCount
    pass = false;
    try {
        ko1.removeValue(2);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Some checks for the removeValue() methods.
 *///from   w ww  .  j  a v  a2s  . co  m
@Test
public void testRemoveValue() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    ko1.removeValue("Key 1");
    assertEquals(1, ko1.getItemCount());
    assertEquals(0, ko1.getIndex("Key 3"));

    // try unknown key
    boolean pass = false;
    try {
        ko1.removeValue("UNKNOWN");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);

    // try null argument
    pass = false;
    try {
        ko1.removeValue(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Some checks for the setObject(Comparable, Object) method.
 *//*from   w ww .jav a 2 s . c  o  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

/**
 * Some checks for the removeValue(int) method.
 *///from   w  w w  .jav  a 2 s  .  c  o m
public void testRemoveValueInt() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    // try negative key index
    boolean pass = false;
    try {
        ko1.removeValue(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    // try key index == itemCount
    pass = false;
    try {
        ko1.removeValue(2);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Some checks for the removeValue() methods.
 *///  w w  w  .j  av  a  2s.  c om
public void testRemoveValue() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    ko1.removeValue("Key 1");
    assertEquals(1, ko1.getItemCount());
    assertEquals(0, ko1.getIndex("Key 3"));

    // try unknown key
    boolean pass = false;
    try {
        ko1.removeValue("UNKNOWN");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);

    // try null argument
    pass = false;
    try {
        ko1.removeValue(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Adds or updates an object.//  w  ww . j a va  2 s  .  c  om
 *
 * @param object  the object.
 * @param rowKey  the row key (<code>null</code> not permitted).
 * @param columnKey  the column key (<code>null</code> not permitted).
 */
public void setObject(Object object, Comparable rowKey, Comparable columnKey) {
    ParamChecks.nullNotPermitted(rowKey, "rowKey");
    ParamChecks.nullNotPermitted(columnKey, "columnKey");
    KeyedObjects row;
    int rowIndex = this.rowKeys.indexOf(rowKey);
    if (rowIndex >= 0) {
        row = (KeyedObjects) this.rows.get(rowIndex);
    } else {
        this.rowKeys.add(rowKey);
        row = new KeyedObjects();
        this.rows.add(row);
    }
    row.setObject(columnKey, object);
    int columnIndex = this.columnKeys.indexOf(columnKey);
    if (columnIndex < 0) {
        this.columnKeys.add(columnKey);
    }
}