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(Locale l, String format, Object... args) 

Source Link

Document

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

Usage

From source file:org.hyperic.util.units.UnitsFormat.java

public static FormattedNumber format(UnitNumber val, Locale locale, FormatSpecifics specifics) {
    FormattedNumber res;//from w  w w .  j a  va 2  s. com
    Formatter formatter;

    formatter = getFormatter(val.getUnits());

    res = formatter.format(val, locale, specifics);
    if (log.isDebugEnabled()) {
        log.debug("format(" + val.getValue() + ") -> " + res);
    }
    return res;
}

From source file:org.apache.ambari.servicemonitor.utils.MonitorUtils.java

/**
 * Convert milliseconds to human time -the exact format is unspecified
 * @param milliseconds a time in milliseconds
 * @return a time that is converted to human intervals
 *//*from   w ww  .ja  v  a  2s  .  co  m*/
public static String millisToHumanTime(long milliseconds) {
    StringBuilder sb = new StringBuilder();
    // Send all output to the Appendable object sb
    Formatter formatter = new Formatter(sb, Locale.US);

    long s = Math.abs(milliseconds / 1000);
    long m = Math.abs(milliseconds % 1000);
    if (milliseconds > 0) {
        formatter.format("%d.%03ds", s, m);
    } else if (milliseconds == 0) {
        formatter.format("0");
    } else {
        formatter.format("-%d.%03ds", s, m);
    }
    return sb.toString();
}

From source file:org.kalypso.shape.deegree.Shape2GML.java

private static String buildValueElementsDefinition(final IDBFField[] fields) {
    final Formatter formatter = new Formatter();

    for (final IDBFField field : fields) {
        final IMarshallingTypeHandler th = findTypeHandler(field);

        final String name = field.getName();
        final String type = th.getTypeName().getLocalPart();

        formatter.format("<element name='%s' type='%s'/>%n", name, type);
    }/*from  ww w .  ja va2 s. c  o m*/

    return formatter.toString();
}

From source file:com.globalsight.ling.tm3.tools.ShowTmCommand.java

private void showOne(TM3Tm tm, Formatter f) throws Exception {
    f.format("%-12s%d\n", "Id:", tm.getId());
    f.format("%-12s%s\n", "Type:", tm.getType());
    if (tm instanceof TM3SharedTm) {
        f.format("%-12s%d\n", "Storage Id:", ((TM3SharedTm) tm).getSharedStorageId());
    }/*from   w  w w  .  j a  v  a2s  .c o m*/
    Set<TM3Attribute> attrs = tm.getAttributes();
    if (attrs.size() > 0) {
        f.format("%s", "Attributes: ");
        for (TM3Attribute attr : attrs) {
            f.format("%s ", attr.getName());
        }
        f.format("\n");
    }
}

From source file:ca.nines.ise.cmd.Help.java

/**
 * Format a list of commands and send it to System.out.
 *
 * @throws InstantiationException// w  w w .ja v  a2s .c  o  m
 * @throws IllegalAccessException
 */
public void listCommands() throws InstantiationException, IllegalAccessException {
    Formatter formatter = new Formatter(System.out);
    formatter.format("%n%12s   %s%n%n", "command", "description");

    Map<String, String> descriptions = new HashMap<>();
    for (Class<?> cls : ClassIndex.getSubclasses(Command.class)) {
        Command cmd = (Command) cls.newInstance();
        descriptions.put(cls.getSimpleName().toLowerCase(), cmd.description());
    }
    String names[] = descriptions.keySet().toArray(new String[descriptions.size()]);
    Arrays.sort(names);
    for (String name : names) {
        formatter.format("%12s   %s%n", name, descriptions.get(name));
    }
}

From source file:ar.com.zauber.garfio.services.impl.HourMinuteTimeConverter.java

/** @see TimeConverter#toHours(String)*/
public final String toHours(final String time) {
    Validate.isTrue(supportsTime(time));

    Matcher m = timePattern.matcher(time);
    m.matches();/*from w w w  .  j a  v a  2 s  .co  m*/
    StringBuilder builder = new StringBuilder();

    if (m.group(1) != null) {
        builder.append(m.group(1));
    }
    if (m.group(3) != null) {
        /* Only hour */
        builder.append(m.group(3));
    } else if (m.group(5) != null || m.group(6) != null) {
        /* [xxh]yym */
        if (m.group(5) != null) {
            builder.append(m.group(5));
        } else {
            builder.append("0");
        }
        if (m.group(6) != null) {
            Formatter f = new Formatter();
            f.format(Locale.US, ".%02.0f", 100 * minutesToHours(Integer.valueOf(m.group(6))));
            builder.append(f.toString());
        }
    } else {
        /* Only minutes >= 60 */
        Formatter f = new Formatter();
        f.format(Locale.US, "%.2f", minutesToHours(Integer.valueOf(m.group(7))));
        builder.append(f.toString());
    }

    return builder.toString();
}

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

protected void pumpDataIntoTestCollection() {
    // there should be 100 of each
    String[] scientists = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr",
            "Galilei", "Maxwell" };
    for (int i = 1; i <= 1000; i++) {
        int index = i % scientists.length;
        Formatter f = new Formatter();
        String doc = f.format("{\"_id\":\"%d\", \"scientist\":\"%s\", \"fixedField\": \"fixedValue\"}", i,
                scientists[index]).toString();
        testCollection.insert((DBObject) JSON.parse(doc), WriteConcern.SAFE);
    }/*from w w w  .  ja  v  a 2 s.c  o m*/
    assertEquals("Data pumping of 1000 entries did not complete entirely", 1000L, testCollection.count());
}

From source file:com.globalsight.ling.tm3.tools.ShowTmCommand.java

private <T extends TM3Data> void showAll(List<TM3Tm<T>> tms, Formatter f) throws Exception {
    if (tms.size() == 0) {
        return;//from w ww .  jav a 2 s. c  o  m
    }
    f.format("%-12s%s\n", "Id", "Type");
    for (TM3Tm tm : tms) {
        f.format("%-12d%s\n", tm.getId(), tm.getType());
    }
}

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

public void formatAst(AstNode astNode, Formatter formatter, int indent) {
    String spaces = "                                                    ";
    formatter.format("%s%s\n", spaces.substring(0, indent), astNode.getText());
    int n = astNode.getChildCount();
    for (int i = 0; i < n; i++) {
        formatAst(astNode.getChild(i), formatter, indent + 2);
    }/*from   w  ww .ja v  a  2s  .co m*/
}

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

public void formatAst(Tree astNode, Formatter formatter, int indent) {
    String spaces = "                                                    ";
    formatter.format("%s%s\n", spaces.substring(0, indent), astNode.getText());
    int n = astNode.getChildCount();
    for (int i = 0; i < n; i++) {
        formatAst(astNode.getChild(i), formatter, indent + 2);
    }//  w w  w  .  j  a  v a2  s. c o  m
}