package com.qmth.boot.tools.excel.filter;

import java.util.HashSet;
import java.util.Set;

/**
 * 针对Excel读取过程中的sheet过滤器
 */
public class SheetFilter {

    private Integer index;

    private Set<String> names;

    private SheetFilter() {
    }

    /**
     * 只读取第一个sheet
     *
     * @return
     */
    public static SheetFilter first() {
        SheetFilter filter = new SheetFilter();
        filter.index = 0;
        return filter;
    }

    /**
     * 读取指定序号的sheet，从0开始
     *
     * @return
     */
    public static SheetFilter forIndex(int index) {
        SheetFilter filter = new SheetFilter();
        filter.index = index;
        return filter;
    }

    /**
     * 读取指定名称的sheet
     *
     * @return
     */
    public static SheetFilter forName(String name) {
        SheetFilter filter = new SheetFilter();
        filter.names = new HashSet<>();
        filter.names.add(name);
        return filter;
    }

    public boolean match(int index, String name) {
        if (this.index != null) {
            return this.index == index;
        } else if (names != null) {
            return names.contains(name);
        } else {
            return false;
        }
    }
}
