Skip to content

Commit

Permalink
🔇 silent changes: add unify function Collection4j #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed May 31, 2024
1 parent de51dff commit 7cfc5e9
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions plugin/src/main/groovy/org/unify4j/common/Collection4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,27 @@ public static <T> Set<T> setOf(T... items) {
return Collections.unmodifiableSet(set);
}

/**
* Converts a collection of items to a string with a specified delimiter between elements.
*
* @param collections the collection of items to be converted to a string
* @param delimiter the delimiter to separate the items in the resulting string
* @return a string representation of the collection with the specified delimiter
*/
public static String toString(Collection<?> collections, String delimiter) {
if (isEmpty(collections)) {
return "";
}
// Use a default delimiter of "," if the provided delimiter is empty or null
delimiter = String4j.isEmpty(delimiter) ? "," : delimiter;
StringBuilder builder = new StringBuilder();
for (Object item : collections) {
builder.append(delimiter);
builder.append(item.toString());
}
return builder.substring(delimiter.length());
}

/**
* Throws UnsupportedOperationException if the list is not of type ArrayList.
*
Expand Down

0 comments on commit 7cfc5e9

Please sign in to comment.