package jfun.yan.xml.nuts;
import java.util.ArrayList;
import java.util.List;
import jfun.util.Misc;
import jfun.util.StringUtils;
import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.SimpleComponent;
import jfun.yan.util.Utils;
/**
* Nut class for <list> tag.
* <p>
* @author Ben Yu
* Nov 9, 2005 11:42:15 PM
*/
public class ListNut extends CollectionNut {
public void setType(Class type){
if(List.class.isAssignableFrom(type)){
super.setType(type);
}
else{
raise(Misc.getTypeName(type) +
" is not a sub-type of java.util.List");
}
}
private List createList(int sz){
try{
return Utils.createList(getType(), sz);
}
catch(Exception e){
throw raise(e);
}
}
public Component eval(){
final Component[] elements = getMandatoryElements();
final Class ltype = getType(ArrayList.class);
final Component step1 = new SimpleComponent(ltype){
public Object create(){
return createList(elements.length);
}
public String toString(){
return "list"+StringUtils.listArray("[",",","]",elements);
}
};
return Components.storeList(step1, elements);
/*
return Components.array(components, Object.class).map(new Map(){
public Object map(Object a){
final Object[] arr = (Object[])a;
final List result = createList(arr.length);
result.addAll(Arrays.asList(arr));
return result;
}
}).cast(listtype);*/
}
}