Example usage for org.jfree.data KeyedObjects KeyedObjects

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

Introduction

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

Prototype

public KeyedObjects() 

Source Link

Document

Creates a new collection (initially empty).

Usage

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

/**
 * Confirm that cloning works./*from  www . ja  v a2  s  .c om*/
 */
@Test
public void testCloning() throws CloneNotSupportedException {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("V1", new Integer(1));
    ko1.addObject("V2", null);
    ko1.addObject("V3", new Integer(3));
    KeyedObjects ko2 = (KeyedObjects) ko1.clone();
    assertTrue(ko1 != ko2);
    assertTrue(ko1.getClass() == ko2.getClass());
    assertTrue(ko1.equals(ko2));
}

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

/**
 * Confirm special features of cloning.//w  w  w.j  a v a 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.junit.KeyedObjectsTests.java

/**
 * Confirm that cloning works./*w  w w.  j  a v a 2s. co m*/
 */
public void testCloning() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("V1", new Integer(1));
    ko1.addObject("V2", null);
    ko1.addObject("V3", new Integer(3));
    KeyedObjects ko2 = null;
    try {
        ko2 = (KeyedObjects) ko1.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    assertTrue(ko1 != ko2);
    assertTrue(ko1.getClass() == ko2.getClass());
    assertTrue(ko1.equals(ko2));
}

From source file:org.jfree.data.pie.DefaultPieDataset.java

/**
 * Constructs a new dataset, initially empty.
 *//*from  www. ja v  a 2 s . c  o m*/
public DefaultPieDataset() {
    this.data = new KeyedObjects();
    setSelectionState(this);
}

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

/**
 * Check that inserting and retrieving values works as expected.
 *///  w w w. j  a va 2 s .com
@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);

}

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

/**
 * Confirm special features of cloning./*  w  ww  .  j  a  va 2  s  .  c o m*/
 */
public void testCloning2() {
    // case 1 - object is mutable but not PublicCloneable
    Object obj1 = new ArrayList();
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("K1", obj1);
    KeyedObjects ko2 = null;
    try {
        ko2 = (KeyedObjects) ko1.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    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 = null;
    try {
        ko2 = (KeyedObjects) ko1.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    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.pie.DefaultPieDataset.java

/**
 * Creates a new dataset by copying data from a {@link KeyedValues}
 * instance.//www . ja v a  2  s .c om
 *
 * @param data  the data (<code>null</code> not permitted).
 */
public DefaultPieDataset(KeyedValues data) {
    if (data == null) {
        throw new IllegalArgumentException("Null 'data' argument.");
    }
    this.data = new KeyedObjects();
    for (int i = 0; i < data.getItemCount(); i++) {
        SelectableValue dataItem = new SelectableValue(data.getValue(i));
        this.data.addObject(data.getKey(i), dataItem);
    }
}

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

/**
 * Serialize an instance, restore it, and check for equality.
 *///from w w w. ja v  a 2  s  .c om
@Test
public void testSerialization() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("Key 1", "Object 1");
    ko1.addObject("Key 2", null);
    ko1.addObject("Key 3", "Object 2");
    KeyedObjects ko2 = (KeyedObjects) TestUtilities.serialised(ko1);
    assertEquals(ko1, ko2);
}

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

/**
 * Check that inserting and retrieving values works as expected.
 *///from  ww  w.j av  a 2 s.  co  m
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);

}

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

/**
 * Simple checks for the getObject(int) method.
 *///from  w  w  w.j av  a  2 s. c  o  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);
}