原文:http://www.genericdeepcopy.com/
Java Generic Deep Copy
Generic Deep Copy 是一个用于深度拷贝的java实用程序。这些被拷贝的对象不需要去实现Serializable,Cloneable或者任何其他的接口。基本上,这个程序可以深度拷贝任何java对象。在很多场合,使用它可以节省时间和精力。
许可证:
由 Aaron Johnson 编写的java程序Generic Deep Copy遵守modified Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License许可证。对于引用作品或者任何商业目的的商业授权,请参考联系页面或者联系销售。
基本信息
这款软件对于个人是可以免费使用的,并且不能用于商业(用于盈利)目的。
最近的代码拷贝可以在以下网站找到:
http://www.genericdeepcopy.com.
允许:
下载:
从这个网站下载该软件,代表你已经同意这些许可证协议,包括你同意不会将该作品用于任何商业或者盈利的目的。
在下载之前,请阅读许可证协议。
Generic Deep Copy 需要 Java 1.5及以上。
点击下载:
DeepCopyUtil-1.4
/** Basic usage of the utility. * @throws Exception */ public void testBasicUsage() throws Exception { DeepCopyUtil deepCopyUtil = new DeepCopyUtil(); // create deep copies of objects. // create an object CustomClass customObject = new CustomClass(); // create a copy of the object CustomClass copy = deepCopyUtil.deepCopy(customObject); assertFalse(customObject == copy); }
/** Test that deep copy correctly copies basic object types. * @throws Exception */ public void testVariousTypes() throws Exception { DeepCopyUtil deepCopyUtil = new DeepCopyUtil(); // used to create deep copies of objects. Calendar cal = Calendar.getInstance(); int intArray[] = {1,2,3}; Integer integerArray[] = new Integer[]{1,2,3}; List<String> stringListFromArray = Arrays.asList(new String[]{"one", "two"}); List<String> normalList = new ArrayList<String>(); normalList.add("weeeeee"); Calendar calCopy = deepCopyUtil.deepCopy(cal); // create deep copies of objects. int intArrayCopy[] = deepCopyUtil.deepCopy(intArray); Integer integerArrayCopy[] = deepCopyUtil.deepCopy(integerArray); List<String> stringListFromArrayCopy = deepCopyUtil.deepCopy(stringListFromArray); List<String> normalListCopy = deepCopyUtil.deepCopy(normalList); assertFalse(cal == calCopy); // assure instances are different. assertFalse(intArray == intArrayCopy); assertFalse(integerArray == integerArrayCopy); assertFalse(stringListFromArray == stringListFromArrayCopy); assertFalse(normalList == normalListCopy); assertTrue(cal.equals(calCopy)); // assure values are the same. for (int i = 0; i < intArray.length; i++) { assertTrue(intArray[i] == intArrayCopy[i]); // int's use == } for (int i = 0; i < integerArray.length; i++) { 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()); }
/** Assure that a LinkedList of 100,000 elements can be deep copied. <br/> * 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<Integer> lst = new LinkedList<Integer>(); // Create a linked list of Integers. for (int i = 0; i < 100000; i++) { lst.add(i); } List<Integer> 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<Integer> origIt = lst.iterator(); Iterator<Integer> 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 copy = deepCopyUtil.deepCopy(simpleCircle); // deep copy the object. assertFalse(simpleCircle == copy); // test that they are not equal. // test that the circular references are in tact. assertTrue(copy == copy.outerToInner.innerToOuter); assertTrue(copy == copy.pointerToSelf); } }