package cn.com.qmth.stmms.biz.utils;

import org.apache.commons.lang3.ArrayUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * 题号排序对象的抽象接口
 */
public interface QuestionNumberBean {

    Integer getMainNumber();

    String getSubNumber();

    default Integer[] getSubNumbers() {
        String subNumber = getSubNumber();
        if (subNumber == null) {
            return ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY;
        }
        int count = 0;
        List<Integer> list = new LinkedList<>();
        StringBuilder buffer = new StringBuilder();
        int length = subNumber.length();
        for (int i = 0; i < length; i++) {
            char c = subNumber.charAt(i);
            // 数字字符
            if (c >= '0' && c <= '9') {
                buffer.append(c);
            }
            // 非数字字符，转换已累计数字
            else if (buffer.length() > 0) {
                try {
                    list.add(Integer.parseInt(buffer.toString()));
                    count++;
                } catch (Exception e) {
                    //
                }
                buffer.setLength(0);
            }
        }
        if (buffer.length() > 0) {
            try {
                list.add(Integer.parseInt(buffer.toString()));
                count++;
            } catch (Exception e) {
                //
            }
            buffer.setLength(0);
        }
        return list.toArray(new Integer[count]);
    }
}
