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

package net.ranides.assira.reflection.impl.bean;

import java.util.Collection;
import net.ranides.assira.collection.maps.Cache;
import net.ranides.assira.reflection.BeanMethod;
import net.ranides.assira.reflection.BeanModel;
import net.ranides.assira.reflection.BeanProperty;
import net.ranides.assira.reflection.IAttribute;
import net.ranides.assira.reflection.IClass;
import net.ranides.assira.reflection.IMethod;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class FBeanModel {
    
    private static final Cache<IClass<?>,BeanModel> CACHE = new Cache<>();
    
    private FBeanModel() {
        /* utility class */
    }
    
    public static BeanModel resolve(IClass<?> type) {
        return CACHE.get(type, ()->new RBeanModel(type));
    }
    
    public static BeanMethod resolve(Collection<IMethod> methods) {
        if(methods.size() > 1) {
            return new RBeanMethods(methods);
        } else {
            return new RBeanMethod(methods.iterator().next());
        }
    }

    public static BeanProperty resolve(RBeanPropertyInfo info) {
        info.validate();
        if( IAttribute.ARRAY.matches(info.type()) ) {
            return new RPArray(info);
        } 
        if( info.item()!=null ) {
            return new RPIndexed(info);
        } 
        return new RPField(info);
    }

}
