版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xyc_csdn/article/details/82496944
- JoinType
package org.eve.framework.string.join;
/**
* 连接类型
*
* @author xiayc
* @date 2018/9/7
*/
public enum JoinType {
/**
* 连接符:_
* aaa_bbb_ccc
*/
STR_STR;
}
- StringJoiner
package org.eve.framework.string.join;
import org.eve.framework.exception.UnknownTypeException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* 字符串连接器
*
* @author xiayc
* @date 2018/9/7
*/
public final class StringJoiner {
private final String jointMark;
private final JoinType joinType;
private final List<CharSequence> charSequences;
public StringJoiner(String jointMark) {
this.jointMark = jointMark;
this.joinType = JoinType.STR_STR;
this.charSequences = new ArrayList<>();
}
public StringJoiner(String jointMark, JoinType joinType) {
Objects.requireNonNull(joinType, "'joinType' must not be null");
this.jointMark = jointMark;
this.joinType = joinType;
this.charSequences = new ArrayList<>();
}
public static StringJoiner getInstance(String jointMark) {
return new StringJoiner(jointMark);
}
public static StringJoiner getInstance(String jointMark, JoinType joinType) {
return new StringJoiner(jointMark, joinType);
}
public String getJointMark() {
return jointMark;
}
public JoinType getJoinType() {
return joinType;
}
//-------------------------------------后追加-------------------------------------//
public StringJoiner append(Object obj) {
return append(String.valueOf(obj));
}
public StringJoiner append(String str) {
this.charSequences.add(str);
return this;
}
public StringJoiner append(StringBuffer sb) {
this.charSequences.add(sb.toString());
return this;
}
public StringJoiner append(StringBuilder sb) {
this.charSequences.add(sb.toString());
return this;
}
public StringJoiner append(CharSequence s) {
this.charSequences.add(s);
return this;
}
public StringJoiner append(CharSequence s, int start, int end) {
this.charSequences.add(s.subSequence(start, end));
return this;
}
public StringJoiner append(char[] str) {
this.charSequences.add(new String(str));
return this;
}
public StringJoiner append(char[] chars, int offset, int len) {
char[] newChars = new char[len];
System.arraycopy(chars, offset, newChars, 0, len);
this.append(newChars);
return this;
}
public StringJoiner append(boolean b) {
this.append(b);
return this;
}
public StringJoiner append(char c) {
this.append(c);
return this;
}
public StringJoiner append(int i) {
this.append(i);
return this;
}
public StringJoiner append(long lng) {
this.append(lng);
return this;
}
public StringJoiner append(float f) {
this.append(f);
return this;
}
public StringJoiner append(double d) {
this.append(d);
return this;
}
//-------------------------------------前追加-------------------------------------//
public StringJoiner prepend(Object obj) {
return prepend(String.valueOf(obj));
}
public StringJoiner prepend(String str) {
this.charSequences.add(0, str);
return this;
}
public StringJoiner prepend(StringBuffer sb) {
this.charSequences.add(0, sb.toString());
return this;
}
public StringJoiner prepend(StringBuilder sb) {
this.charSequences.add(0, sb.toString());
return this;
}
public StringJoiner prepend(CharSequence s) {
this.charSequences.add(0, s);
return this;
}
public StringJoiner prepend(CharSequence s, int start, int end) {
this.charSequences.add(0, s.subSequence(start, end));
return this;
}
public StringJoiner prepend(char[] str) {
this.charSequences.add(0, new String(str));
return this;
}
public StringJoiner prepend(char[] chars, int offset, int len) {
char[] newChars = new char[len];
System.arraycopy(chars, offset, newChars, 0, len);
this.prepend(newChars);
return this;
}
public StringJoiner prepend(boolean b) {
this.prepend(b);
return this;
}
public StringJoiner prepend(char c) {
this.prepend(c);
return this;
}
public StringJoiner prepend(int i) {
this.prepend(i);
return this;
}
public StringJoiner prepend(long lng) {
this.prepend(lng);
return this;
}
public StringJoiner prepend(float f) {
this.prepend(f);
return this;
}
public StringJoiner prepend(double d) {
this.prepend(d);
return this;
}
//-------------------------------------清空-------------------------------------//
public void clear() {
this.charSequences.clear();
}
//-------------------------------------获取字符串-------------------------------------//
@Override
public String toString() {
switch (this.joinType) {
case STR_STR:
return this.toStringBySTR_STR();
default:
throw new UnknownTypeException("Unknown JoinType '" + this.joinType + "'");
}
}
private String toStringBySTR_STR() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < this.charSequences.size(); i++) {
if (i > 0) {
stringBuilder.append(this.jointMark);
}
stringBuilder.append(this.charSequences.get(i));
}
return stringBuilder.toString();
}
}