Exemplo n.º 1
0
  protected static String argumentExpression(ArgumentItem argItem) {
    StringBuilder line = new StringBuilder();

    Argument arg = argItem.getArgumentDescriptor();
    String format;

    if (arg.required()) format = argItem.takesMultipleArguments() ? "%s ..." : "%s";
    else format = argItem.takesMultipleArguments() ? "[%s ...]" : "[%s]";

    String name = StringUtil.varNameToNaturalName(argItem.getArgumentName());

    line.append(String.format(format, name));

    return line.toString();
  }
  @Test
  public void performanceOfCSVSplit() throws Exception {

    StopWatch s = new StopWatch();

    String line = null;
    final int N = 1;
    Pattern p = Pattern.compile(",");

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        ArrayList<String> csv = StringUtil.splitCSV(line);
      }
    }
    _logger.info("OpenCSV split:" + s.getElapsedTime());
  }
Exemplo n.º 3
0
  public void printUsage(Writer out) {
    Properties helpMessageTemplateValue = new Properties();

    // argument list
    Collections.sort(
        argumentItemList,
        new Comparator<ArgumentItem>() {
          public int compare(ArgumentItem o1, ArgumentItem o2) {
            return o1.getRange().compareTo(o2.getRange());
          }
        });

    List<String> argExpressionList =
        Algorithm.map(
            argumentItemList,
            new Mapper<ArgumentItem, String>() {
              public String map(ArgumentItem input) {
                return argumentExpression(input);
              }
            });

    // usage information
    if (usage != null) {
      helpMessageTemplateValue.put(TemplateVariable.COMMAND.name(), usage.command());
      if (usage.description() != null && usage.description().length() > 0)
        helpMessageTemplateValue.put(TemplateVariable.DESCRIPTION.name(), usage.description());
    }

    helpMessageTemplateValue.put(
        TemplateVariable.ARGUMENT_LIST.name(), StringUtil.join(argExpressionList, " "));

    // option list
    Collections.sort(
        optionItemList,
        new Comparator<OptionItem>() {
          public int compare(OptionItem o1, OptionItem o2) {
            Option opt1 = o1.getOption();
            Option opt2 = o2.getOption();

            // prefer options that have a short name
            if (o1.hasSymbol()) {
              if (!o2.hasSymbol()) return -1;
            } else if (o2.hasSymbol()) return 1;

            int diff = opt1.symbol().compareTo(opt2.symbol());
            if (diff == 0) return opt1.longName().compareTo(opt2.longName());
            else return diff;
          }
        });

    List<String> descriptionList =
        Algorithm.map(
            optionItemList,
            new Mapper<OptionItem, String>() {
              public String map(OptionItem input) {
                return optionDescription(input);
              }
            });

    int maxDescriptionLength = 15;
    for (String each : descriptionList)
      if (each.length() > maxDescriptionLength) maxDescriptionLength = each.length();

    String optionHelpFormat = String.format(" %%-%ds  %%s", maxDescriptionLength);

    StringWriter optionListHelpWriter = new StringWriter();
    for (int i = 0; i < optionItemList.size(); ++i) {
      OptionItem optionItem = optionItemList.get(i);
      String optionHelp = descriptionList.get(i);
      String line =
          String.format(optionHelpFormat, optionHelp, optionItem.getOption().description());
      optionListHelpWriter.append(line);
      optionListHelpWriter.append(StringUtil.newline());
    }
    helpMessageTemplateValue.put(
        TemplateVariable.OPTION_LIST.name(), optionListHelpWriter.toString());

    // render help messages using template

    ResourcePath rp =
        new ResourcePath(usage != null ? usage.templatePath() : Usage.DEFAULT_TEMPLATE);

    try {
      Template helpMessageTemplate = new Template(rp.openBinaryStream());
      String helpMessage = helpMessageTemplate.apply(helpMessageTemplateValue);
      out.append(helpMessage);
      out.flush();
    } catch (IOException e) {
      throw new XerialError(XerialErrorCode.OUTPUT_ERROR, e);
    } catch (XerialException e) {
      throw new XerialError(e);
    }
  }
Exemplo n.º 4
0
 @Override
 public String toString() {
   return String.format("function %s(%s)", name, StringUtil.join(argumentList, ", "));
 }
  @Test
  public void performanceOfTabSplit() throws Exception {

    StopWatch s = new StopWatch();

    String line = null;
    final int N = 1;

    Pattern p = Pattern.compile("\t");

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        String[] tab = p.split(line);
      }
    }
    _logger.info("default tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        ArrayList<String> tokens = StringUtil.splitAtTab(line);
      }
    }
    _logger.info("my tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        StringTokenizer t = new StringTokenizer(line, "\t");
        ArrayList<String> tokens = new ArrayList<String>();
        while (t.hasMoreTokens()) {
          tokens.add(t.nextToken());
        }
      }
    }
    _logger.info("tokenizer tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      FastBufferedReader fb =
          new FastBufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = fb.readLine()) != null) {
        ArrayList<String> tab = StringUtil.splitAtTab(line);
      }
    }
    _logger.info("fast reader:" + s.getElapsedTime());
  }