View Javadoc
1   /**
2    * Copyright 2004-2014 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.service;
17  
18  import org.kuali.student.contract.model.MessageStructure;
19  import org.kuali.student.contract.model.ServiceContractModel;
20  import org.kuali.student.contract.writer.JavaClassWriter;
21  
22  /**
23   *
24   * @author nwright
25   */
26  public class GetterSetterNameCalculator {
27  
28      private MessageStructure ms;
29      private JavaClassWriter writer;
30      private ServiceContractModel model;
31  
32      public GetterSetterNameCalculator(MessageStructure ms,
33              JavaClassWriter writer,
34              ServiceContractModel model) {
35          this.ms = ms;
36          this.writer = writer;
37          this.model = model;
38      }
39  
40      /**
41       * Returns the name of the getter method.
42       */
43      public String calcGetter() {
44          if (calcFieldTypeToUse(ms.getType()).equals("Boolean")) {
45              if (ms.getShortName().toLowerCase().startsWith("is")) {
46                  return calcInitLower(ms.getShortName());
47              }
48              return "is" + calcInitUpper(ms.getShortName());
49          }
50          return "get" + calcInitUpper(ms.getShortName());
51      }
52  
53      /**
54       * Returns the name of the setter method.
55       */
56      public String calcSetter() {
57          if (calcFieldTypeToUse(ms.getType()).equals("Boolean")) {
58              if (ms.getShortName().toLowerCase().startsWith("is")) {
59                  return "set" + calcInitUpper(ms.getShortName().substring(2));
60              }
61          }
62          return "set" + calcInitUpper(ms.getShortName());
63      }
64  
65      public static String dot2Camel (String name) {
66          StringBuilder sb = new StringBuilder ();
67          boolean upper = true;
68          for (char c : name.toCharArray()) {
69              if (c == '.') {
70                  upper = true;
71                  continue;
72              }
73              if (upper) {
74                  c = Character.toUpperCase(c);
75                  upper = false;
76              }
77              sb.append (c);
78          }
79          return sb.toString();
80      }
81      public static String calcInitUpper(String name) {
82          return name.substring(0, 1).toUpperCase() + name.substring(1);
83      }
84  
85      public static String calcInitLower(String name) {
86          return name.substring(0, 1).toLowerCase() + name.substring(1);
87      }
88  
89      public String calcFieldTypeToUse(String type) {
90          return MessageStructureTypeCalculator.calculate(writer, model, type, type, null);
91      }
92  
93      public static String stripList(String str) {
94          if (str.endsWith("List")) {
95              return str.substring(0, str.length() - "List".length());
96          }
97          return str;
98      }
99  }