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

org.nutz.ioc.val.CollectionValue Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.ioc.val;

import java.util.ArrayList;
import java.util.Collection;

import org.nutz.ioc.IocMaking;
import org.nutz.ioc.ValueProxy;
import org.nutz.ioc.meta.IocValue;
import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;

public class CollectionValue implements ValueProxy {

    private Class> type;

    private ValueProxy[] values;

    @SuppressWarnings("unchecked")
    public CollectionValue(    IocMaking ing,
                            Collection col,
                            Class> type) {
        this.type = (Class>) (null == type ? ArrayList.class : type);
        values = new ValueProxy[col.size()];
        int i = 0;
        for (IocValue iv : col)
            values[i++] = ing.makeValue(iv);
    }

    public Object get(IocMaking ing) {
        try {
            Collection re = Mirror.me(type).born();
            for (ValueProxy vp : values)
                re.add(vp.get(ing));
            return re;
        }
        catch (Exception e) {
            throw Lang.wrapThrow(e);
        }
    }

}