Class Joiner
java.lang.Object
cloud.opencode.base.core.Joiner
Joiner - Fluent string joining utility
连接器 - 流式字符串连接工具
Provides a fluent API for joining strings with various options for null handling and formatting.
提供用于连接字符串的流式 API,支持空值处理和格式化等选项。
Usage Examples | 使用示例:
// Basic joining
String result = Joiner.on(',').join("a", "b", "c"); // "a,b,c"
// Join with list
String result = Joiner.on(", ").join(List.of("a", "b", "c")); // "a, b, c"
// Skip nulls
String result = Joiner.on(',').skipNulls().join("a", null, "b"); // "a,b"
// Replace nulls
String result = Joiner.on(',').useForNull("N/A").join("a", null, "b"); // "a,N/A,b"
// Join to existing StringBuilder
StringBuilder sb = new StringBuilder("prefix: ");
Joiner.on(',').appendTo(sb, "a", "b", "c"); // "prefix: a,b,c"
// Join map entries
Map<String, Integer> map = Map.of("a", 1, "b", 2);
String result = Joiner.on(',').withKeyValueSeparator('=').join(map); // "a=1,b=2"
Features | 主要功能:
- Fluent API with separator configuration - 支持分隔符配置的流式API
- Null handling: skip nulls or use default text - 空值处理: 跳过或替换
- Join to StringBuilder or Appendable - 连接到StringBuilder或Appendable
- Map entry joining with key-value separator - 使用键值分隔符连接Map条目
Security | 安全性:
- Thread-safe: Yes (immutable after creation) - 线程安全: 是(创建后不可变)
- Null-safe: Yes, with skipNulls() or useForNull() - 空值安全: 是,通过skipNulls()或useForNull()
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA joiner that produces strings from map entries. -
Method Summary
Modifier and TypeMethodDescription<A extends Appendable>
AAppends the joined string to the given appendable.<A extends Appendable>
AAppends the joined string to the given appendable.<A extends Appendable>
AAppends the joined string to the given appendable.appendTo(StringBuilder sb, Iterable<?> parts) Appends the joined string to the given StringBuilder.appendTo(StringBuilder sb, Object... parts) Appends the joined string to the given StringBuilder.Joins the given iterable into a string.Joins the given objects into a string.Joins the given iterator into a string.static Joineron(char separator) Creates a joiner that uses the given separator.static JoinerCreates a joiner that uses the given separator.Returns a joiner that skips null values.useForNull(String nullText) Returns a joiner that replaces null values with the given text.withFormatter(Function<Object, String> formatter) Returns a joiner that formats values using the given function.withKeyValueSeparator(char separator) Returns a map joiner using the given key-value separator.withKeyValueSeparator(String separator) Returns a map joiner using the given key-value separator.
-
Method Details
-
on
Creates a joiner that uses the given separator. 创建使用给定分隔符的连接器。- Parameters:
separator- the separator character - 分隔符字符- Returns:
- the joiner - 连接器
-
on
-
skipNulls
Returns a joiner that skips null values. 返回跳过空值的连接器。- Returns:
- the joiner - 连接器
-
useForNull
-
withFormatter
-
join
-
join
-
join
-
appendTo
Appends the joined string to the given appendable. 将连接后的字符串追加到给定的可追加对象。- Type Parameters:
A- the appendable type - 可追加类型- Parameters:
appendable- the appendable - 可追加对象parts- the parts to join - 要连接的部分- Returns:
- the appendable - 可追加对象
- Throws:
IOException- if an I/O error occurs - 如果发生 I/O 错误
-
appendTo
Appends the joined string to the given appendable. 将连接后的字符串追加到给定的可追加对象。- Type Parameters:
A- the appendable type - 可追加类型- Parameters:
appendable- the appendable - 可追加对象parts- the parts to join - 要连接的部分- Returns:
- the appendable - 可追加对象
- Throws:
IOException- if an I/O error occurs - 如果发生 I/O 错误
-
appendTo
Appends the joined string to the given appendable. 将连接后的字符串追加到给定的可追加对象。- Type Parameters:
A- the appendable type - 可追加类型- Parameters:
appendable- the appendable - 可追加对象parts- the parts to join - 要连接的部分- Returns:
- the appendable - 可追加对象
- Throws:
IOException- if an I/O error occurs - 如果发生 I/O 错误
-
appendTo
Appends the joined string to the given StringBuilder. 将连接后的字符串追加到给定的 StringBuilder。- Parameters:
sb- the StringBuilder - StringBuilderparts- the parts to join - 要连接的部分- Returns:
- the StringBuilder - StringBuilder
-
appendTo
Appends the joined string to the given StringBuilder. 将连接后的字符串追加到给定的 StringBuilder。- Parameters:
sb- the StringBuilder - StringBuilderparts- the parts to join - 要连接的部分- Returns:
- the StringBuilder - StringBuilder
-
withKeyValueSeparator
Returns a map joiner using the given key-value separator. 返回使用给定键值分隔符的映射连接器。- Parameters:
separator- the key-value separator - 键值分隔符- Returns:
- the map joiner - 映射连接器
-
withKeyValueSeparator
Returns a map joiner using the given key-value separator. 返回使用给定键值分隔符的映射连接器。- Parameters:
separator- the key-value separator - 键值分隔符- Returns:
- the map joiner - 映射连接器
-