StringUtil { public static String join(Iterable<String> parts, String separator) { if (parts == null) { return null; } Iterator<String> iterator = parts.iterator(); if (iterator == null) { return null; } else if (!iterator.hasNext()) { return ""; } else { StringBuilder buf = new StringBuilder(); final String first = iterator.next(); if (first != null) { buf.append(first); } while (iterator.hasNext()) { if (separator != null) { buf.append(separator); } final String part = iterator.next(); if (part != null) { buf.append(part); } } return buf.toString(); } } private  StringUtil(); static String[] filterSystemArgs(String[] args); static String trimSuffix(String str, String suffix); static String join(Iterable<String> parts, String separator); static void toUpperCaseArray(String[] source, String[] target); static String noBlank(String str, String dft); static String dropSuffix(String str, String suffix); static String min(Collection<String> strs); static String max(Collection<String> strs); static String min(String s1, String s2); static String max(String s1, String s2); static String[] subArray(String[] array, int start, int endExclusive); static void appendWithSeparator(StringBuilder src, String append); static String[] splitAndTrim(String str, String splitBy); static String[] split(String str, String splitBy); static String[] splitByComma(String str); static int utf8Length(CharSequence sequence); static boolean equals(String a, String b);  }
@Test public void testJoin() { List<String> stringListNormal = Lists.newArrayList(); List<String> stringListEmpty = Lists.newArrayList(); stringListNormal.add("aaa"); stringListNormal.add("bbb"); stringListNormal.add("ccc"); String joinedNormal = StringUtil.join(stringListNormal, ","); assertEquals("aaa,bbb,ccc", joinedNormal); stringListEmpty.add(""); stringListEmpty.add("aa"); stringListEmpty.add(""); stringListEmpty.add("bb"); stringListEmpty.add(""); String joinedEmpty = StringUtil.join(stringListEmpty, ","); assertEquals(",aa,,bb,", joinedEmpty); }
@Test public void testJoinReturningNonEmptyString() { List<String> arrayList = new ArrayList<String>(); LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(arrayList); linkedHashSet.add(")'<Mw@ZR0IYF_l%*>"); linkedHashSet.add(null); String resultString = StringUtil.join(linkedHashSet, null); assertNotNull(resultString); assertEquals(")'<Mw@ZR0IYF_l%*>", resultString); }
@Test public void testJoinOne() { Vector<String> vector = new Vector<>(); vector.add(null); String resultString = StringUtil.join(vector, "PB"); assertNotNull(resultString); assertEquals("", resultString); }
@Test public void testJoinTwo() { Set<String> set = Locale.CHINESE.getUnicodeLocaleAttributes(); String resultString = StringUtil.join(set, "Op"); assertTrue(set.isEmpty()); assertEquals(0, set.size()); assertNotNull(resultString); assertEquals("", resultString); }
@Test public void testJoinReturningNull() { String string = StringUtil.join((Iterable<String>) null, ")Y>1v&V0GU6a"); assertNull(string); }