Class Joiner

java.lang.Object
cloud.opencode.base.core.Joiner

public final class Joiner extends Object
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:
  • OpenCode.cloud

    For an extended version with CharMatcher support, see cloud.opencode.base.string.builder.Joiner in opencode-base-string.

  • Splitter
  • Method Details

    • on

      public static Joiner on(char separator)
      Creates a joiner that uses the given separator. 创建使用给定分隔符的连接器。
      Parameters:
      separator - the separator character - 分隔符字符
      Returns:
      the joiner - 连接器
    • on

      public static Joiner on(String separator)
      Creates a joiner that uses the given separator. 创建使用给定分隔符的连接器。
      Parameters:
      separator - the separator string - 分隔符字符串
      Returns:
      the joiner - 连接器
    • skipNulls

      public Joiner skipNulls()
      Returns a joiner that skips null values. 返回跳过空值的连接器。
      Returns:
      the joiner - 连接器
    • useForNull

      public Joiner useForNull(String nullText)
      Returns a joiner that replaces null values with the given text. 返回用给定文本替换空值的连接器。
      Parameters:
      nullText - the text to use for nulls - 用于空值的文本
      Returns:
      the joiner - 连接器
    • withFormatter

      public Joiner withFormatter(Function<Object,String> formatter)
      Returns a joiner that formats values using the given function. 返回使用给定函数格式化值的连接器。
      Parameters:
      formatter - the formatter function - 格式化函数
      Returns:
      the joiner - 连接器
    • join

      public String join(Object... parts)
      Joins the given objects into a string. 将给定对象连接成字符串。
      Parameters:
      parts - the parts to join - 要连接的部分
      Returns:
      the joined string - 连接后的字符串
    • join

      public String join(Iterable<?> parts)
      Joins the given iterable into a string. 将给定的可迭代对象连接成字符串。
      Parameters:
      parts - the parts to join - 要连接的部分
      Returns:
      the joined string - 连接后的字符串
    • join

      public String join(Iterator<?> parts)
      Joins the given iterator into a string. 将给定的迭代器连接成字符串。
      Parameters:
      parts - the parts to join - 要连接的部分
      Returns:
      the joined string - 连接后的字符串
    • appendTo

      public <A extends Appendable> A appendTo(A appendable, Object... parts) throws IOException
      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

      public <A extends Appendable> A appendTo(A appendable, Iterable<?> parts) throws IOException
      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

      public <A extends Appendable> A appendTo(A appendable, Iterator<?> parts) throws IOException
      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

      public StringBuilder appendTo(StringBuilder sb, Object... parts)
      Appends the joined string to the given StringBuilder. 将连接后的字符串追加到给定的 StringBuilder。
      Parameters:
      sb - the StringBuilder - StringBuilder
      parts - the parts to join - 要连接的部分
      Returns:
      the StringBuilder - StringBuilder
    • appendTo

      public StringBuilder appendTo(StringBuilder sb, Iterable<?> parts)
      Appends the joined string to the given StringBuilder. 将连接后的字符串追加到给定的 StringBuilder。
      Parameters:
      sb - the StringBuilder - StringBuilder
      parts - the parts to join - 要连接的部分
      Returns:
      the StringBuilder - StringBuilder
    • withKeyValueSeparator

      public Joiner.MapJoiner withKeyValueSeparator(char separator)
      Returns a map joiner using the given key-value separator. 返回使用给定键值分隔符的映射连接器。
      Parameters:
      separator - the key-value separator - 键值分隔符
      Returns:
      the map joiner - 映射连接器
    • withKeyValueSeparator

      public Joiner.MapJoiner withKeyValueSeparator(String separator)
      Returns a map joiner using the given key-value separator. 返回使用给定键值分隔符的映射连接器。
      Parameters:
      separator - the key-value separator - 键值分隔符
      Returns:
      the map joiner - 映射连接器