| 1 4 package com.tctest.performance.simulate.type; 5 6 import java.lang.reflect.Constructor ; 7 import java.util.Arrays ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 11 final class SimulatedCollection extends AbstractSimulatedType { 12 13 private Collection masterCopy; 14 15 18 SimulatedCollection(Collection collection) { 19 for (Iterator iter = collection.iterator(); iter.hasNext();) { 20 if (!(iter.next() instanceof SimulatedType)) throw new RuntimeException ("Collection elements must be of type: " 21 + SimulatedType.class.getName()); 22 } 23 this.masterCopy = collection; 24 } 25 26 public Class getType() { 27 return masterCopy.getClass(); 28 } 29 30 public Object cloneUnique() { 31 try { 32 Constructor constructor = masterCopy.getClass().getConstructor(new Class [0]); 33 Collection clone = (Collection ) constructor.newInstance(new Object [0]); 34 for (Iterator iter = masterCopy.iterator(); iter.hasNext();) { 35 clone.add(((SimulatedType) iter.next()).cloneUnique()); 36 } 37 return clone; 38 } catch (Exception e) { 39 throw new RuntimeException (e); 40 } 41 } 42 43 public Object clone() { 44 try { 45 Constructor constructor = masterCopy.getClass().getConstructor(new Class [0]); 46 Collection clone = (Collection ) constructor.newInstance(new Object [0]); 47 for (Iterator iter = masterCopy.iterator(); iter.hasNext();) { 48 clone.add(((SimulatedType) iter.next()).clone()); 49 } 50 return clone; 51 } catch (Exception e) { 52 throw new RuntimeException (e); 53 } 54 } 55 56 public String toString() { 57 return "{type=" + masterCopy.getClass().getName() + " elements=" + Arrays.asList(masterCopy.toArray()) + "}"; 58 } 59 } 60 | Popular Tags |