/** * <p>Right pad a String with spaces (' ').</p> * * <p>The String is padded to the size of {@code size}.</p> * * <pre> * StringUtils.rightPad(null, *) = null * StringUtils.rightPad("", 3) = " " * StringUtils.rightPad("bat", 3) = "bat" * StringUtils.rightPad("bat", 5) = "bat " * StringUtils.rightPad("bat", 1) = "bat" * StringUtils.rightPad("bat", -1) = "bat" * </pre> * * @param str the String to pad out, may be null * @param size the size to pad to * @return right padded String or original String if no padding is necessary, * {@code null} if null String input */ public static String rightPad(final String str, final int size) { return rightPad(str, size, ' '); }
return rightPad(str, size, String.valueOf(padChar));
public TableCreator addRow(String rowHeader, Object rowData) { String trimmed = StringUtils.left(rowHeader, globalRowHeaderWidth); sb.append("| ") .append(StringUtils.rightPad(trimmed, globalRowHeaderWidth)) .append(StringUtils.rightPad(String.valueOf(rowData), rowWidth - globalRowHeaderWidth - 3)) .append("|\r\n"); return this; }
public TableCreator addRow(Object[] data) { if (data.length != cols.length) { throw new IllegalArgumentException("Wrong number of data elements. Needed [" + cols.length + "] " + "but received [" + data.length + "]"); } sb.append('|'); for (int i = 0; i < data.length; i++) { String trimmed = StringUtils.left(String.valueOf(data[i]), cols[i].width); sb.append(' ').append(StringUtils.rightPad(trimmed, cols[i].width)).append(" |"); } sb.append("\r\n"); return this; }
str = rightPad(str, size, padChar); return str;
return rightPad(str, size, padStr.charAt(0));
str = rightPad(str, size, padStr); return str;
private void renderMeasurement(final String label, final TimedResult timedResult, final Writer writer) throws IOException { writer.write(StringUtils.rightPad(label, LABEL_COLUMN_WIDTH)); final String time = MessageFormat.format(TIME_FORMAT, timedResult.totalTimeNanos.get() / 1000000000.0); writer.write(StringUtils.leftPad(time, TIME_COLUMN_WIDTH)); final String selfTime = MessageFormat.format(TIME_FORMAT, timedResult.selfTimeNanos.get() / 1000000000.0); writer.write(StringUtils.leftPad(selfTime, SELF_TIME_COLUMN_WIDTH)); if (timedResult.callCount.get() > 0) { final String callCount = MessageFormat.format(CUSTOM_COUNTER_FORMAT, timedResult.callCount.get()); writer.write(StringUtils.leftPad(callCount, CALL_COLUMN_WIDTH)); if (timedResult.extraDataCounter.get() > 0) { final String counter = MessageFormat.format(CUSTOM_COUNTER_FORMAT, timedResult.extraDataCounter.get()); writer.write(StringUtils.leftPad(counter, COUNTER_COLUMN_WIDTH)); } } writer.write(PMD.EOL); }
private void renderHeader(final String displayName, final Writer writer) throws IOException { final StringBuilder sb = new StringBuilder(COLUMNS) .append(displayName); // Make sure we have an even-length string if (displayName.length() % 2 == 1) { sb.append(' '); } // Surround with <<< and >>> sb.insert(0, "<<< ").append(" >>>"); // Create the ruler while (sb.length() < COLUMNS) { sb.insert(0, '-').append('-'); } writer.write(sb.toString()); writer.write(PMD.EOL); // Write table titles writer.write(StringUtils.rightPad("Label", LABEL_COLUMN_WIDTH)); writer.write(StringUtils.leftPad("Time (secs)", TIME_COLUMN_WIDTH)); writer.write(StringUtils.leftPad("Self Time (secs)", SELF_TIME_COLUMN_WIDTH)); writer.write(StringUtils.leftPad("# Calls", CALL_COLUMN_WIDTH)); writer.write(StringUtils.leftPad("Counter", COUNTER_COLUMN_WIDTH)); writer.write(PMD.EOL); writer.write(PMD.EOL); }
@Test public void testRightPad_StringIntString() { assertNull(StringUtils.rightPad(null, 5, "-+")); assertEquals(" ", StringUtils.rightPad("", 5, " ")); assertNull(StringUtils.rightPad(null, 8, null)); assertEquals("abc-+-+", StringUtils.rightPad("abc", 7, "-+")); assertEquals("abc-+~", StringUtils.rightPad("abc", 6, "-+~")); assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~")); assertEquals("abc", StringUtils.rightPad("abc", 2, " ")); assertEquals("abc", StringUtils.rightPad("abc", -1, " ")); assertEquals("abc ", StringUtils.rightPad("abc", 5, null)); assertEquals("abc ", StringUtils.rightPad("abc", 5, "")); }
String formatCell(Object value, int column) { int width = columnWidths.get(column); TextTableAlignment align = columnAlignments.get(column); Function<Object, String> formatter = columnFormatters.get(column); String content = formatter.apply(value); if (content.length() > width) { // truncate content = StringUtils.abbreviate(content, width); } else if (content.length() < width) { // add padding switch (align) { case LEFT: content = StringUtils.rightPad(content, width); break; case RIGHT: content = StringUtils.leftPad(content, width); break; case CENTER: content = StringUtils.center(content, width); break; } } return content; }
/** * Return the ith row of the column as a set of wrapped strings, each at * most wrapWidth in length. */ String[] getRow(int idx) { String raw = rows.get(idx); // Line-wrap if it's too long String[] lines = new String[] {raw}; if (wrap) { lines = org.apache.hadoop.util.StringUtils.wrap(lines[0], wrapWidth, "\n", true).split("\n"); } for (int i=0; i<lines.length; i++) { if (justification == Justification.LEFT) { lines[i] = StringUtils.rightPad(lines[i], maxWidth); } else if (justification == Justification.RIGHT) { lines[i] = StringUtils.leftPad(lines[i], maxWidth); } } return lines; } }
@Test public void testRightPad_StringIntChar() { assertNull(StringUtils.rightPad(null, 5, ' ')); assertEquals(" ", StringUtils.rightPad("", 5, ' ')); assertEquals("abc ", StringUtils.rightPad("abc", 5, ' ')); assertEquals("abc", StringUtils.rightPad("abc", 2, ' ')); assertEquals("abc", StringUtils.rightPad("abc", -1, ' ')); assertEquals("abcxx", StringUtils.rightPad("abc", 5, 'x')); final String str = StringUtils.rightPad("aaa", 10000, 'a'); // bigger than pad length assertEquals(10000, str.length()); assertTrue(StringUtils.containsOnly(str, 'a')); }
CIPHERTEXT_B64 = StringUtils.rightPad(CIPHERTEXT_B64, paddedLength, '=');
@Override public void render(final TimingReport report, final Writer writer) throws IOException { for (final TimedOperationCategory category : TimedOperationCategory.values()) { final Map<String, TimedResult> labeledMeasurements = report.getLabeledMeasurements(category); if (!labeledMeasurements.isEmpty()) { renderCategoryMeasurements(category, labeledMeasurements, writer); } } renderHeader("Summary", writer); for (final TimedOperationCategory category : TimedOperationCategory.values()) { final TimedResult timedResult = report.getUnlabeledMeasurements(category); if (timedResult != null) { renderMeasurement(category.displayName(), timedResult, writer); } } writer.write(PMD.EOL); renderHeader("Total", writer); writer.write(StringUtils.rightPad("Wall Clock Time", LABEL_COLUMN_WIDTH)); final String wallClockTime = MessageFormat.format(TIME_FORMAT, report.getWallClockMillis() / 1000.0); writer.write(StringUtils.leftPad(wallClockTime, TIME_COLUMN_WIDTH)); writer.write(PMD.EOL); writer.flush(); }
/** * Returns the nanoseconds within the current second * <p> * Note that this method returns the * same value as {@link #getMillis()} but with more precision. * </p> */ public Long getNanos() { if (isBlank(myFractionalSeconds)) { return null; } String retVal = StringUtils.rightPad(myFractionalSeconds, 9, '0'); retVal = retVal.substring(0, 9); return Long.parseLong(retVal); }
/** * Returns the nanoseconds within the current second * <p> * Note that this method returns the * same value as {@link #getMillis()} but with more precision. * </p> */ public Long getNanos() { if (isBlank(myFractionalSeconds)) { return null; } String retVal = StringUtils.rightPad(myFractionalSeconds, 9, '0'); retVal = retVal.substring(0, 9); return Long.parseLong(retVal); }
/** * Returns the nanoseconds within the current second * <p> * Note that this method returns the * same value as {@link #getMillis()} but with more precision. * </p> */ public Long getNanos() { if (isBlank(myFractionalSeconds)) { return null; } String retVal = StringUtils.rightPad(myFractionalSeconds, 9, '0'); retVal = retVal.substring(0, 9); return Long.parseLong(retVal); }
/** * Constructor with service bytes array parameter * * @param pData * service as byte array */ public Service(final String pData) { if (pData != null && pData.length() == 3) { BitUtils bit = new BitUtils(BytesUtils.fromString(StringUtils.rightPad(pData, 4, "0"))); serviceCode1 = EnumUtils.getValue(bit.getNextInteger(4), ServiceCode1Enum.class); serviceCode2 = EnumUtils.getValue(bit.getNextInteger(4), ServiceCode2Enum.class); serviceCode3 = EnumUtils.getValue(bit.getNextInteger(4), ServiceCode3Enum.class); } }