1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.utils; |
23 | |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.expr.UnaryExpr; |
26 | |
27 | import java.io.IOException; |
28 | import java.io.Reader; |
29 | import java.util.*; |
30 | import java.util.function.Function; |
31 | |
32 | import static java.util.Arrays.*; |
33 | |
34 | /** |
35 | * Any kind of utility. |
36 | * |
37 | * @author Federico Tomassetti |
38 | */ |
39 | public class Utils { |
40 | |
41 | /** |
42 | * // TODO: Replace this within the internal codebase. |
43 | * @deprecated New code should use {@link LineSeparator#SYSTEM} if referring to the current host system's line separator, |
44 | * else {@link LineSeparator#CR} or {@link LineSeparator#LF} or {@link LineSeparator#CRLF} if referring to a specific style of line separator. |
45 | */ |
46 | @Deprecated |
47 | public static final String EOL = LineSeparator.SYSTEM.asRawString(); |
48 | |
49 | /** |
50 | * @deprecated Renamed from {@link #EOL} to make it explicit that we're using the system's line separator. |
51 | * New code should use {@link LineSeparator#SYSTEM} if referring to the current host system's line separator, |
52 | * else {@link LineSeparator#CR} or {@link LineSeparator#LF} or {@link LineSeparator#CRLF} if referring to a specific style of line separator. |
53 | * |
54 | */ |
55 | @Deprecated |
56 | public static final String SYSTEM_EOL = LineSeparator.SYSTEM.asRawString(); |
57 | |
58 | public static <E> boolean isNullOrEmpty(Collection<E> collection) { |
59 | return collection == null || collection.isEmpty(); |
60 | } |
61 | |
62 | public static <T> T assertNotNull(T o) { |
63 | if (o == null) { |
64 | throw new AssertionError("A reference was unexpectedly null."); |
65 | } |
66 | return o; |
67 | } |
68 | |
69 | public static String assertNonEmpty(String string) { |
70 | if (string == null || string.isEmpty()) { |
71 | throw new AssertionError("A string was unexpectedly empty."); |
72 | } |
73 | return string; |
74 | } |
75 | |
76 | public static <T extends Number> T assertNonNegative(T number) { |
77 | if (number.longValue() < 0) { |
78 | throw new AssertionError("A number was unexpectedly negative."); |
79 | } |
80 | return number; |
81 | } |
82 | |
83 | public static <T extends Number> T assertPositive(T number) { |
84 | if (number.longValue() <= 0) { |
85 | throw new AssertionError("A number was unexpectedly non-positive."); |
86 | } |
87 | return number; |
88 | } |
89 | |
90 | /** |
91 | * @return string with ASCII characters 10 and 13 replaced by the text "\n" and "\r". |
92 | */ |
93 | public static String escapeEndOfLines(String string) { |
94 | StringBuilder escapedString = new StringBuilder(); |
95 | for (char c : string.toCharArray()) { |
96 | switch (c) { |
97 | case '\n': |
98 | escapedString.append("\\n"); |
99 | break; |
100 | case '\r': |
101 | escapedString.append("\\r"); |
102 | break; |
103 | default: |
104 | escapedString.append(c); |
105 | } |
106 | } |
107 | return escapedString.toString(); |
108 | } |
109 | |
110 | public static String readerToString(Reader reader) throws IOException { |
111 | final StringBuilder result = new StringBuilder(); |
112 | final char[] buffer = new char[8 * 1024]; |
113 | int numChars; |
114 | |
115 | while ((numChars = reader.read(buffer, 0, buffer.length)) > 0) { |
116 | result.append(buffer, 0, numChars); |
117 | } |
118 | |
119 | return result.toString(); |
120 | } |
121 | |
122 | /** |
123 | * @deprecated use screamingToCamelCase |
124 | */ |
125 | @Deprecated |
126 | public static String toCamelCase(String original) { |
127 | return screamingToCamelCase(original); |
128 | } |
129 | |
130 | /** |
131 | * Transform a string to the camel case conversion. |
132 | * <p> |
133 | * For example "ABC_DEF" becomes "abcDef" |
134 | */ |
135 | public static String screamingToCamelCase(String original) { |
136 | StringBuilder sb = new StringBuilder(); |
137 | String[] parts = original.toLowerCase().split("_"); |
138 | for (int i = 0; i < parts.length; i++) { |
139 | sb.append(i == 0 ? parts[i] : capitalize(parts[i])); |
140 | } |
141 | return sb.toString(); |
142 | } |
143 | |
144 | |
145 | /** |
146 | * @param input "aCamelCaseString" |
147 | * @return "A_CAMEL_CASE_STRING" |
148 | */ |
149 | public static String camelCaseToScreaming(String input) { |
150 | if (input.isEmpty()) { |
151 | return ""; |
152 | } |
153 | StringBuilder scream = new StringBuilder(input.substring(0, 1).toUpperCase()); |
154 | for (char c : input.substring(1).toCharArray()) { |
155 | if (Character.isUpperCase(c)) { |
156 | scream.append("_"); |
157 | } |
158 | scream.append(Character.toUpperCase(c)); |
159 | } |
160 | return scream.toString(); |
161 | } |
162 | |
163 | /** |
164 | * Return the next word of the string, in other words it stops when a space is encountered. |
165 | */ |
166 | public static String nextWord(String string) { |
167 | int index = 0; |
168 | while (index < string.length() && !Character.isWhitespace(string.charAt(index))) { |
169 | index++; |
170 | } |
171 | return string.substring(0, index); |
172 | } |
173 | |
174 | /** |
175 | * Make an indent by appending indentLevel tab characters to the builder. |
176 | */ |
177 | public static StringBuilder indent(StringBuilder builder, int indentLevel) { |
178 | for (int i = 0; i < indentLevel; i++) { |
179 | builder.append("\t"); |
180 | } |
181 | return builder; |
182 | } |
183 | |
184 | /** |
185 | * Capitalizes the first character in the string. |
186 | */ |
187 | public static String capitalize(String s) { |
188 | return stringTransformer(s, "capitalize", String::toUpperCase); |
189 | } |
190 | |
191 | /** |
192 | * Lower-cases the first character in the string. |
193 | */ |
194 | public static String decapitalize(String s) { |
195 | return stringTransformer(s, "decapitalize", String::toLowerCase); |
196 | } |
197 | |
198 | private static String stringTransformer(String s, String operationDescription, Function<String, String> transformation) { |
199 | if (s.isEmpty()) { |
200 | throw new IllegalArgumentException(String.format("You cannot %s an empty string", operationDescription)); |
201 | } |
202 | return transformation.apply(s.substring(0, 1)) + |
203 | s.substring(1); |
204 | } |
205 | |
206 | /** |
207 | * @return true if the value is null, an empty Optional, or an empty String. |
208 | */ |
209 | public static boolean valueIsNullOrEmpty(Object value) { |
210 | if (value == null) { |
211 | return true; |
212 | } |
213 | if (value instanceof Optional) { |
214 | if (((Optional) value).isPresent()) { |
215 | value = ((Optional) value).get(); |
216 | } else { |
217 | return true; |
218 | } |
219 | } |
220 | if (value instanceof Collection) { |
221 | if (((Collection) value).isEmpty()) { |
222 | return true; |
223 | } |
224 | } |
225 | return false; |
226 | } |
227 | |
228 | public static boolean valueIsNullOrEmptyStringOrOptional(Object value) { |
229 | if (value == null) { |
230 | return true; |
231 | } |
232 | if (value instanceof Optional) { |
233 | if (((Optional) value).isPresent()) { |
234 | value = ((Optional) value).get(); |
235 | } else { |
236 | return true; |
237 | } |
238 | } |
239 | return false; |
240 | } |
241 | |
242 | /** |
243 | * Like {@link List#set(int, Object)} at {@link List#indexOf(Object)}, but using ==, not equals. |
244 | */ |
245 | public static <E> void replaceElementByObjectIdentity(List<E> list, E oldObject, E newObject) { |
246 | int index = indexOfElementByObjectIdentity(list, oldObject); |
247 | if (index == -1) { |
248 | return; |
249 | } |
250 | list.set(index, newObject); |
251 | } |
252 | |
253 | /** |
254 | * Like {@link List#remove(Object)}, but using ==, not equals. |
255 | */ |
256 | public static <E> void removeElementByObjectIdentity(List<E> list, E o) { |
257 | int index = indexOfElementByObjectIdentity(list, o); |
258 | if (index == -1) { |
259 | return; |
260 | } |
261 | list.remove(index); |
262 | } |
263 | |
264 | /** |
265 | * Like {@link List#indexOf(Object)}, but using ==, not equals. |
266 | */ |
267 | public static <E> int indexOfElementByObjectIdentity(List<E> list, E o) { |
268 | for (int i = 0; i < list.size(); i++) { |
269 | Object listO = list.get(i); |
270 | if (o == listO) { |
271 | return i; |
272 | } |
273 | } |
274 | return -1; |
275 | } |
276 | |
277 | /** |
278 | * @return a set of the items. |
279 | */ |
280 | @SafeVarargs |
281 | public static <T> Set<T> set(T... items) { |
282 | return new HashSet<>(asList(items)); |
283 | } |
284 | |
285 | /** |
286 | * @return content, with all kinds of EOL characters replaced by desiredEndOfLineCharacter |
287 | */ |
288 | public static String normalizeEolInTextBlock(String content, String desiredEndOfLineCharacter) { |
289 | return content.replaceAll("\\R", desiredEndOfLineCharacter); |
290 | } |
291 | |
292 | /** |
293 | * @return content, with all kinds of EOL characters replaced by desiredEndOfLineCharacter |
294 | */ |
295 | public static String normalizeEolInTextBlock(String content, LineSeparator desiredEndOfLineCharacter) { |
296 | return normalizeEolInTextBlock(content, desiredEndOfLineCharacter.asRawString()); |
297 | } |
298 | |
299 | /** |
300 | * @return the filename with the last "." and everything following it removed. |
301 | */ |
302 | public static String removeFileExtension(String filename) { |
303 | int extensionIndex = filename.lastIndexOf("."); |
304 | if (extensionIndex == -1) |
305 | return filename; |
306 | |
307 | return filename.substring(0, extensionIndex); |
308 | } |
309 | |
310 | /** |
311 | * Like {@link String#trim()}, but only the trailing spaces. |
312 | */ |
313 | public static String trimTrailingSpaces(String line) { |
314 | while (line.length() > 0 && line.charAt(line.length() - 1) <= 0x20) { |
315 | line = line.substring(0, line.length() - 1); |
316 | } |
317 | return line; |
318 | } |
319 | |
320 | /** |
321 | * Checks, if the parent is a unary expression with a minus operator. Used to check for negative literals. |
322 | */ |
323 | public static boolean hasUnaryMinusAsParent(Node n) { |
324 | return n.getParentNode() |
325 | .filter(parent -> parent instanceof UnaryExpr) |
326 | .map(parent -> (UnaryExpr) parent) |
327 | .map(unaryExpr -> unaryExpr.getOperator() == UnaryExpr.Operator.MINUS) |
328 | .orElse(false); |
329 | } |
330 | |
331 | } |
332 |
Members