package org.eve.framework.string.join;
/**
* 连接类型
*
* @author xiayc
* @date 2018/9/7
*/
public enum JoinType {
/**
* 连接符:_
* aaa_bbb_ccc
*/
STR_STR;
}
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();
}
}