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

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

package com.moon.poi.excel.table;

import com.moon.core.util.ListUtil;
import com.moon.poi.excel.annotation.TableColumn;
import com.moon.poi.excel.annotation.TableColumnGroup;
import com.moon.poi.excel.annotation.style.DefinitionStyle;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author moonsky
 */
final class Marked implements Descriptor {

    protected final static  T obtain(M m, Class type) {
        return m.getAnnotation(type);
    }

    protected final static  T obtain(Member m, Class type) {
        return obtain((AnnotatedElement) m, type);
    }

    static Marked of(Field field) { return new Marked(field.getName(), field.getType(), field); }

    static Marked of(PropertyDescriptor descriptor, Method method) {
        return new Marked(descriptor.getName(), descriptor.getPropertyType(), method);
    }

    private final static short[] DEFAULT_HEIGHT_ARR = {};

    private final String name;
    private final Class type;
    private final T member;
    private final TableColumn annotationColumn;
    private final TableColumnGroup annotationGroup;

    protected Marked(String name, Class type, T member) {
        this.member = Objects.requireNonNull(member);
        this.type = Objects.requireNonNull(type);
        this.name = Objects.requireNonNull(name);

        this.annotationColumn = obtain(member, TableColumn.class);
        this.annotationGroup = obtain(member, TableColumnGroup.class);
        // Assert.notDuplicated(this);
    }

    public T getMember() { return member; }

    @Override
    public  T getAnnotation(Class annotationType) {
        return obtain(member, annotationType);
    }

    @Override
    public List getDefinitionStylesOnMethod() {
        if (member instanceof Method) {
            return ListUtil.newList(((Method) member).getAnnotationsByType(DefinitionStyle.class));
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public List getDefinitionStylesOnField() {
        if (member instanceof Field) {
            return ListUtil.newList(((Field) member).getAnnotationsByType(DefinitionStyle.class));
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public TableColumn getTableColumn() { return annotationColumn; }

    @Override
    public TableColumnGroup getTableColumnGroup() { return annotationGroup; }

    @Override
    public String[] getTitles() {
        return getOrDefault(col -> col.value(), null);
    }

    @Override
    public short[] getHeadHeightArr() {
        return getOrDefault(col -> col.rowsHeight4Head(), DEFAULT_HEIGHT_ARR);
    }

    @Override
    public Integer getColumnWidth() { return isAnnotated() ? getTableColumn().width() : null; }

    @Override
    public String getName() { return name; }

    @Override
    public Class getPropertyType() { return type; }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy