Example usage for java.io PrintStream format

List of usage examples for java.io PrintStream format

Introduction

In this page you can find the example usage for java.io PrintStream format.

Prototype

public PrintStream format(String format, Object... args) 

Source Link

Document

Writes a formatted string to this output stream using the specified format string and arguments.

Usage

From source file:info.mikaelsvensson.devtools.analysis.shared.reportprinter.PlainTextReportPrinter.java

@Override
public void printTable(PrintStream printStream, String tableHeader, int valueColumnWidth, String[] headerRow,
        Object[][] dataRows, String[] footerRow) {
    int firstColumnWidth = Math.max(headerRow != null && headerRow.length > 0 ? headerRow[0].length() : 0,
            footerRow != null && footerRow.length > 0 ? footerRow[0].length() : 0);
    for (Object[] row : dataRows) {
        if (row != null) {
            firstColumnWidth = Math.max(firstColumnWidth, row[0].toString().length());
        }//from w  w w.  j a  v a 2  s.c  o m
    }
    if (tableHeader != null) {
        printHeader(tableHeader, true, printStream);
    }
    String rowFormat = "%-" + (firstColumnWidth + 1) + "s"
            + StringUtils.repeat("%" + valueColumnWidth + "s",
                    dataRows != null && dataRows.length > 0 ? dataRows[0].length - 1 : headerRow.length - 1)
            + "\n";
    if (headerRow != null) {
        printStream.format(rowFormat, headerRow);
    }
    if (dataRows != null) {
        for (Object[] row : dataRows) {
            if (row != null) {
                for (int i = 0; i < row.length; i++) {
                    row[i] = ToStringUtil.toString(row[i]);
                }
                if (row != null) {
                    printStream.format(rowFormat, row);
                }
            }
        }
    }
    if (footerRow != null) {
        printStream.format(rowFormat, footerRow);
    }
}

From source file:com.analog.lyric.dimple.solvers.core.parameterizedMessages.MultivariateNormalParameters.java

@Override
public void print(PrintStream out, int verbosity) {
    if (verbosity < 0) {
        return;//w w w  .j  a va2 s.  c  o m
    }

    String vectorLabel = "mean";
    double[] vector = _mean;
    if (vector.length == 0) {
        vector = _infoVector;
        vectorLabel = "info";
    }

    out.print("Normal(");

    if (verbosity > 1) {
        out.println();
        out.print("    ");
    }
    out.format("%s=[", vectorLabel);
    for (int i = 0, end = vector.length; i < end; ++i) {
        if (i > 0) {
            out.print(',');
            if (verbosity > 0) {
                out.print(' ');
            }
        }
        if (verbosity > 0) {
            out.format("%d=", i);
        }
        out.format(verbosity > 1 ? "%.12g" : "%g", vector[i]);
    }
    out.print(']');
    if (verbosity > 1) {
        out.println();
    } else {
        out.print(", ");
    }

    out.print(_isInInformationForm ? "precision=[" : "covariance=[");

    if (isDiagonal()) {
        double[] diagonal = _isInInformationForm ? _precision : _variance;

        for (int i = 0, end = diagonal.length; i < end; ++i) {
            if (i > 0) {
                out.print(',');
                if (verbosity > 0) {
                    out.print(' ');
                }
            }
            if (verbosity > 0) {
                out.format("%d=", i);
            }
            out.format("%g", diagonal[i]);
        }
    } else {
        final int n = _matrix.length;
        for (int row = 0; row < n; ++row) {
            if (row > 0) {
                out.print(";");
            }
            out.print("\n        ");
            for (int col = 0; col < n; ++col) {
                if (col > 0) {
                    out.print(',');
                }
                out.format("%g", _matrix[row][col]);
            }
        }
    }

    out.print(']');

    if (verbosity > 1) {
        out.println();
    }
    out.print(')');
}

From source file:com.appeligo.search.actions.ResponseReportAction.java

public String execute() throws Exception {
    long timestamp = new Date().getTime();
    String day = Utils.getDatePath(timestamp);
    String hostname = null;/*from w  w  w.j a v  a  2 s.c  om*/
    try {
        hostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        hostname = "UnknownHost";
    }
    String dirname = documentRoot + "/stats/" + day + "/" + hostname;
    String responseFileName = dirname + "/response-" + reporter + ".html";
    try {
        File dir = new File(dirname);
        if ((!dir.exists()) && (!dir.mkdirs())) {
            throw new IOException("Error creating directory " + dirname);
        }
        File file = new File(responseFileName);
        PrintStream responseFile = null;
        if (file.exists()) {
            responseFile = new PrintStream(new FileOutputStream(responseFileName, true));
        } else {
            responseFile = new PrintStream(new FileOutputStream(responseFileName));
            String title = "Response Times for " + getServletRequest().getServerName() + " to " + reporter;
            responseFile.println("<html><head><title>" + title + "</title></head>");
            responseFile.println("<body><h1>" + title + "</h1>");
            responseFile.println("<table border='1'>");
            responseFile.println("<tr>");
            responseFile.println("<th>Time (UTC)</th>");
            responseFile.println("<th>Response (Millis)</th>");
            responseFile.println("<th>Status</th>");
            responseFile.println("<th>Bytes Read</th>");
            responseFile.println("<th>Timed Out</th>");
            responseFile.println("<th>Exception</th>");
            responseFile.println("</tr>");
        }
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(timestamp);
        String time = String.format("%1$tH:%1$tM:%1$tS", cal);
        responseFile.print("<tr>");
        responseFile.print("<td>" + time + "</td>");
        responseFile.format("<td>%,d</td>", responseMillis);
        responseFile.print("<td>" + status + "</td>");
        responseFile.format("<td>%,d</td>", bytesRead);
        responseFile.print("<td>" + timedOut + "</td>");
        responseFile.print("<td>" + exception + "</td>");
        responseFile.println("</tr>");
    } catch (IOException e) {
        log.error("Error opening or writing to " + responseFileName, e);
    }

    return SUCCESS;
}

From source file:com.marklogic.client.impl.JerseyServices.java

private void logRequest(RequestLogger reqlog, String message, Object... params) {
    if (reqlog == null)
        return;/*  w  w w .  ja va2s .  c o  m*/

    PrintStream out = reqlog.getPrintStream();
    if (out == null)
        return;

    if (params == null || params.length == 0) {
        out.println(message);
    } else {
        out.format(message, params);
        out.println();
    }
}

From source file:org.apache.mahout.classifier.ConfusionMatrixDumper.java

private static void printGrayCell(ConfusionMatrix cm, PrintStream out, int total, String rowLabel,
        String columnLabel) {/*from  w  w w. ja va2  s . c  o m*/

    int count = cm.getCount(rowLabel, columnLabel);
    if (count == 0) {
        out.format("<td class='%s'/>", CSS_EMPTY);
    } else {
        // 0 is white, full is black, everything else gray
        int rating = (int) ((count / (double) total) * 4);
        String css = CSS_GRAY_CELLS[rating];
        format("<td class='%s' title='%s'>%s</td>", out, css, columnLabel, count);
    }
}

From source file:org.jenkinsci.plugins.githubissues.GitHubIssueNotifier.java

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher,
        @Nonnull TaskListener listener) throws InterruptedException, IOException {
    PrintStream logger = listener.getLogger();

    // If we got here, we need to grab the repo to create an issue (or close an existing issue)
    GHRepository repo;/*from   ww w  .j  a v  a2 s .com*/
    try {
        repo = getRepoForJob(run.getParent());
    } catch (GitHubRepositoryException ex) {
        logger.println(
                "WARNING: No GitHub config available for this job, GitHub Issue Notifier will not run! Error: "
                        + ex.getMessage());
        return;
    }

    Result result = run.getResult();
    final GitHubIssueAction previousGitHubIssueAction = getLatestIssueAction((Build) run.getPreviousBuild());
    GHIssue issue = null;
    if (previousGitHubIssueAction != null) {
        issue = repo.getIssue(previousGitHubIssueAction.getIssueNumber());
    }

    if (result == Result.FAILURE) {
        if (issue != null) {
            String issueBody = this.getIssueBody();
            if (StringUtils.isBlank(issueBody)) {
                issueBody = this.getDescriptor().getIssueBody();
            }
            if (issue.getState() == GHIssueState.OPEN) {
                if (issueAppend) {
                    //CONTINUE
                    issue.comment(IssueCreator.formatText(issueBody, run, listener, workspace));
                    logger.format("GitHub Issue Notifier: Build is still failing and issue #%s already exists. "
                            + "Not sending anything to GitHub issues%n", issue.getNumber());
                }
                run.addAction(new GitHubIssueAction(issue, GitHubIssueAction.TransitionAction.CONTINUE));
            } else if (issue.getState() == GHIssueState.CLOSED) {
                if (issueReopen) {
                    // REOPEN
                    logger.format(
                            "GitHub Issue Notifier: Build has started failing again, reopend GitHub issue #%s%n",
                            issue.getNumber());
                    issue.reopen();
                    issue.comment(IssueCreator.formatText(issueBody, run, listener, workspace));
                    //set new labels
                    if (issueLabel != null && !issueLabel.isEmpty()) {
                        issue.setLabels(issueLabel.split(",| "));
                    }
                    run.addAction(new GitHubIssueAction(issue, GitHubIssueAction.TransitionAction.REOPEN));
                } else {
                    //CREATE NEW
                    issue = IssueCreator.createIssue(run, this, repo, listener, workspace);
                    logger.format("GitHub Issue Notifier: Build has started failing, filed GitHub issue #%s%n",
                            issue.getNumber());
                    run.addAction(new GitHubIssueAction(issue, GitHubIssueAction.TransitionAction.OPEN));
                }
            }
        } else {
            // CREATE NEW
            issue = IssueCreator.createIssue(run, this, repo, listener, workspace);
            logger.format("GitHub Issue Notifier: Build has started failing, filed GitHub issue #%s%n",
                    issue.getNumber());
            run.addAction(new GitHubIssueAction(issue, GitHubIssueAction.TransitionAction.OPEN));
        }
    } else if (result == Result.SUCCESS && issue != null && issue.getState() == GHIssueState.OPEN) {
        issue.comment("Build was fixed!");
        issue.close();
        logger.format("GitHub Issue Notifier: Build was fixed, closing GitHub issue #%s%n", issue.getNumber());
        run.addAction(new GitHubIssueAction(issue, GitHubIssueAction.TransitionAction.CLOSE));
    }
}