Package jfun.yan.xml.nuts

Source Code of jfun.yan.xml.nuts.MapNut

package jfun.yan.xml.nuts;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jfun.util.Misc;
import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.SimpleComponent;
import jfun.yan.util.Utils;
/**
* Nut class for <map> tag.
* <p>
* @author Ben Yu
* Nov 9, 2005 11:42:15 PM
*/
public class MapNut extends EntriesNut {
  private Class type = HashMap.class;
  public Class getType() {
    return type;
  }
 
  public void setType(Class type) {
    if(!Map.class.isAssignableFrom(type)){
      raise(Misc.getTypeName(type)+" is not a subtype of java.util.Map");
    }
    this.type = type;
  }
  public Map createMap(int sz){
    try{
      return Utils.createMap(type, sz);
    }
    catch(Exception e){
      throw raise(e);
    }
  }
  public Component eval(){
    checkMandatory("map type", type);
    final List keys = getKeys();
    final int sz = keys.size();
    final Component[] vals = getEntryComponents();
    final Component step1 = new SimpleComponent(type){
      public Object create(){
        return createMap(sz);
      }
    };
    return Components.storeMap(step1, keys.toArray(), vals);
    /*
    return Components.array(vals, Object.class)
    .map(new jfun.yan.Map(){
      public Object map(Object a){
        final Object[] arr = (Object[])a;
        final int sz = keys.size();
        final Map result = createMap(sz);
        for(int i=0; i<sz;i++){
          result.put(keys.get(i), arr[i]);
        }
        return result;
      }
    }).cast(type);*/
  }
}
TOP

Related Classes of jfun.yan.xml.nuts.MapNut

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.