View Javadoc

1   /*
2   
3       dsh-codegen  Source code generation suite.
4       Copyright (c) 2004-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.codegen;
25  
26  import java.util.Arrays;
27  import java.util.List;
28  
29  /**
30   * Static utility methods for the codegen package.
31   *
32   * @author  Michael Heuer
33   */
34  public final class CodegenUtils
35  {
36      /** List of Java primitive names. */
37      private static final List<String> PRIMITIVE_NAMES = Arrays.asList(new String[] { "byte", "short", "int", "long", "char", "float", "double", "boolean" });
38  
39  
40      /**
41       * Return true if the specified name is a Java primitive name.
42       *
43       * @return true if the specified name is a primitive name
44       */
45      public static boolean isPrimitive(final String name)
46      {
47          return PRIMITIVE_NAMES.contains(name);
48      }
49  
50      /**
51       * Make a lowercase name from the specified name.
52       *
53       * @param name name
54       * @return a lowercase name
55       */
56      public static String makeLowercase(final String name)
57      {
58          return makeDescription(name);
59      }
60  
61      /**
62       * Make an Uppercase name from the specified name.
63       *
64       * @param name name
65       * @return an Uppercase name
66       */
67      public static String makeUppercase(final String name)
68      {
69          // names should already be in uppercase form
70          return name;
71      }
72  
73      /**
74       * Make a mixedCase name from the specified name.
75       *
76       * @param name name
77       * @return a mixedCase name
78       */
79      public static String makeMixedCase(final String name)
80      {
81          StringBuffer sb = new StringBuffer();
82          char ch = name.charAt(0);
83          sb.append(Character.toLowerCase(ch));
84          sb.append(name.substring(1));
85          return sb.toString();
86      }
87  
88      /**
89       * Make a description (all lowercase, words split by spaces) from
90       * the specified name.
91       *
92       * @param name name
93       * @return a description
94       */
95      public static String makeDescription(final String name)
96      {
97          StringBuffer sb = new StringBuffer();
98          for (int i = 0, size = name.length(); i < size; i++)
99          {
100             char ch = name.charAt(i);
101             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
102             {
103                 if (i != 0)
104                 {
105                     sb.append(" ");
106                 }
107                 sb.append(Character.toLowerCase(ch));
108             }
109             else
110             {
111                 sb.append(ch);
112             }
113         }
114         return sb.toString();
115     }
116 
117     /**
118      * Make a sentence-case description (first word uppercase, rest lowercase, words split by spaces) from
119      * the specified name.
120      *
121      * @param name name
122      * @return a sentence-case description
123      */
124     public static String makeSentenceCaseDescription(final String name)
125     {
126         StringBuffer sb = new StringBuffer();
127         for (int i = 0, size = name.length(); i < size; i++)
128         {
129             char ch = name.charAt(i);
130             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
131             {
132                 if (i == 0)
133                 {
134                     sb.append(ch);
135                 }
136                 else
137                 {
138                     sb.append(" ");
139                     sb.append(Character.toLowerCase(ch));
140                 }
141             }
142             else
143             {
144                 sb.append(ch);
145             }
146         }
147         return sb.toString();
148     }
149 
150     /**
151      * Make a lowercase-with-dashes name from the specified name.
152      *
153      * @param name name
154      * @return a lowercase-with-dashes name
155      */
156     public static String makeLowercaseWithDashes(final String name)
157     {
158         StringBuffer sb = new StringBuffer();
159         for (int i = 0, size = name.length(); i < size; i++)
160         {
161             char ch = name.charAt(i);
162             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
163             {
164                 if (i != 0)
165                 {
166                     sb.append("-");
167                 }
168                 sb.append(Character.toLowerCase(ch));
169             }
170             else
171             {
172                 sb.append(ch);
173             }
174         }
175         return sb.toString();
176     }
177 
178     /**
179      * Make an UPPERCASE_WITH_UNDERSCORES name from the specified name.
180      *
181      * @param name name
182      * @return an UPPERCASE_WITH_UNDERSCORES name
183      */
184     public static String makeUppercaseWithUnderscores(final String name)
185     {
186         StringBuffer sb = new StringBuffer();
187         for (int i = 0, size = name.length(); i < size; i++)
188         {
189             char ch = name.charAt(i);
190             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
191             {
192                 if (i != 0)
193                 {
194                     sb.append("_");
195                 }
196                 sb.append(ch);
197             }
198             else
199             {
200                 sb.append(Character.toUpperCase(ch));
201             }
202         }
203         return sb.toString();
204     }
205 }