KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > simulate > type > SimulatedPrimitiveType


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.performance.simulate.type;
5
6 import java.lang.reflect.Constructor JavaDoc;
7
8 final class SimulatedPrimitiveType extends AbstractSimulatedType {
9
10   private Object JavaDoc masterCopy;
11   private static int value;
12   private static int iteration = 1;
13
14   SimulatedPrimitiveType(Object JavaDoc obj) {
15     this.masterCopy = obj;
16   }
17
18   public Class JavaDoc getType() {
19     return masterCopy.getClass();
20   }
21
22   public Object JavaDoc cloneUnique() {
23     try {
24       Constructor JavaDoc constructor = masterCopy.getClass().getConstructor(new Class JavaDoc[] { String JavaDoc.class });
25       return constructor.newInstance(new Object JavaDoc[] { String.valueOf(incrementValue()) });
26     } catch (Exception JavaDoc e) {
27       throw new RuntimeException JavaDoc(e);
28     }
29   }
30
31   public Object JavaDoc clone() {
32     try {
33       Constructor JavaDoc constructor = masterCopy.getClass().getConstructor(new Class JavaDoc[] { String JavaDoc.class });
34       return constructor.newInstance(new Object JavaDoc[] { masterCopy.toString() });
35     } catch (Exception JavaDoc e) {
36       throw new RuntimeException JavaDoc(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 JavaDoc toString() {
61     return "{type=" + masterCopy.getClass().getName() + " unique-value=" + value() + " iteration=" + iteration()
62            + " masterCopy=" + masterCopy + "}";
63   }
64 }
65
Popular Tags