Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

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

Source Link

Document

Writes a formatted string to this object's destination using the specified format string and arguments.

Usage

From source file:be.milieuinfo.core.proxy.controller.ProxyServlet.java

/**
 * <p>Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more
 * forgiving, we must escape the problematic characters.  See the URI class for the spec.
 *
 * @param in example: name=value&foo=bar#fragment
 *//*w  ww  . jav  a2  s .  com*/
static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:org.apache.camel.component.mongodb.MongoDbOperationsTest.java

@Test
public void testColStats() throws Exception {
    assertEquals(0, testCollection.count());

    // Add some records to the collection (and do it via camel-mongodb)
    for (int i = 1; i <= 100; i++) {
        String body = null;// w  ww .  j ava2 s  . c  om
        Formatter f = new Formatter();
        body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
        template.requestBody("direct:insert", body);
    }

    Object result = template.requestBody("direct:getColStats", "irrelevantBody");
    assertTrue("Result is not of type DBObject", result instanceof DBObject);
    assertTrue("The result should contain keys", ((DBObject) result).keySet().size() > 0);
}

From source file:com.itemanalysis.psychometrics.polycor.PolychoricTwoStepOLD.java

public String printThresholds() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    double r = this.value();
    double[] t = this.getRowThresholds();

    for (int i = 0; i < t.length - 1; i++) {
        f.format("% 6.4f", t[i]);
        f.format("%2s", "");
    }//from  w ww  .jav  a 2s.  c om
    return f.toString();

}

From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistribution.java

public String printCovariance() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    for (int i = 0; i < sigma.getRowDimension(); i++) {
        for (int j = 0; j < sigma.getColumnDimension(); j++) {
            f.format("% 8.2f", sigma.getEntry(i, j));
            f.format("%5s", "");
        }/* w  w  w . j  ava 2  s  .  c o m*/
        f.format("%n");
    }
    return f.toString();
}

From source file:com.itemanalysis.psychometrics.polycor.PolychoricLogLikelihoodTwoStep.java

public String print(double[] x) {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    double se = this.getStandardError(x);

    f.format("%34s", "Polychoric correlation, Two-step est. = ");
    f.format("%6.4f", rho);
    f.format(" (%6.4f)", se);
    f.format("%n");
    f.format("%n");
    f.format("%-20s", "Row Thresholds");
    f.format("%n");

    for (int i = 0; i < (alpha.length - 1); i++) {
        f.format("%6.4f", alpha[i]);
        f.format("%n");
    }/*from   w  w w. j a  va 2  s  .com*/

    f.format("%n");
    f.format("%n");
    f.format("%-20s", "Column Thresholds");
    f.format("%n");

    for (int i = 0; i < (beta.length - 1); i++) {
        f.format("% 6.4f", beta[i]);
        f.format("%n");
    }

    f.format("%n");
    return f.toString();

}

From source file:uk.ac.ebi.phenotype.web.proxy.ExternalUrlConfiguratbleProxyServlet.java

/**
 * <p>//from   w  w w  .  j a v  a2 s.  c o  m
 * Encodes characters in the query or fragment part of the URI.
 * 
 * <p>
 * Unfortunately, an incoming URI sometimes has characters disallowed by the
 * spec. HttpClient insists that the outgoing proxied request has a valid
 * URI because it uses Java's {@link URI}. To be more forgiving, we must
 * escape the problematic characters. See the URI class for the spec.
 * 
 * @param in
 *            example: name=value&foo=bar#fragment
 */
static CharSequence encodeUriQuery(CharSequence in) {
    // Note that I can't simply use URI.java to encode because it will
    // escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            // escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            // leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);// TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

private void printBytes(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        formatter.format("%02x ", b);
    }//from  w  ww.  java  2  s.co  m
    System.out.println(sb.toString());
}

From source file:com.cognifide.slice.cq.taglib.FormatTag.java

/** {@inheritDoc} */
@Override//  w  w  w.jav a  2  s . com
public int doStartTag() throws JspException {
    if (StringUtils.isNotBlank(format)) {
        Writer out = getJspWriter();

        Formatter formatter;
        if (locale == null) {
            formatter = new Formatter(out);
        } else {
            formatter = new Formatter(out, locale);
        }

        formatter.format(format, value);
        formatter.flush();
        formatter.close();
    }
    return SKIP_BODY;
}

From source file:com.itemanalysis.psychometrics.rasch.RatingScaleItem.java

public String printItem() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    f.format("%10s", this.getName().toString());
    f.format("%5s", "");
    f.format("%8.2f", getDifficulty());
    f.format("%5s", "");

    for (int i = 0; i < thresholds.length; i++) {
        f.format("%8.2f", thresholds[i]);
        f.format("%5s", "");
    }//from  ww w.  ja v a2 s .  c om

    return f.toString();

}

From source file:com.itemanalysis.psychometrics.scaling.ScoreTable.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    String f1 = "%10." + precision + "f";
    String f2 = "%10s";
    //      String f3="%10.4f";

    //table header
    f.format("%28s", "SCORE TABLE");
    f.format("%n");
    f.format("%45s", "=============================================");
    f.format("%n");
    f.format(f2, "Original");
    f.format("%5s", "");
    f.format(f2, "Kelley");
    f.format("%n");

    f.format(f2, "Value");
    f.format("%5s", "");
    f.format(f2, "Score");
    f.format("%n");
    f.format("%45s", "---------------------------------------------");
    f.format("%n");

    for (Double d : table.keySet()) {
        f.format(f1, d);/*w  w  w. ja  v a 2  s . c  om*/
        f.format("%5s", "");
        f.format(f1, table.get(d));
        f.format("%5s", "");
        f.format("%n");
    }

    f.format("%45s", "=============================================");
    f.format("%n");
    return f.toString();

}