Tools.java

package org.mklab.sdpj.tool;

import java.io.File;
import java.io.PrintStream;

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;


/**
 * Class <code>Tools</code> is a set of methods, which are frequently used. 
 * 
 * @author koga
 */
public class Tools {

  /** */
  public static int REVERSE_PRIMAL_DUAL = 1;

  /** 単位 */
  //private static NumericalScalar<?> unitNumber;

  /**
   * @param mess メッセージ
   */
  public static void message(String mess) {
    Tools.message(mess, System.out);
  }

  /**
   * メッセージを出力します。
   * 
   * @param mess メッセージ
   * @param output 出力先
   */
  public static void message(String mess, PrintStream output) {
    output.println(mess);
  }

  /**
   * エラーメッセージを表示し、停止します。
   * 
   * @param message メッセージ
   */
  public static void error(String message) {
    throw new RuntimeException(message);
  }

  /**
 * @param <RS> 実スカラーの型
 * @param <RM> 実行列の型
 * @param <CS> 複素スカラーの型
 * @param <CM> 複素行列の型
   * @param num スカラー
   * @return スカラーに対応する整数値
   */
  public static <RS extends RealNumericalScalar<RS, RM, CS, CM>, RM extends RealNumericalMatrix<RS, RM, CS, CM>, CS extends ComplexNumericalScalar<RS, RM, CS, CM>, CM extends ComplexNumericalMatrix<RS, RM, CS, CM>> int NumericalScalarToInt(RS num) {
    // TODO 多倍長だとエラー出るかも。Doubleの範囲を超える.そのときは文字列の長さを短くすれば大丈夫
    return (int)Double.parseDouble(num.toString());
  }

//  /**
//   * 単位数を設定します。
//   * 
//   * @param <E> 成分の型
//   * @param d 単位
//   */
//  public static <E extends NumericalScalar<E>> void setUnitNumber(E d) {
//    unitNumber = d.create(1);
//  }
//
//  /**
//   * 単位数を返します。
//   * 
//   * @return 単位数
//   */
//  public static NumericalScalar<?> getUnitNumber() {
//    return unitNumber;
//  }
  
  /**
   * The function <code>getFileName()</code> get a file name.
   * 
   * @param fileName The Name of a file
   * @return a name of the file
   */
  public static String getFileName(String fileName) {
    String target;
    int endIndex = fileName.lastIndexOf("."); //$NON-NLS-1$
    if (endIndex == -1) {
      endIndex = fileName.length();
    }
    
    int slashIndex = fileName.lastIndexOf("/"); //$NON-NLS-1$
    
    String sdplib = "";  //$NON-NLS-1$
   if(slashIndex != -1) {
     sdplib = fileName.substring(0, slashIndex);
   }
   
    if(sdplib.equals("sdplib") == true) { //$NON-NLS-1$
      int shift = 1;
      char isNum = fileName.charAt(endIndex - shift);
      while((isNum >= '0' && isNum <= '9') || isNum == '-') {
        shift++;
        isNum = fileName.charAt(endIndex - shift);
      }
      String folderName = fileName.substring(slashIndex + 1, endIndex - shift + 1);
      target = fileName.replaceFirst("sdplib", "result/" + folderName); //$NON-NLS-1$ //$NON-NLS-2$
      String directory = target.substring(0, target.lastIndexOf("/") + 1); //$NON-NLS-1$
      if((new File(directory).isDirectory()) == false ) {
        folderName = "others"; //$NON-NLS-1$
        target = fileName.replaceFirst("sdplib", "result/" + folderName); //$NON-NLS-1$ //$NON-NLS-2$
      }
      endIndex = target.lastIndexOf("."); //$NON-NLS-1$
    }else {
      target = fileName;
    }
    
    return target.substring(0, endIndex);
  }
}