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:com.diversityarrays.dalclient.DalUtil.java

/**
 * Computes the MD5 checksum of the bytes in the InputStream.
 * The input is close()d on exit.//w ww  .j  a va2 s .co  m
 * @param input is the InputStream for which to compute the checksum
 * @return the MD5 checksum as a String of hexadecimal characters
 */
static public String computeMD5checksum(InputStream input) {
    DigestInputStream dis = null;
    Formatter formatter = null;
    try {
        MessageDigest md = MessageDigest.getInstance(DIGEST_MD5);
        dis = new DigestInputStream(input, md);
        while (-1 != dis.read())
            ;

        byte[] digest = md.digest();
        formatter = new Formatter();
        for (byte b : digest) {
            formatter.format("%02x", b); //$NON-NLS-1$
        }
        return formatter.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException ignore) {
            }
        }
        if (formatter != null) {
            formatter.close();
        }
    }
}

From source file:com.dubsar_dictionary.Dubsar.model.Sense.java

@Override
public void parseData(Object jsonResponse) throws JSONException {
    JSONArray response = (JSONArray) jsonResponse;

    JSONArray _word = response.getJSONArray(1);
    JSONArray _synset = response.getJSONArray(2);

    int wordId = _word.getInt(0);
    String wordName = _word.getString(1);
    String wordPos = _word.getString(2);

    int synsetId = _synset.getInt(0);

    mGloss = new String(_synset.getString(1));
    setPos(wordPos);//  w w w.  j a v a2  s.c  o  m

    mWord = new Word(wordId, wordName, getPartOfSpeech());
    mName = new String(wordName);
    mSynset = new Synset(synsetId, mGloss, getPartOfSpeech());

    mIsWeakWordReference = mIsWeakSynsetReference = false;
    mWordReference = null;
    mSynsetReference = null;

    setLexname(response.getString(3));
    getSynset().setLexname(getLexname());

    if (!response.isNull(4)) {
        setMarker(response.getString(4));
    }

    setFreqCnt(response.getInt(5));

    JSONArray _synonyms = response.getJSONArray(6);
    mSynonyms = new ArrayList<Sense>(_synonyms.length());

    int j;
    int senseId;
    String senseName;
    for (j = 0; j < _synonyms.length(); ++j) {
        JSONArray _synonym = _synonyms.getJSONArray(j);

        senseId = _synonym.getInt(0);
        senseName = _synonym.getString(1);
        String marker = null;
        if (!_synonym.isNull(2)) {
            marker = _synonym.getString(2);
        }
        int freqCnt = _synonym.getInt(3);

        Sense synonym = new Sense(senseId, senseName, getSynset());
        synonym.setMarker(marker);
        synonym.setFreqCnt(freqCnt);

        mSynonyms.add(synonym);
    }

    JSONArray _verbFrames = response.getJSONArray(7);
    mVerbFrames = new ArrayList<String>(_verbFrames.length());

    for (j = 0; j < _verbFrames.length(); ++j) {
        String frame = _verbFrames.getString(j);

        // Replace %s in verb frames with the name of the word
        // TODO: Make that a bold span.
        StringBuffer buffer = new StringBuffer();
        Formatter formatter = new Formatter(buffer);
        formatter.format(frame, new Object[] { getName() });
        formatter.close();

        mVerbFrames.add(buffer.toString());
    }

    JSONArray _samples = response.getJSONArray(8);
    mSamples = new ArrayList<String>(_samples.length());

    for (j = 0; j < _samples.length(); ++j) {
        mSamples.add(_samples.getString(j));
    }

    parsePointers(response.getJSONArray(9));
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.iPadRepositoryHelper.java

/**
 * @param hash/*from   w  ww .j av a  2 s. co  m*/
 * @return
 */
private String byteArray2Hex(byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

From source file:com.itemanalysis.psychometrics.irt.estimation.MarginalMaximumLikelihoodEstimation.java

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

    f.format("%30s", "     Latent Distribution      ");
    f.format("%n");
    f.format("%30s", "==============================");
    f.format("%n");
    f.format("%10s", "Point");
    f.format("%4s", "");
    f.format("%16s", "Density");
    f.format("%n");
    f.format("%30s", "------------------------------");
    f.format("%n");
    for (int k = 0; k < latentDistribution.getNumberOfPoints(); k++) {
        f.format("% 10.8f", latentDistribution.getPointAt(k));
        f.format("%4s", "");
        f.format("% 10.8e", latentDistribution.getDensityAt(k));//15 wide
        f.format("%n");
    }//from  ww  w.j  ava 2  s  .com
    f.format("%30s", "==============================");
    f.format("%n");
    f.format("%12s", "Mean = ");
    f.format("%8.4f", latentDistribution.getMean());
    f.format("%n");
    f.format("%12s", "Std. Dev. = ");
    f.format("%8.4f", latentDistribution.getStandardDeviation());
    f.format("%n");
    return f.toString();
}

From source file:org.echocat.locela.api.java.format.MessageFormatter.java

@Override
public void format(@Nullable final Object value, @Nonnull Writer to) throws IOException {
    final Value<Object> calledValue = CALLED_VALUE.get();
    final Object targetValue;
    if (calledValue == null) {
        // noinspection ConstantConditions
        CALLED_VALUE.set(new Value<Object>() {
            @Nonnull/*from  w  ww  . j  a va  2 s  .co m*/
            @Override
            public Object getValue() {
                return value;
            }
        });
        targetValue = value;
    } else {
        targetValue = calledValue.getValue();
    }
    try {
        for (final Formatter formatter : this) {
            formatter.format(targetValue, to);
        }
    } finally {
        if (calledValue == null) {
            CALLED_VALUE.remove();
        }
    }
}

From source file:com.itemanalysis.psychometrics.irt.estimation.MarginalMaximumLikelihoodEstimation.java

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

    f.format("%58s", "MMLE ITEM PARAMETER ESTIMATES");
    f.format("%n");
    f.format("%87s", "=======================================================================================");
    f.format("%n");
    f.format("%-18s", "Item");
    f.format("%5s", "Code");
    f.format("%9s", "Apar");
    f.format("%7s", "(SE)");
    f.format("%9s", "Bpar");
    f.format("%1s", "");
    f.format("%6s", "(SE)");
    f.format("%9s", "Cpar");
    f.format("%1s", "");
    f.format("%6s", "(SE)");
    f.format("%9s", "Upar");
    f.format("%1s", "");
    f.format("%6s", "(SE)");
    f.format("%n");
    f.format("%87s", "---------------------------------------------------------------------------------------");
    f.format("%n");

    for (int j = 0; j < nItems; j++) {
        sb.append(irm[j].toString() + "\n");
    }/*w  w  w. j  ava2  s  .  c  o  m*/

    f.format("%87s", "=======================================================================================");
    f.format("%n");

    return f.toString();
}

From source file:com.itemanalysis.psychometrics.irt.estimation.MarginalMaximumLikelihoodEstimation.java

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

    f.format("%34s", "ITEM FIT STATISTIC");
    f.format("%n");
    f.format("%50s", "==================================================");
    f.format("%n");
    f.format("%-18s", "Item");
    f.format("%2s", "");
    if (g2ItemFit) {
        f.format("%8s", "G2");
    } else {// ww w.  j  a va 2  s. c  om
        f.format("%8s", "S-X2");
    }
    f.format("%2s", "");
    f.format("%8s", "df");
    f.format("%2s", "");
    f.format("%8s", "p-value");
    f.format("%n");
    f.format("%50s", "--------------------------------------------------");
    f.format("%n");

    for (int j = 0; j < nItems; j++) {
        f.format("%-18s", irm[j].getName());
        f.format("%2s", "");
        f.format("%8.4f", itemFit[j].getValue());
        f.format("%2s", "");
        f.format("%8.4f", itemFit[j].getDegreesOfFreedom());
        f.format("%2s", "");
        f.format("%8.4f", itemFit[j].getPValue());
        f.format("%n");
    }

    f.format("%50s", "==================================================");
    f.format("%n");

    return f.toString();
}

From source file:org.kalypso.model.wspm.core.profil.sobek.utils.hw.HeightWidthResult.java

@Override
public void formatLog(final Formatter formatter) {
    calculate();// w  w w  . j a v a  2s.com
    if (m_polygon == null)
        return;

    super.formatLog(formatter);

    final double areaPoly = m_polygon.getArea();
    final double areaHW = calculateArea(m_widths, m_heights);

    formatter.format("Flche Shape: %f%n", areaPoly); //$NON-NLS-1$
    formatter.format("Flche HW   : %f%n", areaHW); //$NON-NLS-1$
}

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

public String toStringWithRawScore(String title, boolean header, boolean footer) {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    if (header) {
        f.format("%-50s", title);
        f.format("%n");
        f.format("%50s", "==================================================");
        f.format("%n");
        f.format("%10s", "Sum Score");
        f.format("%2s", " ");
        f.format("%10s", "Theta");
        f.format("%2s", " ");
        f.format("%10s", "Std. Error");
        f.format("%2s", " ");
        f.format("%10s", "Frequency");
        f.format("%2s", " ");
        f.format("%n");
        f.format("%50s", "--------------------------------------------------");
        f.format("%n");
    }//from w ww .  j av a 2s  . c  o m
    f.format("%10.4f", rawScore);
    f.format("%2s", " ");
    f.format("%10.4f", theta);
    f.format("%2s", " ");
    if (rawScore == 0 || rawScore == maximumPossibleScore) {
        f.format("%10.4s", "--");
        f.format("%2s", " ");
    } else {
        f.format("%10.4f", stdError);
        f.format("%2s", " ");
    }
    f.format("%10.0f", freq);
    f.format("%n");
    if (footer) {
        f.format("%50s", "==================================================");
        f.format("%n");
    }

    return f.toString();
}

From source file:httpmultiplexer.httpproxy.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>//from   w ww .jav a2s.  c  om
 * 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
 */
protected 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;
}