Package jfun.yan.xml.nuts

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

package jfun.yan.xml.nuts;

import java.util.HashSet;

import java.util.Set;


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 <set> tag.
* <p>
* @author Ben Yu
* Nov 9, 2005 11:42:15 PM
*/
public class SetNut extends CollectionNut {
  public void setType(Class type){
    if(Set.class.isAssignableFrom(type)){
      super.setType(type);
    }
    else{
      raise(Misc.getTypeName(type) +
          " is not a sub-type of java.util.Set");
    }
  }

  private Set createSet(int sz){
    try{
      return Utils.createSet(getType(), sz);
    }
    catch(Exception e){
      throw raise(e);
    }
  }
  public Component eval(){
    final Component[] elements = getMandatoryElements();
    final Class ltype = getType(HashSet.class);
    final Component step1 = new SimpleComponent(ltype){
      public Object create(){
        return createSet(elements.length);
      }
      public String toString(){
        return "set"+StringUtils.listArray("[",",","]",elements);
      }
    };
    return Components.storeSet(step1, elements);
  }

}
TOP

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

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.