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

com.github.datalking.common.convert.descriptor.AbstractDescriptor Maven / Gradle / Ivy

package com.github.datalking.common.convert.descriptor;

import com.github.datalking.util.Assert;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Map;

/**
 * 类型描述符 抽象类
 *
 * @author yaoo on 5/10/18
 */
public abstract class AbstractDescriptor {

    private final Class type;

    public abstract Annotation[] getAnnotations();

    protected abstract Class resolveCollectionElementType();

    protected abstract Class resolveMapKeyType();

    protected abstract Class resolveMapValueType();

    protected abstract AbstractDescriptor nested(Class type, int typeIndex);

    protected AbstractDescriptor(Class type) {
        Assert.notNull(type, "Type must not be null");
        this.type = type;
    }

    public Class getType() {
        return this.type;
    }

    public TypeDescriptor getElementTypeDescriptor() {
        if (isCollection()) {
            Class elementType = resolveCollectionElementType();
            return (elementType != null ? new TypeDescriptor(nested(elementType, 0)) : null);
        } else if (isArray()) {
            Class elementType = getType().getComponentType();
            return new TypeDescriptor(nested(elementType, 0));
        } else {
            return null;
        }
    }

    public TypeDescriptor getMapKeyTypeDescriptor() {
        if (isMap()) {
            Class keyType = resolveMapKeyType();
            return keyType != null ? new TypeDescriptor(nested(keyType, 0)) : null;
        } else {
            return null;
        }
    }

    public TypeDescriptor getMapValueTypeDescriptor() {
        if (isMap()) {
            Class valueType = resolveMapValueType();
            return valueType != null ? new TypeDescriptor(nested(valueType, 1)) : null;
        } else {
            return null;
        }
    }

    public AbstractDescriptor nested() {
        if (isCollection()) {
            Class elementType = resolveCollectionElementType();
            return (elementType != null ? nested(elementType, 0) : null);
        } else if (isArray()) {
            return nested(getType().getComponentType(), 0);
        } else if (isMap()) {
            Class mapValueType = resolveMapValueType();
            return (mapValueType != null ? nested(mapValueType, 1) : null);
        } else if (Object.class.equals(getType())) {
            // could be a collection type but we don't know about its element type,
            // so let's just assume there is an element type of type Object
            return this;
        } else {
            throw new IllegalStateException("Not a collection, array, or map: cannot resolve nested value types");
        }
    }

    private boolean isCollection() {
        return Collection.class.isAssignableFrom(getType());
    }

    private boolean isArray() {
        return getType().isArray();
    }

    private boolean isMap() {
        return Map.class.isAssignableFrom(getType());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy