1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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 java.io.IOException; 25 import java.io.Reader; 26 import java.util.*; 27 import java.util.function.Predicate; 28 import java.util.function.Function; 29 30 import static java.util.Arrays.*; 31 32 /** 33 * Any kind of utility. 34 * 35 * @author Federico Tomassetti 36 */ 37 public class Utils { 38 public static final String EOL = System.getProperty("line.separator"); 39 40 public static final Predicate<String> STRING_NOT_EMPTY = s -> !s.isEmpty(); 41 42 /** 43 * @deprecated This is no longer in use by JavaParser, please write your own replacement. 44 */ ensureNotNull(List<T> list)45 public static <T> List<T> ensureNotNull(List<T> list) { 46 return list == null ? new ArrayList<>() : list; 47 } 48 isNullOrEmpty(Collection<E> collection)49 public static <E> boolean isNullOrEmpty(Collection<E> collection) { 50 return collection == null || collection.isEmpty(); 51 } 52 assertNotNull(T o)53 public static <T> T assertNotNull(T o) { 54 if (o == null) { 55 throw new AssertionError("A reference was unexpectedly null."); 56 } 57 return o; 58 } 59 assertNonEmpty(String string)60 public static String assertNonEmpty(String string) { 61 if (string == null || string.isEmpty()) { 62 throw new AssertionError("A string was unexpectedly empty."); 63 } 64 return string; 65 } 66 67 /** 68 * @return string with ASCII characters 10 and 13 replaced by the text "\n" and "\r". 69 */ escapeEndOfLines(String string)70 public static String escapeEndOfLines(String string) { 71 StringBuilder escapedString = new StringBuilder(); 72 for (char c : string.toCharArray()) { 73 switch (c) { 74 case '\n': 75 escapedString.append("\\n"); 76 break; 77 case '\r': 78 escapedString.append("\\r"); 79 break; 80 default: 81 escapedString.append(c); 82 } 83 } 84 return escapedString.toString(); 85 } 86 readerToString(Reader reader)87 public static String readerToString(Reader reader) throws IOException { 88 final StringBuilder result = new StringBuilder(); 89 final char[] buffer = new char[8 * 1024]; 90 int numChars; 91 92 while ((numChars = reader.read(buffer, 0, buffer.length)) > 0) { 93 result.append(buffer, 0, numChars); 94 } 95 96 return result.toString(); 97 } 98 99 /** 100 * Puts varargs in a mutable list. 101 * This does not have the disadvantage of Arrays#asList that it has a static size. 102 * 103 * @deprecated This is no longer in use by JavaParser, please write your own replacement. 104 */ 105 @Deprecated arrayToList(T[] array)106 public static <T> List<T> arrayToList(T[] array) { 107 List<T> list = new LinkedList<>(); 108 Collections.addAll(list, array); 109 return list; 110 } 111 112 /** 113 * @deprecated use screamingToCamelCase 114 */ toCamelCase(String original)115 public static String toCamelCase(String original) { 116 return screamingToCamelCase(original); 117 } 118 119 /** 120 * Transform a string to the camel case conversion. 121 * <p> 122 * For example "ABC_DEF" becomes "abcDef" 123 */ screamingToCamelCase(String original)124 public static String screamingToCamelCase(String original) { 125 StringBuilder sb = new StringBuilder(); 126 String[] parts = original.toLowerCase().split("_"); 127 for (int i = 0; i < parts.length; i++) { 128 sb.append(i == 0 ? parts[i] : capitalize(parts[i])); 129 } 130 return sb.toString(); 131 } 132 133 134 /** 135 * @param input "aCamelCaseString" 136 * @return "A_CAMEL_CASE_STRING" 137 */ camelCaseToScreaming(String input)138 public static String camelCaseToScreaming(String input) { 139 if (input.isEmpty()) { 140 return ""; 141 } 142 StringBuilder scream = new StringBuilder(input.substring(0, 1).toUpperCase()); 143 for (char c : input.substring(1).toCharArray()) { 144 if (Character.isUpperCase(c)) { 145 scream.append("_"); 146 } 147 scream.append(Character.toUpperCase(c)); 148 } 149 return scream.toString(); 150 } 151 152 /** 153 * Return the next word of the string, in other words it stops when a space is encountered. 154 */ nextWord(String string)155 public static String nextWord(String string) { 156 int index = 0; 157 while (index < string.length() && !Character.isWhitespace(string.charAt(index))) { 158 index++; 159 } 160 return string.substring(0, index); 161 } 162 163 /** 164 * Make an indent by appending indentLevel tab characters to the builder. 165 */ indent(StringBuilder builder, int indentLevel)166 public static StringBuilder indent(StringBuilder builder, int indentLevel) { 167 for (int i = 0; i < indentLevel; i++) { 168 builder.append("\t"); 169 } 170 return builder; 171 } 172 173 /** 174 * Capitalizes the first character in the string. 175 */ capitalize(String s)176 public static String capitalize(String s) { 177 return stringTransformer(s, "capitalize", String::toUpperCase); 178 } 179 180 /** 181 * Lower-cases the first character in the string. 182 */ decapitalize(String s)183 public static String decapitalize(String s) { 184 return stringTransformer(s, "decapitalize", String::toLowerCase); 185 } 186 stringTransformer(String s, String operationDescription, Function<String, String> transformation)187 private static String stringTransformer(String s, String operationDescription, Function<String, String> transformation) { 188 if (s.isEmpty()) { 189 throw new IllegalArgumentException(String.format("You cannot %s an empty string", operationDescription)); 190 } 191 return transformation.apply(s.substring(0, 1)) + 192 s.substring(1); 193 } 194 195 /** 196 * @return true if the value is null, an empty Optional, or an empty String. 197 */ valueIsNullOrEmpty(Object value)198 public static boolean valueIsNullOrEmpty(Object value) { 199 if (value == null) { 200 return true; 201 } 202 if (value instanceof Optional) { 203 if (((Optional) value).isPresent()) { 204 value = ((Optional) value).get(); 205 } else { 206 return true; 207 } 208 } 209 if (value instanceof Collection) { 210 if (((Collection) value).isEmpty()) { 211 return true; 212 } 213 } 214 return false; 215 } 216 217 /** 218 * @return a set of the items. 219 */ set(T... items)220 public static <T> Set<T> set(T... items) { 221 return new HashSet<>(asList(items)); 222 } 223 224 /** 225 * @return content with all kinds of EOL characters replaced by endOfLineCharacter 226 */ normalizeEolInTextBlock(String content, String endOfLineCharacter)227 public static String normalizeEolInTextBlock(String content, String endOfLineCharacter) { 228 return content 229 .replaceAll("\\R", endOfLineCharacter); 230 } 231 232 /** 233 * @return the filename with the last "." and everything following it removed. 234 */ removeFileExtension(String filename)235 public static String removeFileExtension(String filename) { 236 int extensionIndex = filename.lastIndexOf("."); 237 if (extensionIndex == -1) 238 return filename; 239 240 return filename.substring(0, extensionIndex); 241 } 242 243 /** 244 * Like {@link String#trim()}, but only the trailing spaces. 245 */ trimTrailingSpaces(String line)246 public static String trimTrailingSpaces(String line) { 247 while (line.length() > 0 && line.charAt(line.length() - 1) <= 0x20) { 248 line = line.substring(0, line.length() - 1); 249 } 250 return line; 251 } 252 253 } 254