View Javadoc

1   /*
2   
3       dsh-codegen  Source code generation suite.
4       Copyright (c) 2004-2011 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   * @version $Revision: 993 $ $Date: 2011-02-25 14:18:11 -0600 (Fri, 25 Feb 2011) $
34   */
35  public final class CodegenUtils
36  {
37      /** List of Java primitive names. */
38      private static final List<String> PRIMITIVE_NAMES = Arrays.asList(new String[] { "byte", "short", "int", "long", "char", "float", "double", "boolean" });
39  
40  
41      /**
42       * Return true if the specified name is a Java primitive name.
43       *
44       * @return true if the specified name is a primitive name
45       */
46      public static boolean isPrimitive(final String name)
47      {
48          return PRIMITIVE_NAMES.contains(name);
49      }
50  
51      /**
52       * Make a lowercase name from the specified name.
53       *
54       * @param name name
55       * @return a lowercase name
56       */
57      public static String makeLowercase(final String name)
58      {
59          return makeDescription(name);
60      }
61  
62      /**
63       * Make an Uppercase name from the specified name.
64       *
65       * @param name name
66       * @return an Uppercase name
67       */
68      public static String makeUppercase(final String name)
69      {
70          // names should already be in uppercase form
71          return name;
72      }
73  
74      /**
75       * Make a mixedCase name from the specified name.
76       *
77       * @param name name
78       * @return a mixedCase name
79       */
80      public static String makeMixedCase(final String name)
81      {
82          StringBuffer sb = new StringBuffer();
83          char ch = name.charAt(0);
84          sb.append(Character.toLowerCase(ch));
85          sb.append(name.substring(1));
86          return sb.toString();
87      }
88  
89      /**
90       * Make a description (all lowercase, words split by spaces) from
91       * the specified name.
92       *
93       * @param name name
94       * @return a description
95       */
96      public static String makeDescription(final String name)
97      {
98          StringBuffer sb = new StringBuffer();
99          for (int i = 0, size = name.length(); i < size; i++)
100         {
101             char ch = name.charAt(i);
102             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
103             {
104                 if (i != 0)
105                 {
106                     sb.append(" ");
107                 }
108                 sb.append(Character.toLowerCase(ch));
109             }
110             else
111             {
112                 sb.append(ch);
113             }
114         }
115         return sb.toString();
116     }
117 
118     /**
119      * Make a sentence-case description (first word uppercase, rest lowercase, words split by spaces) from
120      * the specified name.
121      *
122      * @param name name
123      * @return a sentence-case description
124      */
125     public static String makeSentenceCaseDescription(final String name)
126     {
127         StringBuffer sb = new StringBuffer();
128         for (int i = 0, size = name.length(); i < size; i++)
129         {
130             char ch = name.charAt(i);
131             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
132             {
133                 if (i == 0)
134                 {
135                     sb.append(ch);
136                 }
137                 else
138                 {
139                     sb.append(" ");
140                     sb.append(Character.toLowerCase(ch));
141                 }
142             }
143             else
144             {
145                 sb.append(ch);
146             }
147         }
148         return sb.toString();
149     }
150 
151     /**
152      * Make a lowercase-with-dashes name from the specified name.
153      *
154      * @param name name
155      * @return a lowercase-with-dashes name
156      */
157     public static String makeLowercaseWithDashes(final String name)
158     {
159         StringBuffer sb = new StringBuffer();
160         for (int i = 0, size = name.length(); i < size; i++)
161         {
162             char ch = name.charAt(i);
163             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
164             {
165                 if (i != 0)
166                 {
167                     sb.append("-");
168                 }
169                 sb.append(Character.toLowerCase(ch));
170             }
171             else
172             {
173                 sb.append(ch);
174             }
175         }
176         return sb.toString();
177     }
178 
179     /**
180      * Make an UPPERCASE_WITH_UNDERSCORES name from the specified name.
181      *
182      * @param name name
183      * @return an UPPERCASE_WITH_UNDERSCORES name
184      */
185     public static String makeUppercaseWithUnderscores(final String name)
186     {
187         StringBuffer sb = new StringBuffer();
188         for (int i = 0, size = name.length(); i < size; i++)
189         {
190             char ch = name.charAt(i);
191             if (Character.isTitleCase(ch) || Character.isUpperCase(ch))
192             {
193                 if (i != 0)
194                 {
195                     sb.append("_");
196                 }
197                 sb.append(ch);
198             }
199             else
200             {
201                 sb.append(Character.toUpperCase(ch));
202             }
203         }
204         return sb.toString();
205     }
206 }