1 package jfun.yan.xml.nuts; 2 3 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import jfun.util.Misc; 9 import jfun.yan.Component; 10 import jfun.yan.Components; 11 import jfun.yan.SimpleComponent; 12 import jfun.yan.util.Utils; 13 19 public class MapNut extends EntriesNut { 20 private Class type = HashMap .class; 21 public Class getType() { 22 return type; 23 } 24 25 public void setType(Class type) { 26 if(!Map .class.isAssignableFrom(type)){ 27 raise(Misc.getTypeName(type)+" is not a subtype of java.util.Map"); 28 } 29 this.type = type; 30 } 31 public Map createMap(int sz){ 32 try{ 33 return Utils.createMap(type, sz); 34 } 35 catch(Exception e){ 36 throw raise(e); 37 } 38 } 39 public Component eval(){ 40 checkMandatory("map type", type); 41 final List keys = getKeys(); 42 final int sz = keys.size(); 43 final Component[] vals = getEntryComponents(); 44 final Component step1 = new SimpleComponent(type){ 45 public Object create(){ 46 return createMap(sz); 47 } 48 }; 49 return Components.storeMap(step1, keys.toArray(), vals); 50 63 } 64 } 65 | Popular Tags |