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());
}
}
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);
}
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();
}
});
}
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);
}
}
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);
}
}
Aggregations