add methods

This commit is contained in:
Looly 2022-02-27 00:08:59 +08:00
parent c46781f2eb
commit ce62b5e792
4 changed files with 75 additions and 15 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.7.22 (2022-02-26)
# 5.7.22 (2022-02-27)
### 🐣新特性
* 【poi 】 ExcelUtil.readBySax增加对POI-5.2.0的兼容性issue#I4TJF4@gitee
@ -16,6 +16,8 @@
* 【http 】 增加HttpGlobalConfig.setDecodeUrlissue#I4U8YQ@Gitee
* 【core 】 增加Base58pr#2162@Github
* 【core 】 增加AntPathMatcherissue#I4T7K5@Gitee
* 【core 】 StrJoiner修改toString策略调用不再修改Appendable
* 【core 】 StrJoiner增加length和merge方法
### 🐞Bug修复
* 【cache 】 修复ReentrantCache.toString方法线程不安全问题issue#2140@Github

View File

@ -14,7 +14,6 @@ import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collector;
import java.util.stream.Collectors;

View File

@ -12,7 +12,14 @@ import java.util.Iterator;
import java.util.function.Function;
/**
* 字符串连接器拼接器通过给定的字符串和多个元素拼接为一个字符串
* 字符串连接器拼接器通过给定的字符串和多个元素拼接为一个字符串<br>
* 相较于{@link java.util.StringJoiner}提供更加灵活的配置包括
* <ul>
* <li>支持任意Appendable接口实现</li>
* <li>支持每个元素单独wrap</li>
* <li>支持自定义null的处理逻辑</li>
* <li>支持自定义默认结果</li>
* </ul>
*
* @author looly
* @since 5.7.2
@ -282,6 +289,11 @@ public class StrJoiner implements Appendable, Serializable {
@Override
public StrJoiner append(CharSequence csq) {
return append(csq, 0, csq.length());
}
@Override
public StrJoiner append(CharSequence csq, int startInclude, int endExclude) {
if (null == csq) {
switch (this.nullMode) {
case IGNORE:
@ -298,7 +310,7 @@ public class StrJoiner implements Appendable, Serializable {
if (wrapElement && StrUtil.isNotEmpty(this.prefix)) {
appendable.append(prefix);
}
appendable.append(csq);
appendable.append(csq, startInclude, endExclude);
if (wrapElement && StrUtil.isNotEmpty(this.suffix)) {
appendable.append(suffix);
}
@ -308,29 +320,55 @@ public class StrJoiner implements Appendable, Serializable {
return this;
}
@Override
public StrJoiner append(CharSequence csq, int startInclude, int endExclude) {
return append(StrUtil.sub(csq, startInclude, endExclude));
}
@Override
public StrJoiner append(char c) {
return append(String.valueOf(c));
}
/**
* 合并一个StrJoiner 到当前的StrJoiner<br>
* 合并规则为在尾部直接追加当存在{@link #prefix}如果{@link #wrapElement}{@code false}则去除之
*
* @param strJoiner 其他的StrJoiner
* @return this
* @since 5.7.22
*/
public StrJoiner merge(StrJoiner strJoiner){
if(null != strJoiner && null != strJoiner.appendable){
final String otherStr = strJoiner.toString();
if(strJoiner.wrapElement){
this.append(otherStr);
}else{
this.append(otherStr, this.prefix.length(), otherStr.length());
}
}
return this;
}
/**
* 长度<br>
* 长度计算方式为prefix + suffix + content<br>
* 此方法结果与toString().length()一致
*
* @return 长度如果结果为{@code null}返回-1
* @since 5.7.22
*/
public int length() {
return (this.appendable != null ? this.appendable.toString().length() + suffix.length() :
null == this.emptyResult ? -1 : emptyResult.length());
}
@Override
public String toString() {
if (null == this.appendable) {
return emptyResult;
}
String result = this.appendable.toString();
if (false == wrapElement && StrUtil.isNotEmpty(this.suffix)) {
try {
this.appendable.append(this.suffix);
} catch (IOException e) {
throw new IORuntimeException(e);
}
result += this.suffix;
}
return this.appendable.toString();
return result;
}
/**

View File

@ -78,4 +78,25 @@ public class StrJoinerTest {
.append("3");
Assert.assertEquals("[1],[2],[3]", append.toString());
}
@Test
public void lengthTest(){
StrJoiner joiner = StrJoiner.of(",", "[", "]");
Assert.assertEquals(joiner.toString().length(), joiner.length());
joiner.append("123");
Assert.assertEquals(joiner.toString().length(), joiner.length());
}
@Test
public void mergeTest(){
StrJoiner joiner1 = StrJoiner.of(",", "[", "]");
joiner1.append("123");
StrJoiner joiner2 = StrJoiner.of(",", "[", "]");
joiner1.append("456");
joiner1.append("789");
final StrJoiner merge = joiner1.merge(joiner2);
Assert.assertEquals("[123,456,789]", merge.toString());
}
}