1 4 package com.tctest.performance.simulate.type; 5 6 import java.lang.reflect.Constructor ; 7 8 final class SimulatedPrimitiveType extends AbstractSimulatedType { 9 10 private Object masterCopy; 11 private static int value; 12 private static int iteration = 1; 13 14 SimulatedPrimitiveType(Object obj) { 15 this.masterCopy = obj; 16 } 17 18 public Class getType() { 19 return masterCopy.getClass(); 20 } 21 22 public Object cloneUnique() { 23 try { 24 Constructor constructor = masterCopy.getClass().getConstructor(new Class [] { String .class }); 25 return constructor.newInstance(new Object [] { String.valueOf(incrementValue()) }); 26 } catch (Exception e) { 27 throw new RuntimeException (e); 28 } 29 } 30 31 public Object clone() { 32 try { 33 Constructor constructor = masterCopy.getClass().getConstructor(new Class [] { String .class }); 34 return constructor.newInstance(new Object [] { masterCopy.toString() }); 35 } catch (Exception e) { 36 throw new RuntimeException (e); 37 } 38 } 39 40 private static synchronized int value() { 41 return value; 42 } 43 44 private static synchronized int iteration() { 45 return iteration; 46 } 47 48 private static synchronized int incrementValue() { 49 if (value == Integer.MAX_VALUE) { 50 value = 0; 51 incrementIteration(); 52 } 53 return ++value; 54 } 55 56 private static synchronized void incrementIteration() { 57 iteration++; 58 } 59 60 public String toString() { 61 return "{type=" + masterCopy.getClass().getName() + " unique-value=" + value() + " iteration=" + iteration() 62 + " masterCopy=" + masterCopy + "}"; 63 } 64 } 65 | Popular Tags |