All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moon.poi.excel.table.ParserUtil Maven / Gradle / Ivy

package com.moon.poi.excel.table;

import com.moon.core.util.ListUtil;

import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.Function;

/**
 * @author moonsky
 */
final class ParserUtil {

    static final  T obtainOrNull(Marked marked, Function getter) {
        return marked == null ? null : getter.apply(marked);
    }

    static boolean isExpectCached(Class type) {
        return !type.isMemberClass() || Modifier.isStatic(type.getModifiers());
    }

    /**
     * 检测必须是普通可实例化的类,不能是接口抽象类
     */
    static void checkValidImplClass(Class type, Class expectSuperClass) {
        int modifiers = type.getModifiers();
        if (!Modifier.isPublic(modifiers)) {
            throw new IllegalStateException("指定类「" + type + "」应该是普通公共类(public)");
        }
        if (Modifier.isInterface(modifiers) || Modifier.isAbstract(modifiers)) {
            throw new IllegalStateException("指定类「" + type + "」不能是接口或抽象类");
        }
        if (!expectSuperClass.isAssignableFrom(type)) {
            throw new IllegalStateException("指定类「" + type + "」必须是「" + expectSuperClass + "」的实现类");
        }
    }


    static TableRenderer mapAttrsIfUnAnnotated(
        Class targetClass, Map unAnnotatedMap, Function transformer
    ) { return doMapAttrs(targetClass, ListUtil.newList(unAnnotatedMap.values()), transformer); }

    /**
     * 转换 Attribute 为具体执行类
     *
     * @param annotatedMap {@link #merge2Attr(Map, Map)}
     * @param targetClass  被解析的类
     * @param transformer  like name
     *
     * @return Renderer,最终用于渲染 Table 的类型
     */
    static TableRenderer mapAttrs(
        Class targetClass, Map annotatedMap, Function transformer
    ) { return doMapAttrs(targetClass, new ArrayList<>(annotatedMap.values()), transformer); }

    /**
     * 转换 Attribute
     *
     * @param targetClass 被解析的类
     * @param list        所有 Attribute 项
     * @param transformer 转换器
     *
     * @return Renderer,最终用于渲染 Table 的类型
     */
    private static TableRenderer doMapAttrs(
        Class targetClass, List list, Function transformer
    ) {
        list.sort(Attribute::compareTo);
        // TableCol[] columns = new TableCol[list.size()];
        List columns = new ArrayList<>();
        Map styleMap = StyleUtil.collect(targetClass, list);
        AttrConfig config = new AttrConfig(targetClass, styleMap);

        for (int i = 0, size = list.size(); i < size; i++) {
            Attribute attr = list.get(i);
            config.setAttribute(attr, i);
            columns.add(transformer.apply(config));
        }

        return new TableRenderer(targetClass, styleMap, ListUtil.toArray(columns, TableCol[]::new));
    }

    static void putMarked(Marked marked, Map annotated, Map unAnnotated) {
        Map group = marked.isAnnotated() ? annotated : unAnnotated;
        group.put(marked.getName(), marked);
    }

    /**
     * 合并{@link Marked}为{@link Attribute}
     * 

* 相同字段的注解合并到一个{@link Attribute}上 *

* 顺序以字段声明顺序优先 * * @param atMethod 于方法上的注解 * @param atField 于字段上的注解 * @param {@link Marked}的具体实现类型 * * @return 合并后的 attributes,key 是字段名 */ static Map merge2Attr( Map atMethod, Map atField ) { return merge2Attr(atMethod, atField, new HashMap<>(0), new HashMap<>(0)); } /** * 合并{@link Marked}为{@link Attribute} *

* 相同字段的注解合并到一个{@link Attribute}上 *

* 顺序以字段声明顺序优先 * * @param primaryAtMethod 于方法上的注解 * @param primaryAtField 于字段上的注解 * @param supplementAtMethod 补充,primary 上的内容可能不全,这里用作补充 * @param supplementAtField 补充 * @param {@link Marked}的具体实现类型 * * @return 合并后的 attributes,key 是字段名 */ static Map merge2Attr( Map primaryAtMethod, Map primaryAtField, Map supplementAtMethod, Map supplementAtField ) { Map attrMap = new LinkedHashMap<>(); for (Map.Entry entry : primaryAtField.entrySet()) { String name = entry.getKey(); Marked field = entry.getValue(); Marked method = primaryAtMethod.remove(name); if (method == null) { method = supplementAtMethod.remove(name); } attrMap.put(name, new Attribute(method, field)); } for (Map.Entry entry : primaryAtMethod.entrySet()) { String name = entry.getKey(); Marked method = entry.getValue(); Marked field = supplementAtField.remove(name); attrMap.put(name, new Attribute(method, field)); } return attrMap; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy