/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */

package net.ranides.assira.reflection.impl;

import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import net.ranides.assira.collection.lists.ListUtils;
import net.ranides.assira.generic.LazyReference;
import net.ranides.assira.reflection.IClass;
import net.ranides.assira.reflection.IContext;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class RPClass<T> extends RClass<T> {

    private final ParameterizedType ptype;
    
    private final Supplier<IContext> icontext = LazyReference.concurrent(this::icontext);

    RPClass(IContext context, Class<T> type, ParameterizedType ptype) {
        super(context, type);
        this.ptype = ptype;
    }

    @Override
    public IContext context() {
        return icontext.get();
    }

    private IContext icontext() {
        return super.context().derive(ptype);
    }
    
    @Override
    public List<IClass<?>> params() {
        IContext c = context();
        return ListUtils.map(Arrays.asList(ptype.getActualTypeArguments()), c::typeinfo);
    }
    
    @Override
    public IClass<?> outer() {
        return context().typeinfo(ptype.getOwnerType());
    }

}
