package org.freshvanilla.lang;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Peter Lawrey
 * @version $Revision:$
 * @since 27-Aug-2008 14:51:33
 */
public class VirtualClass<T> implements MetaClass<T> {
    private final MetaClass<T> metaClass;
    private final String nameWithParameters;
    private final MetaField<T, ?>[] fields;

    public VirtualClass(MetaClass<T> metaClass, String nameWithParameters, String[] nameWithParametersSplit) {
        this.metaClass = metaClass;
        this.nameWithParameters = nameWithParameters;
        Map<String, MetaField<T, ?>> fieldsByName = new LinkedHashMap();
        for (MetaField<T, ?> field : metaClass.fields())
            fieldsByName.put(field.getName(), field);
        this.fields = new MetaField[nameWithParametersSplit.length - 1];
        for (int i = 0; i < fields.length; i++) {
            MetaField<T, ?> field = fieldsByName.get(nameWithParametersSplit[i + 1]);
            if (field == null) {
                final String name = nameWithParametersSplit[i + 1];
                field = new VanillaField<T, Void>(name, null, Void.class);
            }
            fields[i] = field;
        }
    }

    public Class<T> getType() {
        return metaClass.getType();
    }

    public String nameWithParameters() {
        return nameWithParameters;
    }

    public MetaField<T, ?>[] fields() {
        return fields;
    }

    public T newInstance() throws InstantiationException {
        return metaClass.newInstance();
    }

    public boolean definesEquals() {
        return metaClass.definesEquals();
    }

    public Class getComponentType() {
        return null;
    }
}