Refine search
private static String escapeUnicode(String input) { StringBuilder b = new StringBuilder(input.length()); Formatter formatter = new Formatter(b); for (char c : input.toCharArray()) { if (c < 128) { b.append(c); } else { formatter.format("\\u%04x", (int) c); } } formatter.close(); return b.toString(); } }
@Test public void testDefaultAppend() { assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1).toString()); assertEquals("fo", FormattableUtils.append("foo", new Formatter(), 0, -1, 2).toString()); assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1).toString()); assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1).toString()); assertEquals(" fo", FormattableUtils.append("foo", new Formatter(), 0, 3, 2).toString()); assertEquals(" fo", FormattableUtils.append("foo", new Formatter(), 0, 5, 2).toString()); assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1).toString()); assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1).toString()); assertEquals("fo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2).toString()); assertEquals("fo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2).toString()); }
private static String escapeUnicode(String input) { StringBuilder b = new StringBuilder(input.length()); Formatter formatter = new Formatter(b); for (char c : input.toCharArray()) { if (c < 128) { b.append(c); } else { formatter.format("\\u%04x", (int) c); } } formatter.close(); return b.toString(); } }
@Test public void testAlternatePadCharacter() { final char pad='_'; assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, pad).toString()); assertEquals("fo", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, pad).toString()); assertEquals("_foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, pad).toString()); assertEquals("___foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, pad).toString()); assertEquals("_fo", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, pad).toString()); assertEquals("___fo", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, pad).toString()); assertEquals("foo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, pad).toString()); assertEquals("foo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, pad).toString()); assertEquals("fo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, pad).toString()); assertEquals("fo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, pad).toString()); }
private List<URL> getDownloadURLsCached() { Envelope extent = graph.getExtent(); Formatter formatter = new Formatter(); String filename = formatter.format("%f,%f-%f,%f.urls", extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY()).toString(); formatter.close(); try { File file = new File(cacheDirectory, filename);
private void writeSingleInteger(String fmt, int num, StringBuffer output, List<Special> numSpecials, Set<CellNumberStringMod> mods) { StringBuffer sb = new StringBuffer(); Formatter formatter = new Formatter(sb, locale); try { formatter.format(locale, fmt, num); } finally { formatter.close(); } writeInteger(sb, output, numSpecials, mods, false); }
/** * Formats number of milliseconds to a HH:MM representation * * TODO: implement automatic scale (e.g. 1d 7h instead of 31:13m) * @param millis * @return */ public String toHHMM(long millis) { StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb); formatter .format("%2d:%2dm", millis / 1000 / 3600, (millis / 1000 / 60) % 60); return sb.toString(); }
@Test public void testEllipsis() { assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "*").toString()); assertEquals("f*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "*").toString()); assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "*").toString()); assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "*").toString()); assertEquals(" f*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "*").toString()); assertEquals(" f*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "*").toString()); assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "*").toString()); assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "*").toString()); assertEquals("f* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "*").toString()); assertEquals("f* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "*").toString()); assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "+*").toString()); assertEquals("+*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "+*").toString()); assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "+*").toString()); assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "+*").toString()); assertEquals(" +*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "+*").toString()); assertEquals(" +*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "+*").toString()); assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "+*").toString()); assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "+*").toString()); assertEquals("+* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "+*").toString()); assertEquals("+* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "+*").toString()); }
/** * Log a printf-style formatted message to the channels specified in this RedwoodChannels object. * @param format The format string for the printf function * @param args The arguments to the printf function */ public void logf(String format, Object... args) { log((Supplier<String>) () -> new Formatter().format(format, args).toString()); }
/** {@inheritDoc} */ public void formatValue(StringBuffer toAppendTo, Object value) { double elapsed = ((Number) value).doubleValue(); if (elapsed < 0) { toAppendTo.append('-'); elapsed = -elapsed; } Object[] parts = new Long[specs.size()]; for (int i = 0; i < specs.size(); i++) { parts[i] = specs.get(i).valueFor(elapsed); } Formatter formatter = new Formatter(toAppendTo, Locale.ROOT); try { formatter.format(printfFmt, parts); } finally { formatter.close(); } }
private static String determineMacAddress() { String formattedMac = "0"; try { InetAddress address = InetAddress.getLocalHost(); NetworkInterface ni = NetworkInterface.getByInetAddress( address ); if ( ni != null ) { byte[] mac = ni.getHardwareAddress(); if ( mac != null ) { StringBuilder sb = new StringBuilder( mac.length * 2 ); Formatter formatter = new Formatter( sb ); for ( byte b : mac ) { formatter.format( "%02x", b ); } formattedMac = sb.toString(); } } } catch ( Throwable t ) { // } return formattedMac; }