001/** 002 * Copyright 2004-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.student.contract.writer; 017 018/** 019 * 020 * @author nwright 021 */ 022public class JavaEnumConstantCalculator { 023 024 private String name; 025 026 public JavaEnumConstantCalculator(String name) { 027 this.name = name; 028 } 029 030 public String calc() { 031 StringBuilder buf = new StringBuilder(name.length() + 3); 032 // do the first character so we don't prepend the first with a _ if it is upper 033 char c = Character.toUpperCase(name.charAt(0)); 034 buf.append(c); 035 boolean lastUpper = Character.isUpperCase(c); 036 for (int i = 1; i < name.length(); i++) { 037 c = name.charAt(i); 038 if (Character.isUpperCase(c)) { 039 if (!lastUpper) { 040 buf.append('_'); 041 } 042 lastUpper = true; 043 } else { 044 lastUpper = false; 045 } 046 047 buf.append(Character.toUpperCase(c)); 048 } 049 050 return buf.toString(); 051 } 052 053 public String reverse() { 054 StringBuffer buf = new StringBuffer(name.length()); 055 boolean uppercase = true; 056 for (int i = 0; i < name.length(); i++) { 057 char c = name.charAt(i); 058 if (uppercase) { 059 c = Character.toUpperCase(c); 060 uppercase = false; 061 } else { 062 c = Character.toLowerCase(c); 063 } 064 if (c == '_') { 065 uppercase = true; 066 continue; 067 } 068 buf.append(c); 069 } 070 071 return buf.toString(); 072 } 073}