/** * <p>Left pad a String with spaces (' ').</p> * * <p>The String is padded to the size of {@code size}.</p> * * <pre> * StringUtils.leftPad(null, *) = null * StringUtils.leftPad("", 3) = " " * StringUtils.leftPad("bat", 3) = "bat" * StringUtils.leftPad("bat", 5) = " bat" * StringUtils.leftPad("bat", 1) = "bat" * StringUtils.leftPad("bat", -1) = "bat" * </pre> * * @param str the String to pad out, may be null * @param size the size to pad to * @return left padded String or original String if no padding is necessary, * {@code null} if null String input */ public static String leftPad(final String str, final int size) { return leftPad(str, size, ' '); }
/** * <p>Converts a {@code long} to a {@code String} with optional * zero padding.</p> * * @param value the value to convert * @param padWithZeros whether to pad with zeroes * @param count the size to pad to (ignored if {@code padWithZeros} is false) * @return the string result */ private static String paddedValue(final long value, final boolean padWithZeros, final int count) { final String longString = Long.toString(value); return padWithZeros ? StringUtils.leftPad(longString, count, '0') : longString; }
public static String toFlagString(int state) { return StringUtils.leftPad(Integer.toBinaryString(state), REFCOUNT_SHIFT, '0'); } }
/** * Returns the bytes corresponding to the BigInteger * * @param bigInteger number to convert * @param pad padding length * @return byte corresponding to input BigInteger */ public byte[] convertToByte(BigInteger bigInteger, int pad) { String bigIntegerString = bigInteger.toString(radix); bigIntegerString = StringUtils.leftPad(bigIntegerString, pad, '0'); return Bytes.toBytes(bigIntegerString); }
public TableCreator addSeparator() { sb.append(StringUtils.leftPad("", rowWidth, '-')).append("\r\n"); return this; }
return leftPad(str, size, String.valueOf(padChar));
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 static String calculateIbanChecksum(String countryCode, String basicBankAccountNumber) { String basis = basicBankAccountNumber + countryCode + "00"; StringBuilder sb = new StringBuilder(); char[] characters = basis.toLowerCase().toCharArray(); for (char c : characters) { if (Character.isLetter(c)) { sb.append(String.valueOf((c - 'a') + 10)); } else { sb.append(c); } } int mod97 = new BigInteger(sb.toString()).mod(BigInteger.valueOf(97L)).intValue(); return StringUtils.leftPad(String.valueOf(98 - mod97), 2, '0'); }
return str; str = leftPad(str, strLen + pads / 2, padChar); str = rightPad(str, size, padChar); return str;
return leftPad(str, size, padStr.charAt(0));
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); }
return str; str = leftPad(str, strLen + pads / 2, padStr); str = rightPad(str, size, padStr); return str;
private String getPrefix(long i) { return StringUtils.leftPad(String.valueOf((int)(i % numPartitions)), PREFIX_PAD_TO, "0"); }
private String getEstimatedTimeRemaining() { // Base data final double drainRate = getPrayerDrainRate(client); if (drainRate == 0) { return "N/A"; } final int currentPrayer = client.getBoostedSkillLevel(Skill.PRAYER); // Calculate how many seconds each prayer points last so the prayer bonus can be applied final double secondsPerPoint = (60.0 / drainRate) * (1.0 + (prayerBonus / 30.0)); // Calculate the number of seconds left final double secondsLeft = (currentPrayer * secondsPerPoint); final int minutes = (int) Math.floor(secondsLeft / 60.0); final int seconds = (int) Math.floor(secondsLeft - (minutes * 60.0)); // Return the text return Integer.toString(minutes) + ":" + StringUtils.leftPad(Integer.toString(seconds), 2, "0"); } }
@Test public void testLeftPad_StringIntString() { assertNull(StringUtils.leftPad(null, 5, "-+")); assertNull(StringUtils.leftPad(null, 5, null)); assertEquals(" ", StringUtils.leftPad("", 5, " ")); assertEquals("-+-+abc", StringUtils.leftPad("abc", 7, "-+")); assertEquals("-+~abc", StringUtils.leftPad("abc", 6, "-+~")); assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~")); assertEquals("abc", StringUtils.leftPad("abc", 2, " ")); assertEquals("abc", StringUtils.leftPad("abc", -1, " ")); assertEquals(" abc", StringUtils.leftPad("abc", 5, null)); assertEquals(" abc", StringUtils.leftPad("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; }
@Test public void testLeftPad_StringIntChar() { assertNull(StringUtils.leftPad(null, 5, ' ')); assertEquals(" ", StringUtils.leftPad("", 5, ' ')); assertEquals(" abc", StringUtils.leftPad("abc", 5, ' ')); assertEquals("xxabc", StringUtils.leftPad("abc", 5, 'x')); assertEquals("\uffff\uffffabc", StringUtils.leftPad("abc", 5, '\uffff')); assertEquals("abc", StringUtils.leftPad("abc", 2, ' ')); final String str = StringUtils.leftPad("aaa", 10000, 'a'); // bigger than pad length assertEquals(10000, str.length()); assertTrue(StringUtils.containsOnly(str, 'a')); }
@Override public byte[] getDeterministicUniqueKey(long keyBase) { String num = StringUtils.leftPad(String.valueOf(keyBase), PAD_TO, "0"); return Bytes.toBytes(getPrefix(keyBase) + num); }
@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(); }