/** * Returns an array containing the names of the given types. * * @param types the given types. * @return the names of the given types stored in an array. */ public static String[] namesOf(Class<?>... types) { if (isNullOrEmpty(types)) { return new String[0]; } String[] names = new String[types.length]; for (int i = 0; i < types.length; i++) { names[i] = types[i].getName(); } return names; }
/** * Concatenates the given objects into a single {@code String}. This method is more efficient than concatenating using * "+", since only one <code>{@link StringBuilder}</code> is created. * * @param objects the objects to concatenate. * @return a {@code String} containing the given objects. */ public static String concat(Object... objects) { if (Arrays.isNullOrEmpty(objects)) { return null; } StringBuilder b = new StringBuilder(); for (Object o : objects) { b.append(o); } return b.toString(); }
/** * Specifies the delimeter to use to join {@code String}s. * * @param delimeter the delimeter to use. * @return the {@code String}s joined using the given delimeter. */ public String with(String delimeter) { if (delimeter == null) { throw new IllegalArgumentException("Delimiter should not be null"); } if (Arrays.isNullOrEmpty(strings)) { return ""; } StringBuilder b = new StringBuilder(); int stringCount = strings.length; for (int i = 0; i < stringCount; i++) { String s = strings[i]; b.append(s != null ? s : ""); if (i < stringCount - 1) { b.append(delimeter); } } return b.toString(); } }
/** * Returns the names of the files inside the specified directory. * * @param dir the name of the directory to start the search from. * @param recurse if {@code true}, we will look in subdirectories. * @return the names of the files inside the specified directory. */ private static List<String> fileNamesIn(File dir, boolean recurse) { List<String> scriptNames = new ArrayList<String>(); File[] existingFiles = dir.listFiles(); if (isNullOrEmpty(existingFiles)) { return scriptNames; } for (File existingFile : existingFiles) { if (existingFile.isDirectory()) { if (recurse) { scriptNames.addAll(fileNamesIn(existingFile, recurse)); } continue; } String filename = existingFile.getAbsolutePath(); if (!scriptNames.contains(filename)) { scriptNames.add(filename); } } return scriptNames; }
/** * Creates a new file using the given path. * * @param path the path of the new file. * @return the new created file. * @throws FilesException if the path belongs to an existing non-empty directory. * @throws FilesException if the path belongs to an existing file. * @throws FilesException if any I/O error is thrown when creating the new file. */ public static File newFile(String path) { File file = new File(path); if (file.isDirectory() && !isNullOrEmpty(file.list())) { throw cannotCreateNewFile(path, "a non-empty directory was found with the same path"); } try { if (!file.createNewFile()) { throw cannotCreateNewFile(path, "a file was found with the same path"); } } catch (IOException e) { throw cannotCreateNewFile(path, e); } return file; }
/** * Creates a new directory using the given path. * * @param path the path of the new directory. * @return the new created directory. * @throws FilesException if the path belongs to an existing non-empty directory. * @throws FilesException if the path belongs to an existing file. * @throws FilesException if any I/O error is thrown when creating the new directory. */ public static File newFolder(String path) { File file = new File(path); if (file.isDirectory() && !isNullOrEmpty(file.list())) { throw cannotCreateNewFile(path, "a non-empty directory was found with the same path"); } try { if (!file.mkdir()) { throw cannotCreateNewFile(path, "a file was found with the same path"); } } catch (Exception e) { throw cannotCreateNewFile(path, e); } return file; }