一个计数器(Java)

239 阅读1分钟

自定义序号的那种,比如:1A -> 1B -> 1C -> B1 -> C4 ... ZZ

import java.util.Arrays;

/**
 * 序号的生成,初始值全部填充0,即在尚未调用{@link Counter#add()}方法时使用{@link    Counter#getValue()}返回全部填充0的字符串
 *
 * @author 免费教学录影带
 */
public class C {

    private static final int MAX_LENGTH = 9;

    private static final char[] DEFAULT_ITEMS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    /**
     * 生成序号的元素
     */
    private char[] items = DEFAULT_ITEMS;

    /**
     * 序号数组的第一个元素
     */
    private char firstItem;

    /**
     * 序号数组的最后一个元素
     */
    private char lastItem;

    /**
     * 序号长度
     */
    private final int length;

    /**
     * 序号数组
     */
    private char[] sn;

    /**
     * 按照{@code char}元素数组的的顺序每次向后进一
     *
     * @return 进一后的序号
     */
    public String add() {
        if (snIsMax()) {
            throw new RuntimeException("序号已达到最大值");
        }

        if (sn[length - 1] == lastItem) {
            sn[length - 1] = firstItem;
            int step = length - 2;
            while (true) {
                if (sn[step] == lastItem) {
                    sn[step] = firstItem;
                    step--;
                } else {
                    sn[step] = items[findItemIndex(sn[step]) + 1];
                    break;
                }
            }
        } else {
            if (findItemIndex(sn[length - 1]) != -1) {
                sn[length - 1] = items[findItemIndex(sn[length - 1]) + 1];
            } else {
                sn[length - 1] = items[0];
            }
        }
        return getValue();
    }

    /**
     * 返回此对象当前的序号
     *
     * @return 序号
     */
    public String getValue() {
        final String regex = ", ";
        return Arrays.toString(this.sn).replaceAll(regex, "").substring(1, sn.length + 1);
    }

    private int findItemIndex(char item) {
        for (int i = 0; i < items.length; i++) {
            if (items[i] == item) {
                return i;
            }
        }
        return -1;
    }

    private boolean snIsMax() {
        for (char c : sn) {
            if (c != lastItem) {
                return false;
            }
        }
        return true;
    }

    private void init() {
        final char initialValue = '0';
        if (length < 1 || length > MAX_LENGTH) {
            throw new IllegalArgumentException("长度不能小于1或超过" + MAX_LENGTH + "位");
        }
        this.sn = new char[this.length];
        this.firstItem = items[0];
        this.lastItem = items[items.length - 1];
        for (int i = 0; i < length; i++) {
            sn[i] = initialValue;
        }
    }

    /**
     * @param length 序号长度
     */
    public C(int length) {
        this.length = length;
        init();
    }

    /**
     * @param length 序号长度
     * @param items  自定义填充序号的字符数组,程序不对传入数组做重复检查
     */
    public C(int length, char[] items) {
        this.length = length;
        this.items = items;
        init();
    }

}