JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/utils/Utils.java
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
22package com.github.javaparser.utils;
23
24import com.github.javaparser.ast.Node;
25import com.github.javaparser.ast.expr.UnaryExpr;
26
27import java.io.IOException;
28import java.io.Reader;
29import java.util.*;
30import java.util.function.Function;
31
32import static java.util.Arrays.*;
33
34/**
35 * Any kind of utility.
36 *
37 * @author Federico Tomassetti
38 */
39public 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 <Eboolean isNullOrEmpty(Collection<Ecollection) {
59        return collection == null || collection.isEmpty();
60    }
61
62    public static <TT 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 NumberT 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 NumberT 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(buffer0buffer.length)) > 0) {
116            result.append(buffer0numChars);
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 = 0i < parts.lengthi++) {
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(01).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(0index);
172    }
173
174    /**
175     * Make an indent by appending indentLevel tab characters to the builder.
176     */
177    public static StringBuilder indent(StringBuilder builderint indentLevel) {
178        for (int i = 0i < indentLeveli++) {
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 sString operationDescriptionFunction<StringStringtransformation) {
199        if (s.isEmpty()) {
200            throw new IllegalArgumentException(String.format("You cannot %s an empty string"operationDescription));
201        }
202        return transformation.apply(s.substring(01)) +
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 (((Optionalvalue).isPresent()) {
215                value = ((Optionalvalue).get();
216            } else {
217                return true;
218            }
219        }
220        if (value instanceof Collection) {
221            if (((Collectionvalue).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 (((Optionalvalue).isPresent()) {
234                value = ((Optionalvalue).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 <Evoid replaceElementByObjectIdentity(List<ElistE oldObjectE newObject) {
246        int index = indexOfElementByObjectIdentity(listoldObject);
247        if (index == -1) {
248            return;
249        }
250        list.set(indexnewObject);
251    }
252
253    /**
254     * Like {@link List#remove(Object)}, but using ==, not equals.
255     */
256    public static <Evoid removeElementByObjectIdentity(List<ElistE o) {
257        int index = indexOfElementByObjectIdentity(listo);
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 <Eint indexOfElementByObjectIdentity(List<ElistE o) {
268        for (int i = 0i < 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 <TSet<Tset(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 contentString 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 contentLineSeparator desiredEndOfLineCharacter) {
296        return normalizeEolInTextBlock(contentdesiredEndOfLineCharacter.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(0extensionIndex);
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(0line.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 -> (UnaryExprparent)
327                .map(unaryExpr -> unaryExpr.getOperator() == UnaryExpr.Operator.MINUS)
328                .orElse(false);
329    }
330
331}
332
MembersX
Utils:screamingToCamelCase
Utils:SYSTEM_EOL
Utils:assertNonNegative
Utils:valueIsNullOrEmpty
Utils:EOL
Utils:set
Utils:escapeEndOfLines
Utils:readerToString:Block:result
Utils:toCamelCase
Utils:indent
Utils:assertNonEmpty
Utils:readerToString:Block:buffer
Utils:normalizeEolInTextBlock
Utils:screamingToCamelCase:Block:parts
Utils:escapeEndOfLines:Block:escapedString
Utils:assertNotNull
Utils:isNullOrEmpty
Utils:camelCaseToScreaming
Utils:stringTransformer
Utils:replaceElementByObjectIdentity
Utils:camelCaseToScreaming:Block:scream
Utils:screamingToCamelCase:Block:sb
Utils:indexOfElementByObjectIdentity
Utils:removeElementByObjectIdentity
Utils:assertPositive
Utils:trimTrailingSpaces
Utils:decapitalize
Utils:removeFileExtension
Utils:capitalize
Utils:nextWord:Block:index
Utils:readerToString
Utils:readerToString:Block:numChars
Utils:replaceElementByObjectIdentity:Block:index
Utils:removeElementByObjectIdentity:Block:index
Utils:hasUnaryMinusAsParent
Utils:nextWord
Utils:removeFileExtension:Block:extensionIndex
Utils:indexOfElementByObjectIdentity:Block:Block:listO
Utils:valueIsNullOrEmptyStringOrOptional
Members
X