Example usage for java.util Formatter Formatter

List of usage examples for java.util Formatter Formatter

Introduction

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

Prototype

public Formatter() 

Source Link

Document

Constructs a new formatter.

Usage

From source file:com.richtodd.android.quiltdesign.block.Swatch.java

public String getDescription() {
    Formatter formatter = new Formatter();
    try {/*from   w  w  w  .  jav  a  2  s  .  c o m*/
        formatter.format("#%02x%02x%02x", Color.red(m_color), Color.green(m_color), Color.blue(m_color));
        return formatter.toString();
    } finally {
        formatter.close();
    }
}

From source file:ch.unibe.cde.geonet.kernel.security.Md5PasswordEncoder.java

/**
 *
 * @param hash/*from   w w w  .  j  a  v  a2 s. c  om*/
 * @return
 *
 * Copied from http://stackoverflow.com/a/9071224/1829038
 */
private static String byteToHex(final byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
        formatter.format("%02x", b);
    }
    String result = formatter.toString();
    formatter.close();
    return result;
}

From source file:javasnack.snacks.xml.sax2.DebugPrinter.java

@SuppressWarnings("resource")
void start(String f, Object... args) {
    indent++;//from  w w w.jav  a 2 s .co  m
    String s = new Formatter().format(f, args).toString();
    System.out.println(StringUtils.repeat(">", indent) + " " + s);
}

From source file:com.richtodd.android.quiltdesign.block.Swatch.java

public String getSortString() {
    if (m_sortString == null) {
        Color.colorToHSV(m_color, m_hsv);

        Formatter formatter = new Formatter();
        try {//from  ww  w .j a v  a 2s  .  co m
            formatter.format("%03d-%03d-%03d", (int) m_hsv[0], (int) (m_hsv[1] * 100f),
                    (int) (m_hsv[2] * 100f));
            m_sortString = formatter.toString();
        } finally {
            formatter.close();
        }
    }

    return m_sortString;
}

From source file:javasnack.snacks.xml.sax2.DebugPrinter.java

@SuppressWarnings("resource")
void print(String f, Object... args) {
    String s = new Formatter().format(f, args).toString();
    System.out.println(StringUtils.repeat(">", indent) + " " + s);
}

From source file:edu.smc.mediacommons.modules.Md5Module.java

public static String getSHA1(final File file) {
    String sha1 = null;//from w w w .  j a  v a 2s . c  om

    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("SHA1");

        InputStream is = new BufferedInputStream(new FileInputStream(file));
        final byte[] buffer = new byte[1024];
        for (int read = 0; (read = is.read(buffer)) != -1;) {
            messageDigest.update(buffer, 0, read);
        }

        // Convert the byte to hex format
        Formatter formatter = new Formatter();
        for (final byte b : messageDigest.digest()) {
            formatter.format("%02x", b);
        }

        sha1 = formatter.toString();
        formatter.close();
        is.close();
    } catch (IOException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {

    }

    return sha1;
}

From source file:javasnack.snacks.xml.sax2.DebugPrinter.java

@SuppressWarnings("resource")
void end(String f, Object... args) {
    String s = new Formatter().format(f, args).toString();
    System.out.println(StringUtils.repeat("<", indent) + " " + s);
    indent--;/*from w  w  w. jav  a 2 s.c  o  m*/
}

From source file:com.slytechs.utils.factory.FactoryLoader.java

@SuppressWarnings("unchecked")
public void loadFactoryClass() {
    String path = System.getProperty(property, defaults);

    try {/*w  ww.  j a  v  a  2s .co m*/
        factory = (T) Class.forName(path).newInstance();

    } catch (final Exception e) {

        final Formatter out = new Formatter();

        logger.error(out.format("Unable to load factory class [%s]", path).toString());

        //         throw new IllegalStateException(e);
    }
}

From source file:org.apache.drill.parsers.DrqlParserAstTest.java

@Test
public void testQueryList() throws IOException {
    Joiner withNewline = Joiner.on("\n");
    //tests parsing all SQL that are encountered in the documentation
    for (int i = 1; i <= 15; i++) {

        File tempFile = getFile("q" + i + "_temp.drql.sm");
        File expectedFile = getFile("q" + i + ".drql.ast");
        File queryFile = getFile("q" + i + ".drql");

        String query = FileUtils.readFileToString(queryFile);
        String ast = AntlrParser.parseToAst(query).toStringTree();
        Formatter f = new Formatter();
        formatAst(AntlrParser.parseToAst(query), f, 0);

        query = withNewline.join(Resources.readLines(Resources.getResource("q" + i + ".drql"), Charsets.UTF_8));
        assertEquals(query, f.toString());

        FileUtils.writeStringToFile(tempFile, ast);

        assertEquals(withNewline.join(Files.readLines(expectedFile, Charsets.UTF_8)),
                withNewline.join(Files.readLines(expectedFile, Charsets.UTF_8)));
        assertEquals(String.format("sm files differs %s versus %s", expectedFile, tempFile),
                withNewline.join(Files.readLines(expectedFile, Charsets.UTF_8)),
                withNewline.join(Files.readLines(tempFile, Charsets.UTF_8)));
        assertTrue(String.format("sm files differs %s versus %s", expectedFile, tempFile),
                FileUtils.contentEquals(expectedFile, tempFile));

        FileUtils.forceDelete(tempFile);
    }//from ww w . j  av a 2 s  . c  o m
}

From source file:javasnack.snacks.xml.sax2.DebugPrinter.java

@SuppressWarnings("resource")
void oops(String f, Object... args) {
    String s = new Formatter().format(f, args).toString();
    System.err.println(s);/* w  w w .ja  v a 2 s.  c  o  m*/
}