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

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

Introduction

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

Prototype

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

Source Link

Document

Right pad a String with a specified String.

Usage

From source file:org.kuali.ole.sys.report.BusinessObjectReportHelper.java

/**
 * Returns multiple lines of what represent a table header. The last line in this list is the format of the table cells.
 * //w w w . ja  v a 2s.c  o m
 * @param maximumPageWidth maximum before line is out of bounds. Used to fill message to the end of this range. Note that if
 *        there isn't at least maximumPageWidth characters available it will go minimumMessageLength out of bounds. It is up to
 *        the calling class to handle that
 * @return table header. Last element is the format of the table cells.
 */
public List<String> getTableHeader(int maximumPageWidth) {
    String separatorLine = StringUtils.EMPTY;
    String messageFormat = StringUtils.EMPTY;

    // Construct the header based on orderedPropertyNameToHeaderLabelMap. It will pick the longest of label or DD size
    for (Iterator<Map.Entry<String, String>> entries = orderedPropertyNameToHeaderLabelMap.entrySet()
            .iterator(); entries.hasNext();) {
        Map.Entry<String, String> entry = entries.next();

        int longest;
        try {
            longest = retrievePropertyValueMaximumLength(dataDictionaryBusinessObjectClass, entry.getKey());
        } catch (Exception e) {
            throw new RuntimeException("Failed getting propertyName=" + entry.getKey()
                    + " from businessObjecName=" + dataDictionaryBusinessObjectClass.getName(), e);
        }
        if (entry.getValue().length() > longest) {
            longest = entry.getValue().length();
        }

        separatorLine = separatorLine + StringUtils.rightPad("", longest, OLEConstants.DASH) + " ";
        messageFormat = messageFormat + "%-" + longest + "s ";
    }

    // Now fill to the end of pageWidth for the message column. If there is not enough space go out of bounds
    int availableWidth = maximumPageWidth - (separatorLine.length() + 1);
    if (availableWidth < minimumMessageLength) {
        availableWidth = minimumMessageLength;
    }
    separatorLine = separatorLine + StringUtils.rightPad("", availableWidth, OLEConstants.DASH);
    messageFormat = messageFormat + "%-" + availableWidth + "s";

    // Fill in the header labels. We use the errorFormat to do this to get justification right
    List<Object> formatterArgs = new ArrayList<Object>();
    formatterArgs.addAll(orderedPropertyNameToHeaderLabelMap.values());
    formatterArgs.add(messageLabel);
    String tableHeaderLine = String.format(messageFormat, formatterArgs.toArray());

    // Construct return list
    List<String> tableHeader = new ArrayList<String>();
    tableHeader.add(tableHeaderLine);
    tableHeader.add(separatorLine);
    tableHeader.add(messageFormat);

    return tableHeader;
}

From source file:org.kuali.ole.sys.report.BusinessObjectReportHelper.java

/**
 * get the separator line/*from w w  w  . ja v a2s  .  com*/
 * @param cellWidthList the given cell width list
 * @return the separator line
 */
public String getSepartorLine(List<Integer> cellWidthList) {
    StringBuffer separatorLine = new StringBuffer();

    for (int index = 0; index < this.columnCount; index++) {
        Integer cellWidth = cellWidthList.get(index);
        separatorLine = separatorLine
                .append(StringUtils.rightPad(StringUtils.EMPTY, cellWidth, OLEConstants.DASH)).append(" ");
    }

    return separatorLine.toString();
}

From source file:org.mule.module.datapack.DelimitedOutputTransformer.java

@Override
public Object datapack(MuleMessage message, String outputEncoding) throws TransformerException {
    this.generateHeadersIfNecessary(message);

    StringBuilder output = new StringBuilder();

    for (int i = 0; i < columns.size(); i++) {
        Column column = columns.get(i);//from w  ww. j av  a  2 s  . c  om
        String value = this.evaluate(message, column);

        String encloseChar = determineEncloseChar(column);

        // Opening enclose 
        this.encloseIfNessesary(output, encloseChar);

        int columnLength = 0;
        if (!StringUtils.isEmpty(column.getLength())) {
            try {
                columnLength = Integer.parseInt(column.getLength());
            } catch (NumberFormatException e) {
                columnLength = 0;
            }
        }

        if (columnLength > 0) {
            int vlen = value.length();

            if (this.trimToLength) {
                value = value.substring(0, vlen < columnLength ? vlen : columnLength);
            } else if (this.fillToLength && vlen < columnLength) {
                value = StringUtils.rightPad(value, columnLength, this.fillLengthChar);
            } else if (this.prefixToLength && vlen < columnLength) {
                value = StringUtils.leftPad(value, columnLength, this.fillLengthChar);
            }
        }

        output.append(value);

        // check to see if a space is needed
        if (addSpace && value.length() == 0) {
            output.append(' ');
        }

        // Closing enclose 
        this.encloseIfNessesary(output, encloseChar);

        // column marked as a linebreak
        if (column.getLineBreak() != null && Boolean.parseBoolean(column.getLineBreak())) {
            output.append(newlineChar);
        }
        // Only put the delimiter on everything except for the last column or column marked as line break
        else if (i < columns.size() - 1) {
            output.append(delimiterChar);
        }
    }

    output.append(newlineChar);

    return output.toString();
}

From source file:org.onosproject.t3.cli.TroubleshootMcastCommand.java

@Override
protected void execute() {
    TroubleshootService service = get(TroubleshootService.class);
    print("Tracing all Multicast routes in the System");

    //Create the generator for the list of traces.
    VlanId vlanId = vlan == null || vlan.isEmpty() ? VlanId.NONE : VlanId.vlanId(vlan);
    Generator<Set<StaticPacketTrace>> generator = service.traceMcast(vlanId);
    int totalTraces = 0;
    List<StaticPacketTrace> failedTraces = new ArrayList<>();
    StaticPacketTrace previousTrace = null;
    while (generator.iterator().hasNext()) {
        totalTraces++;/*from  w  ww.java 2  s  .  c om*/
        //Print also Route if possible or packet
        Set<StaticPacketTrace> traces = generator.iterator().next();
        if (!verbosity1 && !verbosity2 && !verbosity3) {
            for (StaticPacketTrace trace : traces) {
                previousTrace = printTrace(previousTrace, trace);
                if (!trace.isSuccess()) {
                    print("Failure: %s", trace.resultMessage());
                    failedTraces.add(trace);
                } else {
                    print("Success");
                }
            }
        } else {
            traces.forEach(trace -> {
                print("Tracing packet: %s", trace.getInitialPacket());
                print("%s", T3CliUtils.printTrace(trace, verbosity2, verbosity3));
                print("%s", StringUtils.rightPad("", 125, '-'));
            });
        }
    }

    if (!verbosity1 && !verbosity2 && !verbosity3) {
        if (failedTraces.size() != 0) {
            print("%s", StringUtils.rightPad("", 125, '-'));
            print("Failed Traces: %s", failedTraces.size());
        }
        previousTrace = null;
        for (StaticPacketTrace trace : failedTraces) {
            previousTrace = printTrace(previousTrace, trace);
            print("Failure: %s", trace.resultMessage());
        }
        print("%s", StringUtils.rightPad("", 125, '-'));
        print("Summary");
        print("Total Traces %s, errors %s", totalTraces, failedTraces.size());
        print("%s", StringUtils.rightPad("", 125, '-'));
    }

}

From source file:org.onosproject.t3.cli.TroubleshootMcastCommand.java

private StaticPacketTrace printTrace(StaticPacketTrace previousTrace, StaticPacketTrace trace) {
    if (previousTrace == null || !previousTrace.equals(trace)) {
        print("%s", StringUtils.rightPad("", 125, '-'));
        previousTrace = trace;// w  ww.java 2s  .com
        ConnectPoint initialConnectPoint = trace.getInitialConnectPoint();
        TrafficSelector initialPacket = trace.getInitialPacket();
        boolean isIPv4 = ((EthTypeCriterion) initialPacket.getCriterion(Criterion.Type.ETH_TYPE)).ethType()
                .equals(EthType.EtherType.IPV4.ethType());
        IpPrefix group = ((IPCriterion) (isIPv4 ? trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST)
                : trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST))).ip();
        print("Source %s, group %s", initialConnectPoint, group);
    }
    StringBuilder destinations = new StringBuilder();
    if (trace.getCompletePaths().size() > 1) {
        destinations.append("Sinks: ");
    } else {
        destinations.append("Sink: ");
    }
    trace.getCompletePaths().forEach(path -> {
        destinations.append(path.get(path.size() - 1) + " ");
    });
    print("%s", destinations.toString());
    return previousTrace;
}

From source file:org.onosproject.t3.cli.TroubleshootPingAllCommand.java

@Override
protected void execute() {
    TroubleshootService service = get(TroubleshootService.class);

    EtherType type = EtherType.valueOf(ethType.toUpperCase());

    print("Tracing between all %s hosts", ethType);

    if (!type.equals(EtherType.IPV4) && !type.equals(EtherType.IPV6)) {
        print("Command only support IPv4 or IPv6");
    } else {//  w ww . ja  va 2 s . com
        //Create the generator for the list of traces.
        Generator<Set<StaticPacketTrace>> generator = service.pingAllGenerator(type);
        Host previousHost = null;
        int totalTraces = 0;
        List<StaticPacketTrace> failedTraces = new ArrayList<>();
        boolean ipv4 = type.equals(EtherType.IPV4);
        while (generator.iterator().hasNext()) {
            Set<StaticPacketTrace> traces = generator.iterator().next();
            totalTraces++;
            for (StaticPacketTrace trace : traces) {
                //no verbosity is mininet style output
                if (!verbosity1 && !verbosity2) {
                    if (trace.getEndpointHosts().isPresent()) {
                        Host src = trace.getEndpointHosts().get().getLeft();
                        if (previousHost == null || !previousHost.equals(src)) {
                            print("%s", StringUtils.rightPad("", 125, '-'));
                            previousHost = printSrc(trace, ipv4, src);
                        }
                        String host = getDstString(trace, ipv4, src);
                        if (!trace.isSuccess()) {
                            host = host + " " + trace.resultMessage();
                            failedTraces.add(trace);
                        }
                        print("%s", host);
                    }
                } else {
                    print("%s", StringUtils.leftPad("", 125, '-'));

                    if (trace.getInitialPacket() != null) {
                        if (verbosity1) {
                            printResultOnly(trace, ipv4);
                        } else if (verbosity2) {
                            printVerbose(trace);
                        }
                    } else {
                        if (trace.getEndpointHosts().isPresent()) {
                            Host source = trace.getEndpointHosts().get().getLeft();
                            Host destination = trace.getEndpointHosts().get().getRight();
                            print("Source %s --> Destination %s", source.id(), destination.id());
                        }
                        print("Error in obtaining trace: %s", trace.resultMessage());
                    }
                }
            }
            try {
                sleep(delay);
            } catch (InterruptedException e) {
                log.debug("interrupted while sleep");
            }
        }
        print("%s", StringUtils.rightPad("", 125, '-'));
        print("Failed Traces: %s", failedTraces.size());
        print("%s", StringUtils.rightPad("", 125, '-'));
        failedTraces.forEach(t -> {
            if (t.getEndpointHosts().isPresent()) {
                printSrc(t, ipv4, t.getEndpointHosts().get().getLeft());
                String dst = getDstString(t, ipv4, t.getEndpointHosts().get().getRight());
                dst = dst + " " + t.resultMessage();
                print("%s", dst);
                print("%s", StringUtils.rightPad("", 125, '-'));
            }
        });
        print("Summary");
        print("Total Traces %s, errors %s", totalTraces, failedTraces.size());
    }
}

From source file:org.openhab.binding.dscalarm.internal.protocol.API.java

/**
 * Constructor for Serial Connection//from   w w  w.j av a  2 s.  co  m
 * 
 * @param sPort
 * @param baud
 */
public API(String sPort, int baud, String userCode) {
    if (StringUtils.isNotBlank(sPort)) {
        serialPort = sPort;
    }

    if (isValidBaudRate(baud))
        baudRate = baud;

    if (StringUtils.isNotBlank(userCode)) {
        this.dscAlarmUserCode = userCode;
    }

    //The IT-100 requires 6 digit codes. Shorter codes are right padded with 0.
    this.dscAlarmUserCode = StringUtils.rightPad(dscAlarmUserCode, 6, '0');

    connectorType = DSCAlarmConnectorType.SERIAL;
}

From source file:org.openhab.binding.dscalarm1.internal.protocol.API.java

/**
 * Constructor for Serial Connection/* w ww  . j a va2  s.c om*/
 *
 * @param sPort
 * @param baud
 */
public API(String sPort, int baud, String userCode, DSCAlarmInterfaceType interfaceType) {
    if (StringUtils.isNotBlank(sPort)) {
        serialPort = sPort;
    }

    if (isValidBaudRate(baud)) {
        baudRate = baud;
    }

    if (StringUtils.isNotBlank(userCode)) {
        this.dscAlarmUserCode = userCode;
    }

    // The IT-100 requires 6 digit codes. Shorter codes are right padded with 0.
    this.dscAlarmUserCode = StringUtils.rightPad(dscAlarmUserCode, 6, '0');

    connectorType = DSCAlarmConnectorType.SERIAL;

    if (interfaceType != null) {
        this.interfaceType = interfaceType;
    } else {
        this.interfaceType = DSCAlarmInterfaceType.IT100;
    }
}

From source file:org.openlegacy.terminal.support.TerminalConnectionDelegator.java

private static void handleRightAdjust(TerminalSendAction terminalSendAction) {
    List<TerminalField> fields = terminalSendAction.getFields();
    for (TerminalField terminalField : fields) {
        if (terminalField.getRightAdjust() != RightAdjust.NONE) {
            if (terminalField.getLength() > terminalField.getValue().length()) {
                String fillerChar = terminalField.getRightAdjust() == RightAdjust.ZERO_FILL ? "0" : " ";
                String newValue = null;
                if (terminalField.isRightToLeft()) {
                    newValue = StringUtils.rightPad(terminalField.getValue(), terminalField.getLength(),
                            fillerChar);
                } else {
                    newValue = StringUtils.leftPad(terminalField.getValue(), terminalField.getLength(),
                            fillerChar);
                }//from  www  .ja  va2  s . c  o m
                terminalField.setValue(newValue);
            }
        }
    }
}

From source file:org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggeneratorTest.java

@Test
public void testCreateAndRegisterUniqueAlias() throws IOException {
    Assert.assertEquals("0alias1", jmxConfiggenerator.createAndRegisterUniqueAlias("alias1"));
    Assert.assertEquals("1alias1", jmxConfiggenerator.createAndRegisterUniqueAlias("alias1"));

    String someAlias = StringUtils.rightPad("X", 20, "X") + "YYY";
    String someOtherAlias = StringUtils.rightPad("X", 20, "X") + "XXX";
    Assert.assertEquals("0XXXXXXXXXXXXXXXXXX", jmxConfiggenerator.createAndRegisterUniqueAlias(someAlias));
    Assert.assertEquals("0XXXXXXXXXXXXXXXXXXXXXXX_NAME_CRASH_AS_19_CHAR_VALUE",
            jmxConfiggenerator.createAndRegisterUniqueAlias(someOtherAlias));

}