package jfun.yan.xml.nuts;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import jfun.yan.Component;
import jfun.yan.Monad;
import jfun.yan.xml.nut.ComponentNut;
/**
* Super class for tags that support <entry> sub elements.
* <p>
* @author Ben Yu
* Nov 17, 2005 1:03:03 AM
*/
public abstract class EntriesNut extends ComponentNut {
private final HashMap components = new HashMap();
private final ArrayList keys = new ArrayList();
private Class of;
private Class key_type;
public Class getKey_type() {
return key_type;
}
public void setKey_type(Class key_type) {
this.key_type = key_type;
}
public void setOf(Class of){
this.of = of;
}
public Class getOf(){
return of;
}
public void addEntry(MapEntry entry){
Object key = entry.getKey();
if(key_type!=null){
key = entry.convert(key_type, key);
}
if(components.containsKey(key)){
raise("duplicate key: "+key);
}
keys.add(key);
components.put(key, entry);
}
public List getKeys(){
return keys;
}
public Component[] getEntryComponents(){
final int sz = keys.size();
final Component[] vals = new Component[sz];
for(int i=0; i<sz; i++){
final Object key = keys.get(i);
final MapEntry entry = (MapEntry)components.get(key);
Component val = entry.getVal(Object.class);
final Component def = entry.getDefault(Object.class);
if(def!=null){
val = Monad.mplus(val, def);
}
vals[i] = (of==null)?val:Util.convert(entry, of, val);
}
return vals;
}
}