View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.writer;
17  
18  /**
19   *
20   * @author nwright
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          // do the first character so we don't prepend the first with a _ if it is upper
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  }