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

com.github.leeonky.util.GenericBeanClass Maven / Gradle / Ivy

The newest version!
package com.github.leeonky.util;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class GenericBeanClass extends BeanClass {
    private final static Map> instanceCache = new ConcurrentHashMap<>();
    private final GenericType genericType;

    @SuppressWarnings("unchecked")
    protected GenericBeanClass(GenericType genericType) {
        super((Class) genericType.getRawType());
        this.genericType = genericType;
    }

    public static BeanClass create(GenericType genericType) {
        return instanceCache.computeIfAbsent(genericType, GenericBeanClass::new);
    }

    @Override
    public Optional> getTypeArguments(int position) {
        return genericType.getGenericTypeParameter(position).map(BeanClass::create);
    }

    @Override
    public boolean hasTypeArguments() {
        return genericType.hasTypeArguments();
    }

    @Override
    public int hashCode() {
        return Objects.hash(GenericBeanClass.class, genericType);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof GenericBeanClass && Objects.equals(genericType, ((GenericBeanClass) obj).genericType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy