Skip to content

Commit

Permalink
[aiven] chore: add utils#join back to avoid breaking connectors that …
Browse files Browse the repository at this point in the history
…depend on it
  • Loading branch information
jeqo committed Aug 21, 2024
1 parent ad6bac8 commit 64a1690
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
40 changes: 40 additions & 0 deletions clients/src/main/java/org/apache/kafka/common/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,46 @@ public static String formatBytes(long bytes) {
}
}

/**
* Create a string representation of an array joined by the given separator
* @param strs The array of items
* @param separator The separator
* @return The string representation.
*/
public static <T> String join(T[] strs, String separator) {
return join(Arrays.asList(strs), separator);
}

/**
* Create a string representation of a collection joined by the given separator
* @param collection The list of items
* @param separator The separator
* @return The string representation.
*/
public static <T> String join(Collection<T> collection, String separator) {
Objects.requireNonNull(collection);
return mkString(collection.stream(), "", "", separator);
}

/**
* Create a string representation of a stream surrounded by `begin` and `end` and joined by `separator`.
*
* @return The string representation.
*/
public static <T> String mkString(Stream<T> stream, String begin, String end, String separator) {
Objects.requireNonNull(stream);
StringBuilder sb = new StringBuilder();
sb.append(begin);
Iterator<T> iter = stream.iterator();
while (iter.hasNext()) {
sb.append(iter.next());
if (iter.hasNext())
sb.append(separator);
}
sb.append(end);
return sb.toString();
}

/**
* Converts a {@code Map} class into a string, concatenating keys and values
* Example:
Expand Down
14 changes: 14 additions & 0 deletions clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1043,4 +1043,18 @@ public int hashCode() {
return key.hashCode();
}
}

@Test
public void testJoin() {
assertEquals("", Utils.join(Collections.emptyList(), ","));
assertEquals("1", Utils.join(asList("1"), ","));
assertEquals("1,2,3", Utils.join(asList(1, 2, 3), ","));
}

@Test
public void testMkString() {
assertEquals("[]", Utils.mkString(Stream.empty(), "[", "]", ","));
assertEquals("(1)", Utils.mkString(Stream.of("1"), "(", ")", ","));
assertEquals("{1,2,3}", Utils.mkString(Stream.of(1, 2, 3), "{", "}", ","));
}
}

0 comments on commit 64a1690

Please sign in to comment.