Example usage for org.jfree.data KeyedObject clone

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

Introduction

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

Prototype

@Override
public Object clone() throws CloneNotSupportedException 

Source Link

Document

Returns a clone of this object.

Usage

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

/**
 * Confirm that cloning works./*from ww w.j a  va  2  s  .co m*/
 */
@Test
public void testCloning() throws CloneNotSupportedException {
    KeyedObject ko1 = new KeyedObject("Test", "Object");
    KeyedObject ko2 = (KeyedObject) ko1.clone();
    assertTrue(ko1 != ko2);
    assertTrue(ko1.getClass() == ko2.getClass());
    assertTrue(ko1.equals(ko2));
}

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

/**
 * Confirm special features of cloning.//from  w  ww .j  av  a 2s  .  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 a clone of this object.  Keys in the list should be immutable
 * and are not cloned.  Objects in the list are cloned only if they
 * implement {@link PublicCloneable}.//from www  .ja va 2  s  .  c om
 *
 * @return A clone.
 *
 * @throws CloneNotSupportedException if there is a problem cloning.
 */
@Override
public Object clone() throws CloneNotSupportedException {
    KeyedObjects clone = (KeyedObjects) super.clone();
    clone.data = new java.util.ArrayList();
    Iterator iterator = this.data.iterator();
    while (iterator.hasNext()) {
        KeyedObject ko = (KeyedObject) iterator.next();
        clone.data.add(ko.clone());
    }
    return clone;
}