Java Generic Deep Copy(二)

2014-11-24 03:19:36 · 作者: · 浏览: 1
assertFalse(integerArray[i] == integerArrayCopy[i]); // Integers use .equals

assertTrue(integerArray[i].equals(integerArrayCopy[i]));

}

for (int i = 0; i < stringListFromArray.size(); i++) { // instances are different, values are equal.

assertFalse(stringListFromArray.get(i) == stringListFromArrayCopy.get(i));

assertTrue(stringListFromArray.get(i).equals(stringListFromArrayCopy.get(i)));

}

for (int i = 0; i < normalList.size(); i++) { // instances are different, values are equal.

assertFalse(normalList.get(i) == normalListCopy.get(i));

assertTrue(normalList.get(i).equals(normalListCopy.get(i)));

}

assertTrue(cal.getClass() == calCopy.getClass()); // assure class types are the same.

assertTrue(intArray.getClass() == intArrayCopy.getClass());

assertTrue(integerArray.getClass() == integerArrayCopy.getClass());

assertTrue(stringListFromArray.getClass() == stringListFromArrayCopy.getClass());

assertTrue(normalList.getClass() == normalListCopy.getClass());

}

大型LinkedList

深度拷贝超过包含100,000个对象的LinkedList

/** Assure that a LinkedList of 100,000 elements can be deep copied.

* Add -Xms512m and -Xmx1536m to the JVM args to see this work with 1,000,000.

* @throws Exception */

public void testLinkedList() throws Exception {

DeepCopyUtil deepCopyUtil = new DeepCopyUtil(); // used to create deep copies of objects.

List lst = new LinkedList(); // Create a linked list of Integers.

for (int i = 0; i < 100000; i++) {

lst.add(i);

}

List copyOfList = deepCopyUtil.deepCopy(lst); // create a deep copy

assertTrue(copyOfList.size() == lst.size()); // test that the lists are the same size.

assertFalse(copyOfList == lst); // test that the lists are not the same lists.

// create some iterators instead of using list.get(), because we're using a LinkedList

// and that would be slow.

Iterator origIt = lst.iterator();

Iterator copyIt = copyOfList.iterator();

Integer origInt, copyInt;

while (origIt.hasNext()) { // compare each of the items in both lists

origInt = origIt.next();

copyInt = copyIt.next();

assertFalse(origInt == copyInt); // the lists should not be the same instance.

assertTrue(origInt.equals(copyInt)); // the inner values should be the same.

}

}

循环引用

深度拷贝一个循环引用的类。这个类包含很多对自己的引用并且仍然能够被正确拷贝。测试显示,这些循环引用仍然能够保持一样的关系。(例如:内部实例仍然能够正确的引用外部的类实例)

package test.com.ajexperience.utils.deepcopyutil;

import junit.framework.TestCase;

import com.ajexperience.utils.DeepCopyUtil;

public class DeepCopyTestSimpleCircle extends TestCase {

public InnerClass outerToInner = new InnerClass(); // an instance of the inner class

public DeepCopyTestSimpleCircle pointerToSelf;

public class InnerClass { // an inner class with a link to the outer class

DeepCopyTestSimpleCircle innerToOuter;

}

public DeepCopyTestSimpleCircle() {

outerToInner.innerToOuter = this; // link the inner class to the outer class

this.pointerToSelf = this; // self reference

}

public static void main(String args[]) throws Exception {

DeepCopyUtil deepCopyUtil = new DeepCopyUtil(); // used to create deep copies of objects.

DeepCopyTestSimpleCircle simpleCircle = new DeepCopyTestSimpleCircle(); // create an instance.

DeepCopyTestSimpleCircle