1 package jfun.yan.xml.nuts; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 6 import java.util.List ; 7 import jfun.yan.Component; 8 import jfun.yan.Monad; 9 import jfun.yan.xml.nut.ComponentNut; 10 11 17 public abstract class EntriesNut extends ComponentNut { 18 private final HashMap components = new HashMap (); 19 private final ArrayList keys = new ArrayList (); 20 private Class of; 21 private Class key_type; 22 23 public Class getKey_type() { 24 return key_type; 25 } 26 public void setKey_type(Class key_type) { 27 this.key_type = key_type; 28 } 29 public void setOf(Class of){ 30 this.of = of; 31 } 32 public Class getOf(){ 33 return of; 34 } 35 public void addEntry(MapEntry entry){ 36 Object key = entry.getKey(); 37 if(key_type!=null){ 38 key = entry.convert(key_type, key); 39 } 40 if(components.containsKey(key)){ 41 raise("duplicate key: "+key); 42 } 43 keys.add(key); 44 components.put(key, entry); 45 } 46 public List getKeys(){ 47 return keys; 48 } 49 public Component[] getEntryComponents(){ 50 final int sz = keys.size(); 51 final Component[] vals = new Component[sz]; 52 for(int i=0; i<sz; i++){ 53 final Object key = keys.get(i); 54 final MapEntry entry = (MapEntry)components.get(key); 55 Component val = entry.getVal(Object .class); 56 final Component def = entry.getDefault(Object .class); 57 if(def!=null){ 58 val = Monad.mplus(val, def); 59 } 60 vals[i] = (of==null)?val:Util.convert(entry, of, val); 61 } 62 return vals; 63 } 64 65 } 66 | Popular Tags |