Search in sources :

Example 86 with Formatter

use of java.util.Formatter in project drools by kiegroup.

the class DefaultExpander method displayUsage.

private void displayUsage(String what, Map<String, Integer> use) {
    logger.info("=== Usage of " + what + " ===");
    Formatter fmt = new Formatter(System.out);
    for (Map.Entry<String, Integer> entry : use.entrySet()) {
        fmt.format("%4d %s%n", entry.getValue(), entry.getKey());
    }
}
Also used : Formatter(java.util.Formatter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 87 with Formatter

use of java.util.Formatter in project wikidata-query-rdf by wikimedia.

the class StatementHelper method randomDate.

/**
 * Construct a random date literal.
 */
public static LiteralImpl randomDate() {
    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb, Locale.US);
    formatter.format("%04d-%02d-%02d", randomIntBetween(1, 9999), randomIntBetween(1, 12), randomIntBetween(1, 28));
    formatter.close();
    return new LiteralImpl(sb.toString(), XMLSchema.DATE);
}
Also used : IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Formatter(java.util.Formatter)

Example 88 with Formatter

use of java.util.Formatter in project vertx-examples by vert-x3.

the class TopCommand method start.

@Override
public void start() throws Exception {
    Command starwars = CommandBuilder.command("top").processHandler(process -> {
        long id = process.vertx().setPeriodic(500, id_ -> {
            StringBuilder buf = new StringBuilder();
            Formatter formatter = new Formatter(buf);
            List<Thread> threads = new ArrayList<>(Thread.getAllStackTraces().keySet());
            for (int i = 1; i <= process.height(); i++) {
                // Change cursor position and erase line with ANSI escape code magic
                buf.append("\033[").append(i).append(";1H\033[K");
                // 
                String format = "  %1$-5s %2$-20s %3$-50s %4$s";
                if (i == 1) {
                    formatter.format(format, "ID", "STATE", "NAME", "GROUP");
                } else {
                    int index = i - 2;
                    if (index < threads.size()) {
                        Thread thread = threads.get(index);
                        formatter.format(format, thread.getId(), thread.getState().name(), thread.getName(), thread.getThreadGroup().getName());
                    }
                }
            }
            process.write(buf.toString());
        });
        // Terminate when user hits Ctrl-C
        process.interruptHandler(v -> {
            vertx.cancelTimer(id);
            process.end();
        });
    }).build(vertx);
    ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions(new TelnetTermOptions().setHost("localhost").setPort(3000)));
    CommandRegistry.getShared(vertx).registerCommand(starwars);
    service.start(ar -> {
        if (!ar.succeeded()) {
            ar.cause().printStackTrace();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) List(java.util.List) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) Runner(io.vertx.example.util.Runner) ArrayList(java.util.ArrayList) Formatter(java.util.Formatter) ShellService(io.vertx.ext.shell.ShellService) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) Formatter(java.util.Formatter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 89 with Formatter

use of java.util.Formatter in project karaf by apache.

the class TestPerf method testGelfTimestamp.

@Test
public void testGelfTimestamp() throws Exception {
    long timestamp = System.currentTimeMillis();
    int iterations = 1000000;
    for (int p = 0; p < 10; p++) {
        Buffer buffer = new Buffer(Buffer.Format.Json);
        long t0 = measure(() -> {
            buffer.clear();
            long secs = timestamp / 1000;
            int ms = (int) (timestamp - secs * 1000);
            buffer.format(secs);
            buffer.append('.');
            int temp = ms / 100;
            buffer.append((char) (temp + '0'));
            ms -= 100 * temp;
            temp = ms / 10;
            buffer.append((char) (temp + '0'));
            ms -= 10 * temp;
            buffer.append((char) (ms + '0'));
            return null;
        }, iterations);
        System.out.println("t0 = " + t0);
        long t1 = measure(() -> {
            buffer.clear();
            long secs = timestamp / 1000;
            int ms = (int) (timestamp - secs * 1000);
            buffer.format(Long.toString(secs));
            buffer.append('.');
            int temp = ms / 100;
            buffer.append((char) (temp + '0'));
            ms -= 100 * temp;
            temp = ms / 10;
            buffer.append((char) (temp + '0'));
            ms -= 10 * temp;
            buffer.append((char) (ms + '0'));
            return null;
        }, iterations);
        System.out.println("t1 = " + t1);
        long t2 = measure(() -> {
            buffer.clear();
            new Formatter(buffer).format("%.3f", ((double) timestamp) / 1000.0);
            return null;
        }, iterations);
        System.out.println("t2 = " + t2);
    }
}
Also used : Buffer(org.apache.karaf.audit.util.Buffer) Formatter(java.util.Formatter) Test(org.junit.Test)

Example 90 with Formatter

use of java.util.Formatter in project systemml by apache.

the class GPUTests method assertEqualMatrices.

/**
 * Asserts that the values in two matrices are in {@link UnaryOpTests#DOUBLE_PRECISION_THRESHOLD} of each other
 *
 * @param expected expected matrix
 * @param actual   actual matrix
 */
private void assertEqualMatrices(Matrix expected, Matrix actual) {
    try {
        // Faster way to compare two matrices
        MLContext cpuMLC = new MLContext(spark);
        String scriptStr = "num_mismatch = sum((abs(X - Y) / X) > " + getTHRESHOLD() + ");";
        Script script = ScriptFactory.dmlFromString(scriptStr).in("X", expected).in("Y", actual).out("num_mismatch");
        long num_mismatch = cpuMLC.execute(script).getLong("num_mismatch");
        cpuMLC.close();
        if (num_mismatch == 0)
            return;
        // If error, print the actual incorrect values
        MatrixBlock expectedMB = expected.toMatrixObject().acquireRead();
        MatrixBlock actualMB = actual.toMatrixObject().acquireRead();
        long rows = expectedMB.getNumRows();
        long cols = expectedMB.getNumColumns();
        Assert.assertEquals(rows, actualMB.getNumRows());
        Assert.assertEquals(cols, actualMB.getNumColumns());
        if (PRINT_MAT_ERROR)
            printMatrixIfNotEqual(expectedMB, actualMB);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                double expectedDouble = expectedMB.quickGetValue(i, j);
                double actualDouble = actualMB.quickGetValue(i, j);
                if (expectedDouble != 0.0 && !Double.isNaN(expectedDouble) && Double.isFinite(expectedDouble)) {
                    double relativeError = Math.abs((expectedDouble - actualDouble) / expectedDouble);
                    double absoluteError = Math.abs(expectedDouble - actualDouble);
                    Formatter format = new Formatter();
                    format.format("Relative error(%f) is more than threshold (%f). Expected = %f, Actual = %f, differed at [%d, %d]", relativeError, getTHRESHOLD(), expectedDouble, actualDouble, i, j);
                    if (FLOATING_POINT_PRECISION.equals("double"))
                        Assert.assertTrue(format.toString(), relativeError < getTHRESHOLD());
                    else
                        Assert.assertTrue(format.toString(), relativeError < getTHRESHOLD() || absoluteError < getTHRESHOLD());
                    format.close();
                } else {
                    Assert.assertEquals(expectedDouble, actualDouble, getTHRESHOLD());
                }
            }
        }
        expected.toMatrixObject().release();
        actual.toMatrixObject().release();
    } catch (DMLRuntimeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) Formatter(java.util.Formatter) MLContext(org.apache.sysml.api.mlcontext.MLContext) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

Formatter (java.util.Formatter)558 ArrayList (java.util.ArrayList)26 File (java.io.File)25 IOException (java.io.IOException)25 Date (java.util.Date)22 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 MessageDigest (java.security.MessageDigest)14 PrintWriter (java.io.PrintWriter)13 Justif (aQute.lib.justif.Justif)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Locale (java.util.Locale)11 BigInteger (java.math.BigInteger)10 PrintStream (java.io.PrintStream)9 Calendar (java.util.Calendar)7 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 View (android.view.View)6