Package jfun.yan.xml

Source Code of jfun.yan.xml.NutsContinuation

package jfun.yan.xml;

import jfun.yan.Component;
import jfun.yan.SimpleComponent;

/**
* A function that's called within <callcc>.
* <p>
* @author Ben Yu
* Dec 2, 2005 4:15:35 PM
*/
final class NutsContinuation implements NutsFunction {
  private final Object id;
  private final String name;
  private final Location loc;
  private static final String[] pnames = {Constants.RESULT};
 
  NutsContinuation(String name, Location loc, Object id) {
    this.id = id;
    this.loc = loc;
    this.name = name;
  }

  public Object call(Object[] args) {
    final Object result = args[0];
    return new SimpleComponent(null){
      public Object create(){
        throw getContinuation(id, result);
      }
      public String toString(){
        return name + " " + result;
      }
    };
  }

  public String getName() {
    return name;
  }

  public int getParameterCount() {
    return 1;
  }

  public Class getReturnType() {
    return Component.class;
  }

  public String[] getParameterNames() {
    return pnames;
  }

  public Location getLocation() {
    return loc;
  }

  public boolean equals(Object obj) {
    if(obj instanceof NutsContinuation){
      final NutsContinuation other = (NutsContinuation)obj;
      return id==other.id;
    }
    else return false;
  }

  public int hashCode() {
    return System.identityHashCode(id);
  }

  public String toString() {
    return name;
  }
  private static ContinuationEscapeException getContinuation(Object id, Object result){
    final ContinuationEscapeException ex = (ContinuationEscapeException)
      exception.get();
    ex.clearResolutionTrace();
    ex.setId(id);
    ex.setResult(result);
    return ex;
  }
  private static final ThreadLocal exception = new ThreadLocal(){
    protected Object initialValue() {
      return new ContinuationEscapeException("continuation");
    }
  };
}
TOP

Related Classes of jfun.yan.xml.NutsContinuation

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.