概述
今天在浏览jdk元代,java.util包下面有个StringJoiner类,之前没怎么见过,更没有用过,所以呢,就写点代码了解了一下,顺便看了一下其不怎么多的的源代码,在这里记录分享一下。
基本使用
package test;
import java.util.StringJoiner;
/**
* @author wangqianyi
* @Title: StringJoiners
* @ProjectName API-TEST
* @Description: TODO
* @date 2019/2/27 16:26
*/
public class StringJoiners {
public static void main(String[] args) {
//一个没有前缀和后缀的简单示例
StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add("Test").add("String").add("Joiner");
System.out.println(stringJoiner.toString());
//输出:Test,String,Joiner
//一个有前缀的实例
StringJoiner sj = new StringJoiner(":","JDK<<<<<",">>>>>TestEND");
sj.add("Test").add("StringJoiner").add("prefix").add("and").add("suffix");
System.out.println(sj.toString());
//输出:JDK<<<<<Test:StringJoiner:prefix:and:suffix>>>>>TestEND
stringJoiner.merge(sj);
System.out.println(stringJoiner.toString());
//输出: Test,String,Joiner,Test:StringJoiner:prefix:and:suffix
}
}
源码阅读
首先看到上面的例子,我们想到了什么?当然是StringBuilder和StringBuffer。可以猜到,其中必然使用到了其中一个实现。
也可以这么说,StringJoiner类是对上述其中一个类的增强,在这里我理解为装饰者模式。
类属性
如下,可以看到关键的变量是value,其他的变量为附注变量
private final String prefix; //前缀
private final String delimiter; //分隔符
private final String suffix; //后缀
/*
* StringBuilder value -- at any time, the characters constructed from the
* prefix, the added element separated by the delimiter, but without the
* suffix, so that we can more easily add elements without having to jigger
* the suffix each time.
*/
private StringBuilder value; // 每次在调用add方法的时候,都是对value进行多次append操作。
/*
* By default, the string consisting of prefix+suffix, returned by
* toString(), or properties of value, when no elements have yet been added,
* i.e. when it is empty. This may be overridden by the user to be some
* other value including the empty String.
*/
private String emptyValue; // 仅仅包含前缀和后缀,没有添加任何实际的数据
构造函数
public StringJoiner(CharSequence delimiter) {
this(delimiter, "", "");
}
public StringJoiner(CharSequence delimiter,
CharSequence prefix,
CharSequence suffix) {
Objects.requireNonNull(prefix, "The prefix must not be null");
Objects.requireNonNull(delimiter, "The delimiter must not be null");
Objects.requireNonNull(suffix, "The suffix must not be null");
// make defensive copies of arguments
this.prefix = prefix.toString();
this.delimiter = delimiter.toString();
this.suffix = suffix.toString();
this.emptyValue = this.prefix + this.suffix;
}
可以看到有两个可用的构造函数,其中前后缀是不必须的,但是间隔符通常是必须的,否则就没有意义了。
关键方法
1. add方法。可以看到,在私有方法prepareBuilder()中,首次调用时,会初始化一个value并append一个前缀,之后每次调用,都会给value中append一个delimiter。在add方法中,首先调用上述私有方法,然后再append一个字符。
private StringBuilder prepareBuilder() {
if (value != null) {
value.append(delimiter);
} else {
value = new StringBuilder().append(prefix);
}
return value;
}
public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
2. merge方法。merge方法可以将两个StringJoiner类进行合并。
public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
final int length = other.value.length();
// lock the length so that we can seize the data to be appended
// before initiate copying to avoid interference, especially when
// merge 'this'
StringBuilder builder = prepareBuilder();
builder.append(other.value, other.prefix.length(), length);
}
return this;
}
3. toString方法。
这里比较简单。
public String toString() {
if (value == null) {
return emptyValue;
} else {
if (suffix.equals("")) {
return value.toString();
} else {
int initialLength = value.length();
String result = value.append(suffix).toString();
// reset value to pre-append initialLength
value.setLength(initialLength);
return result;
}
}
}
注意
- StringJoiner类在内部使用了StringBuilder,由于后者不是线程安全的,因此StringJoiner也不是线程安全的。
- 从merge方法可以看出来,在类定义是,是可以访问其私有变量的