1 import otherpackage.OtherPackagePublicEnum;
2 
3 import java.lang.reflect.*;
4 
5 public class Main {
6     /** used by {@link #basisCall} */
7     static private int basisTestValue = 12;
8 
main(String[] args)9     static public void main(String[] args) throws Exception {
10         try {
11             Class<?> enumClass = Enum.class;
12             Method enumValueOf = null;
13             for (Method m : enumClass.getDeclaredMethods()) {
14                 if (m.getName().equals("valueOf")) {
15                     enumValueOf = m;
16                     break;
17                 }
18             }
19             enumValueOf.invoke(null, String.class, "blah");
20             throw new AssertionError();
21         } catch (InvocationTargetException expected) {
22             IllegalArgumentException iae = (IllegalArgumentException) expected.getCause();
23             if (!iae.getMessage().equals("class java.lang.String is not an enum type")) {
24                 throw new AssertionError();
25             }
26         }
27 
28         boolean timing = (args.length >= 1) && args[0].equals("--timing");
29         run(timing);
30     }
31 
run(boolean timing)32     static public void run(boolean timing) {
33         preTest();
34 
35         long time0 = System.nanoTime();
36         int count1 = test1(500);
37         long time1 = System.nanoTime();
38         int count2 = test2(500);
39         long time2 = System.nanoTime();
40         int count3 = test3(500);
41         long time3 = System.nanoTime();
42         int count4 = basis(500);
43         long time4 = System.nanoTime();
44 
45         System.out.println("basis: performed " + count4 + " iterations");
46         System.out.println("test1: performed " + count1 + " iterations");
47         System.out.println("test2: performed " + count2 + " iterations");
48         System.out.println("test3: performed " + count3 + " iterations");
49 
50         double msec1 = (time1 - time0) / (double) count1 / 1000000;
51         double msec2 = (time2 - time1) / (double) count2 / 1000000;
52         double msec3 = (time3 - time2) / (double) count3 / 1000000;
53         double basisMsec = (time4 - time3) / (double) count4 / 1000000;
54 
55         double avg = (msec1 + msec2 + msec3) / 3;
56         if (avg < (basisMsec * 10)) {
57             System.out.println("Timing is acceptable.");
58         } else {
59             System.out.println("Iterations are taking too long!");
60             timing = true;
61         }
62 
63         if (timing) {
64             System.out.printf("basis time: %.3g msec\n", basisMsec);
65             System.out.printf("test1: %.3g msec per iteration\n", msec1);
66             System.out.printf("test2: %.3g msec per iteration\n", msec2);
67             System.out.printf("test3: %.3g msec per iteration\n", msec3);
68         }
69 
70     }
71 
preTest()72     static public void preTest() {
73         /*
74          * This is meant to ensure that the basic enum functionality
75          * really is working.
76          */
77 
78         Class<SamePackagePublicEnum> c = SamePackagePublicEnum.class;
79 
80         System.out.println(Enum.valueOf(c, "FOUR"));
81         System.out.println(Enum.valueOf(c, "ONE"));
82         System.out.println(Enum.valueOf(c, "FOURTEEN"));
83         System.out.println(Enum.valueOf(c, "NINE"));
84         System.out.println(Enum.valueOf(c, "FIVE"));
85         System.out.println(Enum.valueOf(c, "TWELVE"));
86 
87         System.out.println(Enum.valueOf(c, "ZERO").getClass().getName());
88     }
89 
90     static final String[] BASIS_COMPARE_ARRAY = {
91         "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT",
92         "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN",
93         "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"
94     };
95 
basis(int iters)96     static public int basis(int iters) {
97         for (int i = iters; i > 0; i--) {
98             basisValueOf("ZERO");
99             basisValueOf("ONE");
100             basisValueOf("TWO");
101             basisValueOf("THREE");
102             basisValueOf("FOUR");
103             basisValueOf("FIVE");
104             basisValueOf("SIX");
105             basisValueOf("SEVEN");
106             basisValueOf("EIGHT");
107             basisValueOf("NINE");
108             basisValueOf("TEN");
109             basisValueOf("ELEVEN");
110             basisValueOf("TWELVE");
111             basisValueOf("THIRTEEN");
112             basisValueOf("FOURTEEN");
113             basisValueOf("FIFTEEN");
114             basisValueOf("SIXTEEN");
115             basisValueOf("SEVENTEEN");
116             basisValueOf("EIGHTEEN");
117             basisValueOf("NINETEEN");
118         }
119 
120         return iters * 20;
121     }
122 
basisValueOf(String key)123     static String basisValueOf(String key) {
124         for (String s : BASIS_COMPARE_ARRAY) {
125             if (s.equals(key)) {
126                 return s;
127             }
128         }
129         throw new IllegalArgumentException();
130     }
131 
test1(int iters)132     static public int test1(int iters) {
133         Class<SamePackagePublicEnum> c = SamePackagePublicEnum.class;
134         for (int i = iters; i > 0; i--) {
135             Enum.valueOf(c, "ZERO");
136             Enum.valueOf(c, "ONE");
137             Enum.valueOf(c, "TWO");
138             Enum.valueOf(c, "THREE");
139             Enum.valueOf(c, "FOUR");
140             Enum.valueOf(c, "FIVE");
141             Enum.valueOf(c, "SIX");
142             Enum.valueOf(c, "SEVEN");
143             Enum.valueOf(c, "EIGHT");
144             Enum.valueOf(c, "NINE");
145             Enum.valueOf(c, "TEN");
146             Enum.valueOf(c, "ELEVEN");
147             Enum.valueOf(c, "TWELVE");
148             Enum.valueOf(c, "THIRTEEN");
149             Enum.valueOf(c, "FOURTEEN");
150             Enum.valueOf(c, "FIFTEEN");
151             Enum.valueOf(c, "SIXTEEN");
152             Enum.valueOf(c, "SEVENTEEN");
153             Enum.valueOf(c, "EIGHTEEN");
154             Enum.valueOf(c, "NINETEEN");
155         }
156 
157         return iters * 20;
158     }
159 
test2(int iters)160     static public int test2(int iters) {
161         Class<SamePackagePrivateEnum> c = SamePackagePrivateEnum.class;
162         for (int i = iters; i > 0; i--) {
163             Enum.valueOf(c, "ZERO");
164             Enum.valueOf(c, "ONE");
165             Enum.valueOf(c, "TWO");
166             Enum.valueOf(c, "THREE");
167             Enum.valueOf(c, "FOUR");
168             Enum.valueOf(c, "FIVE");
169             Enum.valueOf(c, "SIX");
170             Enum.valueOf(c, "SEVEN");
171             Enum.valueOf(c, "EIGHT");
172             Enum.valueOf(c, "NINE");
173             Enum.valueOf(c, "TEN");
174             Enum.valueOf(c, "ELEVEN");
175             Enum.valueOf(c, "TWELVE");
176             Enum.valueOf(c, "THIRTEEN");
177             Enum.valueOf(c, "FOURTEEN");
178             Enum.valueOf(c, "FIFTEEN");
179             Enum.valueOf(c, "SIXTEEN");
180             Enum.valueOf(c, "SEVENTEEN");
181             Enum.valueOf(c, "EIGHTEEN");
182             Enum.valueOf(c, "NINETEEN");
183         }
184 
185         return iters * 20;
186     }
187 
test3(int iters)188     static public int test3(int iters) {
189         Class<OtherPackagePublicEnum> c = OtherPackagePublicEnum.class;
190         for (int i = iters; i > 0; i--) {
191             Enum.valueOf(c, "ZERO");
192             Enum.valueOf(c, "ONE");
193             Enum.valueOf(c, "TWO");
194             Enum.valueOf(c, "THREE");
195             Enum.valueOf(c, "FOUR");
196             Enum.valueOf(c, "FIVE");
197             Enum.valueOf(c, "SIX");
198             Enum.valueOf(c, "SEVEN");
199             Enum.valueOf(c, "EIGHT");
200             Enum.valueOf(c, "NINE");
201             Enum.valueOf(c, "TEN");
202             Enum.valueOf(c, "ELEVEN");
203             Enum.valueOf(c, "TWELVE");
204             Enum.valueOf(c, "THIRTEEN");
205             Enum.valueOf(c, "FOURTEEN");
206             Enum.valueOf(c, "FIFTEEN");
207             Enum.valueOf(c, "SIXTEEN");
208             Enum.valueOf(c, "SEVENTEEN");
209             Enum.valueOf(c, "EIGHTEEN");
210             Enum.valueOf(c, "NINETEEN");
211         }
212 
213         return iters * 20;
214     }
215 }
216