PathUtils { @Nullable public static String concatRelativePaths(String... relativePaths) { StringBuilder result = new StringBuilder(); for (String path : relativePaths) { if (path != null && !path.isEmpty()) { int i0 = 0; int i1 = path.length(); while (i0 < i1 && path.charAt(i0) == '/') { i0++; } while (i1 > i0 && path.charAt(i1-1) == '/') { i1--; } if (i1 > i0) { if (result.length() > 0) { result.append('/'); } result.append(path.substring(i0, i1)); } } } return result.length() == 0 ? null : result.toString(); } private  PathUtils(); static boolean denotesRoot(String path); static boolean denotesCurrent(String element); static boolean denotesParent(String element); static boolean isAbsolute(String path); @NotNull static String getParentPath(String path); @NotNull static String getAncestorPath(String path, int nth); @NotNull static String getName(String path); @NotNull static String dropIndexFromName(@NotNull String name); static int getDepth(String path); @NotNull static Iterable<String> elements(final String path); @NotNull static String concat(String parentPath, String... relativePaths); @NotNull static String concat(String parentPath, String subPath); @Nullable static String concatRelativePaths(String... relativePaths); static boolean isAncestor(String ancestor, String path); @NotNull static String relativize(String parentPath, String path); static int getNextSlash(String path, int index); static void validate(String path); static boolean isValid(String path); static void unifyInExcludes(Set<String> includePaths, Set<String> excludedPaths); static final String ROOT_PATH; static final String ROOT_NAME; }
@Test public void testConcatRelativePaths() { assertNull(PathUtils.concatRelativePaths("", "", "")); assertNull(PathUtils.concatRelativePaths()); assertEquals("a/b/c", PathUtils.concatRelativePaths("a", "b", "c")); assertEquals("a/b/c", PathUtils.concatRelativePaths("a", "b/c")); assertEquals("a/b/c", PathUtils.concatRelativePaths("a/b/c", "")); assertEquals("a/b/c", PathUtils.concatRelativePaths("a/b", "c")); assertEquals("a/b/c", PathUtils.concatRelativePaths("/", "a", "", "b/c/")); }