Example usage for org.apache.commons.lang3 StringUtils rightPad

List of usage examples for org.apache.commons.lang3 StringUtils rightPad

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils rightPad.

Prototype

public static String rightPad(final String str, final int size, String padStr) 

Source Link

Document

Right pad a String with a specified String.

The String is padded to the size of size .

 StringUtils.rightPad(null, *, *)      = null StringUtils.rightPad("", 3, "z")      = "zzz" StringUtils.rightPad("bat", 3, "yz")  = "bat" StringUtils.rightPad("bat", 5, "yz")  = "batyz" StringUtils.rightPad("bat", 8, "yz")  = "batyzyzy" StringUtils.rightPad("bat", 1, "yz")  = "bat" StringUtils.rightPad("bat", -1, "yz") = "bat" StringUtils.rightPad("bat", 5, null)  = "bat  " StringUtils.rightPad("bat", 5, "")    = "bat  " 

Usage

From source file:fr.gouv.diplomatie.applitutoriel.utility.Graphique.java

/**
 * permet de soit complter le libell pour viter d'avoir 2 lgendes sur la mme ligne, soit de dcouper
 * le libell s'il ne loge pas en longueur sur une seule ligne.
 *
 * @param chaine/* w  w w  .j  a  va  2  s  .  c  om*/
 *            the chaine
 * @param longueur
 *            the longueur
 * @return the string
 */
public static String genererLibellePourLegende(final String chaine, final int longueur) {

    final StringBuffer retour = new StringBuffer();

    if (chaine.length() <= longueur) {

        retour.append(StringUtils.rightPad(chaine, longueur, ' '));
    } else {

        retour.append(chaine);
        final int index = StringUtils.lastIndexOf(" ", chaine.substring(0, longueur));

        if (index > 0) {
            retour.replace(index, index + 1, "\r\n");
        }

    }

    return retour.toString();
}

From source file:com.spectralogic.ds3contractcomparator.print.utils.SimplePrinterUtils.java

/**
 * Prints a line denoting the value of an element within two versions
 * of a contract. If the value changed between versions, then an arrow
 * is printed between the older version to the newer version. If both
 * values are either empty or null, then the item is not printed.
 * @param label The label denoting what category is being printed
 * @param oldVal The older contract version of the value
 * @param newVal The newer contract version fo the value
 *///ww w. j ava 2s.  com
public static void printModifiedLine(final String label, final String oldVal, final String newVal,
        final int indent, final WriterHelper writer) {
    if (isEmpty(oldVal) && isEmpty(newVal)) {
        //Don't print an empty value
        return;
    }
    final String printFormat = getPrintStringFormat(indent);
    if (oldVal.equals(newVal)) {
        writer.append(String.format(printFormat, label, oldVal, newVal));
        return;
    }
    //Print value with arrow emphasizing change
    writer.append(String.format(printFormat, label, StringUtils.rightPad(oldVal, COLUMN_WIDTH - 1, '-') + ">",
            newVal));

}

From source file:au.com.addstar.SpigotUpdater.java

private static void doOutHeader(List<Plugin> plugins) {
    List<String> out = new ArrayList<>();
    System.out.println("Processing " + plugins.size() + " plugins.... ");
    String name = StringUtils.rightPad("Plugin Name", 25, " ");
    out.add(name);/*  w  ww.  ja  v  a2s  .co  m*/
    out.add(StringUtils.rightPad("Res. ID", 7));
    out.add(StringUtils.rightPad("Version", 10));
    out.add(StringUtils.rightPad("Latest", 10));
    out.add(StringUtils.rightPad("Update?", 7));
    out.add(StringUtils.rightPad("Date Upd.", 10));
    out.add("Extra Notes");
    StringBuilder sb = new StringBuilder();
    String[] message = new String[out.size()];
    out.toArray(message);
    sb.append(StringUtils.join(message, " | "));
    System.out.println(sb.toString());
    System.out.println(StringUtils.rightPad("", sb.length(), "-"));
}

From source file:monasca.api.infrastructure.persistence.influxdb.InfluxV9Utils.java

public String threeDigitMillisTimestamp(String origTimestamp) {
    final int length = origTimestamp.length();
    final String timestamp;
    if (length == 20) {
        timestamp = origTimestamp.substring(0, 19) + ".000Z";
    } else {/*from ww w. j  a  v a2  s  . co  m*/
        final String millisecond = origTimestamp.substring(20, length - 1);
        final String millisecond_3d = StringUtils.rightPad(millisecond, 3, '0');
        timestamp = origTimestamp.substring(0, 19) + '.' + millisecond_3d + 'Z';
    }
    return timestamp;
}

From source file:com.netflix.imfutility.ttmltostl.stl.StlTtiTest.java

/**
 * Each text must end with 0x8f!// ww  w. ja  v a2 s . c  o  m
 *
 * @throws Exception
 */
@Test
public void testTextEndsWith8H() throws Exception {
    // prepare long subtitles, so that one subtitles is stored in two tti blocks
    TimedTextObject tto = StlTestUtil.buildTto("10:00:00:00", "10:00:05:00", StringUtils.rightPad("", 111, '1'), // 1 block
            "10:00:05:00", "10:00:10:00", StringUtils.rightPad("", 112, '2'), // 2 blocks
            "10:00:10:00", "10:00:15:00", StringUtils.rightPad("", 222, '3'), // 2 blocks
            "10:00:15:00", "10:00:20:00", StringUtils.rightPad("", 223, '4') // 3 blocks
    );
    byte[][] stl = StlTestUtil.build(tto, StlTestUtil.getMetadataXml());
    byte[] tti = stl[1];

    // 1st subtitle  - 1 block
    int offset = 128; // subtitle zero
    byte[] textWithPadding = new byte[112];
    Arrays.fill(textWithPadding, (byte) 0x31);
    textWithPadding[111] = (byte) 0x8f;
    assertArrayEquals(new byte[] { 0x01, 0x00, // subtitle number - 1
            (byte) 0xff, // extension block - last
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    // 2d subtitle  - 2 blocks
    offset += 128;
    textWithPadding = new byte[112];
    Arrays.fill(textWithPadding, (byte) 0x32);
    textWithPadding[111] = (byte) 0x8f;
    assertArrayEquals(new byte[] { 0x02, 0x00, // subtitle number - 2
            (byte) 0x00, // extension block - 1st
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    offset += 128;
    textWithPadding = new byte[112];
    Arrays.fill(textWithPadding, (byte) 0x8f);
    textWithPadding[0] = (byte) 0x32;
    assertArrayEquals(new byte[] { 0x02, 0x00, // subtitle number - 2
            (byte) 0xff, // extension block - last
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    // 3d subtitle  - 2 blocks
    offset += 128;
    textWithPadding = new byte[112];
    Arrays.fill(textWithPadding, (byte) 0x33);
    textWithPadding[111] = (byte) 0x8f;
    assertArrayEquals(new byte[] { 0x03, 0x00, // subtitle number - 3
            (byte) 0x00, // extension block - 1st
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    offset += 128;
    assertArrayEquals(new byte[] { 0x03, 0x00, // subtitle number - 3
            (byte) 0xff, // extension block - last
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    // 4th subtitle  - 3 blocks
    offset += 128;
    textWithPadding = new byte[112];
    Arrays.fill(textWithPadding, (byte) 0x34);
    textWithPadding[111] = (byte) 0x8f;
    assertArrayEquals(new byte[] { 0x04, 0x00, // subtitle number - 4
            (byte) 0x00, // extension block - 1st
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    offset += 128;
    assertArrayEquals(new byte[] { 0x04, 0x00, // subtitle number - 4
            (byte) 0x01, // extension block - last
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

    offset += 128;
    textWithPadding = new byte[112];
    Arrays.fill(textWithPadding, (byte) 0x8f);
    textWithPadding[0] = (byte) 0x34;
    assertArrayEquals(new byte[] { 0x04, 0x00, // subtitle number - 4
            (byte) 0xff, // extension block - last
    }, Arrays.copyOfRange(tti, offset + 1, offset + 4));
    assertArrayEquals(textWithPadding, Arrays.copyOfRange(tti, offset + 16, offset + 128));

}

From source file:at.beris.virtualfile.shell.Shell.java

private void list(boolean local) throws IOException {
    File file = local ? localFile : workingFile;

    if (file == null && !local) {
        System.out.println(NOT_CONNECTED_MSG);
        return;//from w  w w  . j  a  v a  2s . com
    }

    int maxLengthOwner = 0, maxLengthGroup = 0, maxLengthSize = 0;
    int maxLengthDateStr = 0, maxLengthTimeStr = 0;

    StringBuilder sb = new StringBuilder();
    List<ExtFileModel> fileModelList = new ArrayList<>();
    for (File childFile : file.list()) {
        ExtFileModel model = new ExtFileModel();
        model.setUrl(childFile.getUrl());
        model.setAttributes(childFile.getAttributes());
        model.setDirectory(childFile.isDirectory());
        model.setOwner(childFile.getOwner());
        model.setGroup(childFile.getGroup());
        model.setSize(childFile.getSize());
        model.setLastModifiedTime(childFile.getLastModifiedTime());

        UserPrincipal owner = model.getOwner();
        if (owner != null && maxLengthOwner < owner.getName().length())
            maxLengthOwner = model.getOwner().getName().length();
        GroupPrincipal group = model.getGroup();
        if (group != null && maxLengthGroup < group.getName().length())
            maxLengthGroup = model.getGroup().getName().length();
        String sizeString = String.valueOf(model.getSize());
        if (maxLengthSize < sizeString.length())
            maxLengthSize = sizeString.length();

        if (model.getLastModifiedTime() != null) {
            Date lastModifiedDate = new Date(model.getLastModifiedTime().toMillis());
            model.dateStr = DATE_FORMATTER.format(lastModifiedDate);
            if (maxLengthDateStr < model.dateStr.length())
                maxLengthDateStr = model.dateStr.length();
            model.timeStr = TIME_FORMATTER.format(lastModifiedDate);
            if (maxLengthTimeStr < model.timeStr.length())
                maxLengthTimeStr = model.timeStr.length();
        } else {
            model.dateStr = "";
            model.timeStr = "";
        }

        fileModelList.add(model);
    }

    for (ExtFileModel model : fileModelList) {
        sb.setLength(0);
        sb.append(model.isDirectory() ? 'd' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OWNER_READ) ? 'r' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OWNER_WRITE) ? 'w' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OWNER_EXECUTE) ? 'x' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.GROUP_READ) ? 'r' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.GROUP_WRITE) ? 'w' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.GROUP_EXECUTE) ? 'x' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OTHERS_READ) ? 'r' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OTHERS_WRITE) ? 'w' : '-');
        sb.append(model.getAttributes().contains(PosixFilePermission.OTHERS_EXECUTE) ? 'x' : '-').append(' ');
        sb.append(StringUtils.rightPad(model.getOwner().getName(), maxLengthOwner, ' ')).append(' ');
        sb.append(StringUtils.rightPad(model.getGroup().getName(), maxLengthGroup, ' ')).append(' ');
        sb.append(StringUtils.leftPad(String.valueOf(model.getSize()), maxLengthSize, ' ')).append(' ');
        sb.append(StringUtils.leftPad(model.dateStr, maxLengthDateStr, ' ')).append(' ');
        sb.append(StringUtils.leftPad(model.timeStr, maxLengthTimeStr, ' ')).append(' ');
        sb.append(FileUtils.getName(model.getUrl().toString()));
        System.out.println(sb.toString());
    }

    for (FileModel fileModel : fileModelList)
        fileModel.clear();
    fileModelList.clear();
}

From source file:com.jkoolcloud.tnt4j.core.UsecTimestamp.java

/**
 * <p>Creates UsecTimestamp from string representation of timestamp in the
 * specified format.</p>/*from  w  w w.  ja  v  a2 s.co  m*/
 * <p>This is based on {@link SimpleDateFormat}, but extends its support to
 * recognize microsecond fractional seconds. If number of fractional second
 * characters is greater than 3, then it's assumed to be microseconds.
 * Otherwise, it's assumed to be milliseconds (as this is the behavior of
 * {@link SimpleDateFormat}.
 *
 * @param timeStampStr timestamp string
 * @param formatStr format specification for timestamp string
 * @param timeZone time zone that timeStampStr represents. This is only needed when formatStr does not include
 *                   time zone specification and timeStampStr does not represent a string in local time zone.
 * @param locale locale for date format to use.
 * @throws NullPointerException if timeStampStr is {@code null}
 * @throws IllegalArgumentException if timeStampStr is not in the correct format
 * @throws ParseException if failed to parse string based on specified format
 * @see java.util.TimeZone
 */
public UsecTimestamp(String timeStampStr, String formatStr, TimeZone timeZone, String locale)
        throws ParseException {
    if (timeStampStr == null)
        throw new NullPointerException("timeStampStr must be non-null");

    int usecs = 0;

    SimpleDateFormat dateFormat;

    if (StringUtils.isEmpty(formatStr)) {
        dateFormat = new SimpleDateFormat();
    } else {
        // Java date formatter cannot deal with usecs, so we need to extract those ourselves
        int fmtPos = formatStr.indexOf('S');
        if (fmtPos > 0) {
            int endFmtPos = formatStr.lastIndexOf('S');
            int fmtFracSecLen = endFmtPos - fmtPos + 1;

            if (fmtFracSecLen > 6)
                throw new ParseException(
                        "Date format containing more than 6 significant digits for fractional seconds is not supported",
                        0);

            StringBuilder sb = new StringBuilder();
            int usecPos = timeStampStr.lastIndexOf('.') + 1;
            int usecEndPos;
            if (usecPos > 2) {
                for (usecEndPos = usecPos; usecEndPos < timeStampStr.length(); usecEndPos++) {
                    if (!StringUtils.containsAny("0123456789", timeStampStr.charAt(usecEndPos)))
                        break;
                }

                if (fmtFracSecLen > 3) {
                    // format specification represents more than milliseconds, assume microseconds
                    String usecStr = String.format("%s", timeStampStr.substring(usecPos, usecEndPos));
                    if (usecStr.length() < fmtFracSecLen)
                        usecStr = StringUtils.rightPad(usecStr, fmtFracSecLen, '0');
                    else if (usecStr.length() > fmtFracSecLen)
                        usecStr = usecStr.substring(0, fmtFracSecLen);
                    usecs = Integer.parseInt(usecStr);

                    // trim off fractional part < microseconds from both timestamp and format strings
                    sb.append(timeStampStr);
                    sb.delete(usecPos - 1, usecEndPos);
                    timeStampStr = sb.toString();

                    sb.setLength(0);
                    sb.append(formatStr);
                    sb.delete(fmtPos - 1, endFmtPos + 1);
                    formatStr = sb.toString();
                } else if ((usecEndPos - usecPos) < 3) {
                    // pad msec value in date string with 0's so that it is 3 digits long
                    sb.append(timeStampStr);
                    while ((usecEndPos - usecPos) < 3) {
                        sb.insert(usecEndPos, '0');
                        usecEndPos++;
                    }
                    timeStampStr = sb.toString();
                }
            }
        }

        dateFormat = StringUtils.isEmpty(locale) ? new SimpleDateFormat(formatStr)
                : new SimpleDateFormat(formatStr, Utils.getLocale(locale));
    }

    dateFormat.setLenient(true);
    if (timeZone != null)
        dateFormat.setTimeZone(timeZone);

    Date date = dateFormat.parse(timeStampStr);

    setTimestampValues(date.getTime(), 0, 0);
    add(0, usecs);
}

From source file:com.netflix.imfutility.ttmltostl.stl.StlGsiTest.java

@Test
public void testNumberOfSubtitles() throws Exception {
    // prepare long subtitles, so that one subtitles is stored in two tti blocks
    TimedTextObject tto = StlTestUtil.buildTto("10:00:00:00", "10:00:05:00",
            StringUtils.rightPad("test", 200, '1'), // in 2 tti
            "10:00:05:00", "10:00:10:00", StringUtils.rightPad("test", 300, '2'), // in 3 tti
            "10:00:10:00", "10:01:10:00", StringUtils.rightPad("test", 400, '3') // in 4 tti
    );//from   ww w. j  ava2  s  .co m
    byte[][] stl = StlTestUtil.build(tto, StlTestUtil.getMetadataXml());
    byte[] gsi = stl[0];

    assertArrayEquals(new byte[] { 0x30, 0x30, 0x30, 0x31, 0x30 // number of tti blocks - 9 + subtitle zero = 10
    }, Arrays.copyOfRange(gsi, 238, 243));

    assertArrayEquals(new byte[] { 0x30, 0x30, 0x30, 0x30, 0x34 // number of subtitles - 3 + subtitle zero = 4
    }, Arrays.copyOfRange(gsi, 243, 248));

    assertArrayEquals(new byte[] { 0x30, 0x30, 0x31 // number of subtitle groups - 1
    }, Arrays.copyOfRange(gsi, 248, 251));
}

From source file:com.thoughtworks.go.server.controller.AgentRegistrationControllerTest.java

@Test
public void shouldSendAnEmptyStringInBase64_AsAgentExtraProperties_IfTheValueIsTooBigAfterConvertingToBase64()
        throws IOException {
    final String longExtraPropertiesValue = StringUtils.rightPad("",
            AgentRegistrationController.MAX_HEADER_LENGTH, "z");
    final String expectedValueToBeUsedForProperties = "";
    final String expectedBase64ExtraPropertiesValue = getEncoder()
            .encodeToString(expectedValueToBeUsedForProperties.getBytes(UTF_8));

    when(systemEnvironment.get(AGENT_EXTRA_PROPERTIES)).thenReturn(longExtraPropertiesValue);

    controller.downloadAgent(response);/*from   w w  w.  j ava  2 s  .  c  om*/

    assertEquals(expectedBase64ExtraPropertiesValue,
            response.getHeader(SystemEnvironment.AGENT_EXTRA_PROPERTIES_HEADER));
}

From source file:com.github.tomakehurst.wiremock.RecordingDslAcceptanceTest.java

@Test
public void defaultsToWritingTextResponseFilesOver1Kb() {
    targetService.stubFor(get("/large.txt").willReturn(
            aResponse().withHeader(CONTENT_TYPE, "text/plain").withBody(StringUtils.rightPad("", 10241, 'a'))));

    proxyingService.startRecording(recordSpec().forTarget(targetBaseUrl));

    client.get("/large.txt");

    List<StubMapping> mappings = proxyingService.stopRecording().getStubMappings();
    StubMapping mapping = mappings.get(0);
    String bodyFileName = mapping.getResponse().getBodyFileName();

    assertThat(bodyFileName, is("largetxt-" + mapping.getId() + ".txt"));
    File bodyFile = new File(fileRoot, "__files/" + bodyFileName);
    assertThat(bodyFile.exists(), is(true));
}