View Javadoc

1   /**
2    * Copyright 2005-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.rice.devtools.generators.mo;
17  
18  import java.util.Arrays;
19  import java.util.List;
20  
21  /**
22   * A class with general utility constants and methods used by the Model Object generator.
23   * 
24   * @author Kuali Rice Team (rice.collab@kuali.org)
25   *
26   */
27  final class Util {
28  	
29  	static final String VERSION_NUMBER_FIELD = "versionNumber";
30  	static final String OBJECT_ID_FIELD = "objectId";
31  	
32  	static final String CONSTANTS_CLASS_NAME = "Constants";
33  	static final String ROOT_ELEMENT_NAME_FIELD = "ROOT_ELEMENT_NAME";
34  	static final String TYPE_NAME_FIELD = "TYPE_NAME";
35  	static final String TYPE_NAME_SUFFIX = "Type";
36  	static final String HASH_CODE_EQUALS_EXCLUDE_FIELD = "HASH_CODE_EQUALS_EXCLUDE";
37  	static final String COMMON_ELEMENTS_CLASS = "CommonElements";
38  	static final String FUTURE_ELEMENTS_FIELD = "FUTURE_ELEMENTS";
39  	static final String CONSTANTS_CLASS_JAVADOC = "Defines some internal constants used on this class.";
40  	
41  	static final String ELEMENTS_CLASS_NAME = "Elements";
42  	static final String ELEMENTS_CLASS_JAVADOC = "A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.";
43  	static final List<String> COMMON_ELEMENTS = Arrays.asList(VERSION_NUMBER_FIELD, OBJECT_ID_FIELD);
44  	
45  	static final String BUILDER_CLASS_NAME = "Builder";
46  	
47  	static String toLowerCaseFirstLetter(String value) {
48  		return value.substring(0, 1).toLowerCase() + value.substring(1);
49  	}
50  	
51  	static String toUpperCaseFirstLetter(String value) {
52  		return value.substring(0, 1).toUpperCase() + value.substring(1);
53  	}
54  	
55  	static String toConstantsVariable(String fieldName) {
56  		StringBuilder constantVariable = new StringBuilder();
57  		// just to be safe
58  		fieldName = toLowerCaseFirstLetter(fieldName);
59  		StringBuilder segAccum = new StringBuilder();
60  		for (char character : fieldName.toCharArray()) {
61  			if (Character.isUpperCase(character)) {
62  				constantVariable.append(segAccum.toString().toUpperCase());
63  				constantVariable.append("_");
64  				segAccum = new StringBuilder();
65  			}
66  			segAccum.append(character);
67  		}
68  		// do the last bit
69  		constantVariable.append(segAccum.toString().toUpperCase());
70  		return constantVariable.toString();
71  	}
72  	
73  	static String generateGetterName(String fieldName, boolean is) {
74  		return (is ? "is" : "get") + Util.toUpperCaseFirstLetter(fieldName);
75  	}
76  	
77  	static String generateGetter(String fieldName, boolean is) {
78  		return generateGetterName(fieldName, is) + "()";
79  	}
80  	
81  	static String generateSetterName(String fieldName) {
82  		return "set" + Util.toUpperCaseFirstLetter(fieldName);
83  	}
84  	
85  	static String generateSetter(String fieldName, String valueToSet) {
86  		return generateSetterName(fieldName) + "(" + valueToSet + ")";
87  	} 
88  	
89  	
90  	static boolean isCommonElement(String fieldName) {
91  		return COMMON_ELEMENTS.contains(fieldName);
92  	}
93  		
94  	static String generateBuilderJavadoc(String simpleClassName, String simpleContractClassName) {
95  		return "A builder which can be used to construct {@link " + simpleClassName + "} instances.  " +
96  			"Enforces the constraints of the {@link " + simpleContractClassName + "}.";
97  	}
98  
99  }