1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.contract.writer;
17
18
19
20
21
22 public class JavaEnumConstantCalculator {
23
24 private String name;
25
26 public JavaEnumConstantCalculator(String name) {
27 this.name = name;
28 }
29
30 public String calc() {
31 StringBuilder buf = new StringBuilder(name.length() + 3);
32
33 char c = Character.toUpperCase(name.charAt(0));
34 buf.append(c);
35 boolean lastUpper = Character.isUpperCase(c);
36 for (int i = 1; i < name.length(); i++) {
37 c = name.charAt(i);
38 if (Character.isUpperCase(c)) {
39 if (!lastUpper) {
40 buf.append('_');
41 }
42 lastUpper = true;
43 } else {
44 lastUpper = false;
45 }
46
47 buf.append(Character.toUpperCase(c));
48 }
49
50 return buf.toString();
51 }
52
53 public String reverse() {
54 StringBuffer buf = new StringBuffer(name.length());
55 boolean uppercase = true;
56 for (int i = 0; i < name.length(); i++) {
57 char c = name.charAt(i);
58 if (uppercase) {
59 c = Character.toUpperCase(c);
60 uppercase = false;
61 } else {
62 c = Character.toLowerCase(c);
63 }
64 if (c == '_') {
65 uppercase = true;
66 continue;
67 }
68 buf.append(c);
69 }
70
71 return buf.toString();
72 }
73 }