字符串连接工具类

  • 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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值