Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.api.java.source; |
44 |
|
45 |
import org.netbeans.api.annotations.common.NonNull; |
46 |
import org.netbeans.api.annotations.common.NullAllowed; |
47 |
import org.netbeans.lib.editor.util.CharSequenceUtilities; |
48 |
|
49 |
/** |
50 |
* |
51 |
* @author Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org> |
52 |
* @since 0.122 |
53 |
*/ |
54 |
public final class CodeStyleUtils { |
55 |
|
56 |
private CodeStyleUtils() { |
57 |
} |
58 |
|
59 |
/** |
60 |
* Add a prefix and suffix to a name. If the prefix or suffix are null, then |
61 |
* the name without prefixes or suffixes will be returned. When you add a |
62 |
* prefix value ending with an alphabetic character, the name will be |
63 |
* capitalized. For example, if the prefix for a name is defined as s, and |
64 |
* the name is "i", then the suggested name will be "sI". |
65 |
* |
66 |
* |
67 |
* @param name the name to add the Prefix and Suffix to. |
68 |
* @param prefix the prefix to add, or null if no prefix should be added. |
69 |
* @param suffix the suffix to add, or null if no suffix should be added. |
70 |
* @return the name with added prefix and suffix |
71 |
* @see #getCapitalizedName(java.lang.CharSequence) |
72 |
*/ |
73 |
@NonNull |
74 |
public static String addPrefixSuffix(@NullAllowed CharSequence name, |
75 |
@NullAllowed String prefix, |
76 |
@NullAllowed String suffix) { |
77 |
StringBuilder sb = new StringBuilder(); |
78 |
boolean capitalize = false; |
79 |
if (prefix != null && prefix.length() > 0) { |
80 |
if (Character.isAlphabetic(prefix.charAt(prefix.length() - 1))) { |
81 |
capitalize = true; |
82 |
} |
83 |
sb.insert(0, prefix); |
84 |
} |
85 |
if (name != null) { |
86 |
sb.append(capitalize ? getCapitalizedName(name) : name); |
87 |
} |
88 |
if (suffix != null) { |
89 |
sb.append(suffix); |
90 |
} |
91 |
return sb.toString(); |
92 |
} |
93 |
|
94 |
/** |
95 |
* Removes a prefix and suffix from a name. When you remove a prefix value |
96 |
* ending with an alphabetic character, the name will be decapitalized. If |
97 |
* the name originally started with _ , they will not be added. |
98 |
* |
99 |
* @param name the name to remove the Prefix and Suffix from. |
100 |
* @param prefix the prefix to remove, or null if no prefix should be |
101 |
* removed. |
102 |
* @param suffix the suffix to remove, or null if no suffix should be |
103 |
* removed. |
104 |
* @return the name without the prefix and suffix |
105 |
* @see #addPrefixSuffix(java.lang.CharSequence, java.lang.String, |
106 |
* java.lang.String) |
107 |
* @see #getDecapitalizedName(java.lang.CharSequence) |
108 |
*/ |
109 |
@NonNull |
110 |
public static String removePrefixSuffix(@NonNull CharSequence name, |
111 |
@NullAllowed String prefix, |
112 |
@NullAllowed String suffix) { |
113 |
StringBuilder sb = new StringBuilder(name); |
114 |
int start = 0; |
115 |
int end = name.length(); |
116 |
boolean decapitalize = false; |
117 |
if (prefix != null && CharSequenceUtilities.startsWith(name, prefix)) { |
118 |
start = prefix.length(); |
119 |
if (Character.isAlphabetic(prefix.charAt(prefix.length() - 1))) { |
120 |
decapitalize = true; |
121 |
} |
122 |
} |
123 |
if (suffix != null && CharSequenceUtilities.endsWith(name, suffix)) { |
124 |
end = end - suffix.length(); |
125 |
} |
126 |
String result = sb.substring(start, end); |
127 |
if(decapitalize) { |
128 |
return getDecapitalizedName(result); |
129 |
} else { |
130 |
return result; |
131 |
} |
132 |
} |
133 |
|
134 |
/** |
135 |
* Capitalize a name following the javabeans specification. This normally |
136 |
* means converting the first character from lower case to upper case, but |
137 |
* in the (unusual) special case when there is more than one character and |
138 |
* the second character is upper case, we leave it alone. Thus "fooBah" |
139 |
* becomes "FooBah" and "x" becomes "X", but "eMail" stays as "eMail". |
140 |
* <p>To stay backwards compatible, if the name starts with _ they will be |
141 |
* removed. |
142 |
* |
143 |
* @param name the name to capitalize |
144 |
* @return the capizalized name |
145 |
*/ |
146 |
@NonNull |
147 |
public static String getCapitalizedName(CharSequence name) { |
148 |
StringBuilder sb = new StringBuilder(name); |
149 |
while (sb.length() > 1 && sb.charAt(0) == '_') { //NOI18N |
150 |
sb.deleteCharAt(0); |
151 |
} |
152 |
|
153 |
//Beans naming convention, #165241 |
154 |
if (sb.length() > 1 && Character.isUpperCase(sb.charAt(1))) { |
155 |
return sb.toString(); |
156 |
} |
157 |
|
158 |
if (sb.length() > 0) { |
159 |
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); |
160 |
} |
161 |
return sb.toString(); |
162 |
} |
163 |
|
164 |
/** |
165 |
* Decapitalize a name to its normal java form. This normally |
166 |
* means converting the first character from upper case to lower case, but |
167 |
* in the (unusual) special case when there is more than one character and |
168 |
* the second character is upper case, we leave it alone. Thus "FooBah" |
169 |
* becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL". |
170 |
* |
171 |
* @param name the name to decapitalize |
172 |
* @return the decapizalized name |
173 |
*/ |
174 |
@NonNull |
175 |
public static String getDecapitalizedName(@NonNull CharSequence name) { |
176 |
//Beans naming convention, #165241 |
177 |
if (name.length() > 1 && (Character.isUpperCase(name.charAt(1)) || |
178 |
Character.isLowerCase(name.charAt(0)))) { |
179 |
return name.toString(); |
180 |
} |
181 |
|
182 |
StringBuilder sb = new StringBuilder(name); |
183 |
if (sb.length() > 0) { |
184 |
sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); |
185 |
} |
186 |
return sb.toString(); |
187 |
} |
188 |
|
189 |
/** |
190 |
* Computes the method name that should be used to read the property value. |
191 |
* |
192 |
* @param fieldName the name of the property |
193 |
* @param isBoolean true if the property is of boolean type |
194 |
* @param isStatic true if the property is static |
195 |
* @param cs the CodeSyle to use |
196 |
* @return the getter name |
197 |
*/ |
198 |
@NonNull |
199 |
public static String computeGetterName(CharSequence fieldName, boolean isBoolean, boolean isStatic, CodeStyle cs) { |
200 |
StringBuilder sb = new StringBuilder(getCapitalizedName(removeFieldPrefixSuffix(fieldName, isStatic, cs))); |
201 |
sb.insert(0, isBoolean && cs.useIsForBooleanGetters() ? "is" : "get"); //NOI18N |
202 |
String getterName = sb.toString(); |
203 |
return getterName; |
204 |
} |
205 |
|
206 |
/** |
207 |
* Computes the method name that should be used to write the property value. |
208 |
* |
209 |
* @param fieldName the name of the property |
210 |
* @param isStatic true if the property is static |
211 |
* @param cs the CodeSyle to use |
212 |
* @return the setter name |
213 |
*/ |
214 |
@NonNull |
215 |
public static String computeSetterName(CharSequence fieldName, boolean isStatic, CodeStyle cs) { |
216 |
StringBuilder name = new StringBuilder(getCapitalizedName(removeFieldPrefixSuffix(fieldName, isStatic, cs))); |
217 |
name.insert(0, "set"); //NOI18N |
218 |
return name.toString(); |
219 |
} |
220 |
|
221 |
private static String removeFieldPrefixSuffix(CharSequence fieldName, boolean isStatic, CodeStyle cs) { |
222 |
return removePrefixSuffix(fieldName, |
223 |
isStatic ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), |
224 |
isStatic ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); |
225 |
} |
226 |
} |