LibraryMethod.java :  » Science » jmatlab » org » jmatlab » compiler » Java Open Source

Java Open Source » Science » jmatlab 
jmatlab » org » jmatlab » compiler » LibraryMethod.java
/*
 * Created on Jan 19, 2004
 *
 */
package org.jmatlab.compiler;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.jmatlab.linalg.ICell;
import org.jmatlab.linalg.IComplex;
import org.jmatlab.linalg.IMatrix;
import org.jmatlab.linalg.IStruct;
import org.jmatlab.semantic.DataType;
import org.jmatlab.semantic.Symbol;

/**
 * @author Ali
 *
 */
public class LibraryMethod {

  public static final int M_METHOD = 0;
  public static final int J_METHOD = 1;
  
  private Method method;
  private String methodName;
  private Class[] signature;
  private Class[] returns;
  private String path;
  private int type;
  private String help;
  
  /**
   * @param methodName
   * @param signature
   * @param returns
   * @param type
   */
  public LibraryMethod(String methodName, Class[] signature) {
    this.methodName = methodName;
    this.signature = signature;
  }

  public String getMethodKey() {
    return makeKey(methodName, signature);
  }
  
  public static String makeKey(String name, Class[] sign) {
    StringBuffer buffer = new StringBuffer();
    buffer.append(name);
    for (int i=0; i<sign.length; i++) {
      int n = sign[i].getName().lastIndexOf(".");
      buffer.append(sign[i].getName().substring(n+1));
    }
    return buffer.toString();
  }
  
  public boolean isValidArguments(Class[] sign) {
    if (sign.length != this.signature.length) return false;
    for (int i=0; i<sign.length; i++) {
      if (sign[i]!=signature[i]) {
        return false;
      }
    }
    return true;
  }
  
  /**
   * @return Returns the methodName.
   */
  public String getMethodName() {
    return methodName;
  }

  /**
   * @param methodName The methodName to set.
   */
  public void setMethodName(String methodName) {
    this.methodName = methodName;
  }

  /**
   * @return Returns the returns.
   */
  public Class[] getReturns() {
    return returns;
  }

  /**
   * @param returns The returns to set.
   */
  public void setReturns(Class[] returns) {
    this.returns = returns;
  }

  /**
   * @return Returns the signature.
   */
  public Class[] getSignature() {
    return signature;
  }

  /**
   * @param signature The signature to set.
   */
  public void setSignature(Class[] signature) {
    this.signature = signature;
  }

  /**
   * @return Returns the type.
   */
  public int getType() {
    return type;
  }

  /**
   * @param type The type to set.
   */
  public void setType(int type) {
    this.type = type;
  }

  /**
   * @return Returns the path.
   */
  public String getPath() {
    return path;
  }

  /**
   * @param path The path to set.
   */
  public void setPath(String path) {
    this.path = path;
  }

  /**
   * @return Returns the method.
   */
  public Method getMethod() {
    return method;
  }

  /**
   * @param method The method to set.
   */
  public void setMethod(Method method) {
    this.method = method;
    this.returns = new Class[1]; 
    this.returns[0] = method.getReturnType();
  }

  public static Class[] getSignature(Vector v) {
    int sz = v.size();
    Class[] ret = new Class[sz];
    if (sz==0) return ret;
    for (int i=0;i<sz;i++) {
      int j = i;
      Symbol p = (Symbol) v.get(j);
      int type = p.getType();
      if (type==DataType.DOUBLE) {
        ret[i] = double.class;
      } else if (type==DataType.COMPLEX) {
        ret[i] = IComplex.class;
      } else if (type==DataType.MATRIX) {
        ret[i] = IMatrix.class;
      } else if (type==DataType.BOOLEAN) {
        ret[i] = boolean.class;
      } else if (type==DataType.STRING) {
        ret[i] = String.class;
      } else if (type == DataType.STRUCT) {
        ret[i] = IStruct.class;
      } else if (type == DataType.CELL) {
        ret[i] = ICell.class;
      } else if (type == DataType.LIST) {
        ret[i] = List.class;
      } else if (type == DataType.VECTOR) {
        ret[i] = Vector.class;
      } else {
        ret[i] = String.class;
      }
    }
    return ret;
  }

  public static Object[] getArguments(Vector v) {
    int sz = v.size();
    Object[] ret = new Object[sz];
    if (sz==0) return ret;
    for (int i=0;i<sz;i++) {
      int j = i;
      Symbol p = (Symbol) v.get(j);
      int type = p.getType();
      if (type==DataType.DOUBLE) {
        ret[i] = p.getDouble();
      } else if (type==DataType.COMPLEX) {
        ret[i] = p.getComplex();
      } else if (type==DataType.MATRIX) {
        ret[i] = p.getMatrix();
      } else if (type==DataType.BOOLEAN) {
        ret[i] = p.getBoolean();
      } else if (type==DataType.STRING) {
        ret[i] = p.getString();
      } else if (type == DataType.STRUCT) {
        ret[i] = p.getStruct();
      } else if (type == DataType.CELL) {
        ret[i] = p.getCell();
      } else if (type == DataType.LIST) {
        ret[i] = p.getList();
      } else if (type == DataType.VECTOR) {
        ret[i] = p.getVector();
      } else {
        ret[i] = p.getName();
      }
    }
    return ret;
  }
  
  public static Object[] getFevalArguments(Vector v) {
    int sz = v.size();
    Object[] ret = new Object[sz];
    if (sz==0) return ret;
    Symbol p = (Symbol) v.get(0);
    ret[0] = p.getString();
    List list = new ArrayList();
    for (int i=1;i<sz;i++) {
      p = (Symbol) v.get(i);
      int type = p.getType();
      if (type==DataType.DOUBLE) {
        list.add(p.getDouble());
      } else if (type==DataType.COMPLEX) {
        list.add(p.getComplex());
      } else if (type==DataType.MATRIX) {
        list.add(p.getMatrix());
      } else if (type==DataType.BOOLEAN) {
        list.add(p.getBoolean());
      } else if (type==DataType.STRING) {
        list.add(p.getString());
      } else if (type == DataType.STRUCT) {
        list.add(p.getStruct());
      } else if (type == DataType.CELL) {
        list.add(p.getCell());
      } else if (type == DataType.LIST) {
        list.add(p.getList());
      } else if (type == DataType.VECTOR) {
        list.add(p.getVector());
      } else {
        list.add(p.getName());
      }
    }
    ret[1] = list;
    return ret;
  }

  public void setHelp(String help) {
    this.help = help;
  }
  
  public String getHelp() {
    return help;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.