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

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

Introduction

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

Prototype

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

Source Link

Document

Left pad a String with a specified String.

Pad to a size of size .

 StringUtils.leftPad(null, *, *)      = null StringUtils.leftPad("", 3, "z")      = "zzz" StringUtils.leftPad("bat", 3, "yz")  = "bat" StringUtils.leftPad("bat", 5, "yz")  = "yzbat" StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat" StringUtils.leftPad("bat", 1, "yz")  = "bat" StringUtils.leftPad("bat", -1, "yz") = "bat" StringUtils.leftPad("bat", 5, null)  = "  bat" StringUtils.leftPad("bat", 5, "")    = "  bat" 

Usage

From source file:org.openmrs.module.radiologyapp.RadiologyServiceImpl.java

@Override
public void ensureAccessionNumberAssignedToOrder(Order order) {
    if (order.getAccessionNumber() == null) {
        String accessionNumber = new LuhnMod10IdentifierValidator()
                .getValidIdentifier(order.getOrderId().toString());
        accessionNumber = StringUtils.leftPad(accessionNumber, 10, "0"); // pad the accession number to 10 digits
        order.setAccessionNumber(accessionNumber);
    }/*ww  w. j a v  a  2  s .  com*/
}

From source file:org.opensha.commons.geo.RegionUtils.java

private static String toHex(int i) {
    return StringUtils.leftPad(Integer.toHexString(i), 2, '0');
}

From source file:org.openwms.common.comm.api.CommonHeader.java

/**
 * {@inheritDoc}/*from  w ww .j  a va 2 s . c o m*/
 */
@Override
public String toString() {
    return new StringBuilder().append(this.sync)
            .append(StringUtils.leftPad("" + this.messageLength, LENGTH_MESSAGE_LENGTH_FIELD, "0"))
            .append(this.sender).append(this.receiver)
            .append(StringUtils.leftPad("" + this.sequenceNo, LENGTH_SEQUENCE_NO_FIELD, "0")).toString();
}

From source file:org.openwms.common.domain.values.Barcode.java

/**
 * Force the Barcode to be aligned to the determined rules regarding padding, alignment.
 * /*from  w  w  w  .  java 2s  .c o m*/
 * @param val
 *            The old Barcode as String
 * @return The new aligned Barcode
 */
public final String adjustBarcode(String val) {
    if (val == null) {
        throw new IllegalArgumentException("Cannot create a barcode without value");
    }
    if (isPadded()) {
        this.value = (alignment == BARCODE_ALIGN.RIGHT) ? StringUtils.leftPad(val, length, padder)
                : StringUtils.rightPad(val, length, padder);
    } else {
        this.value = val;
    }
    return this.value;
}

From source file:org.orcid.core.manager.impl.OrcidGenerationManagerImpl.java

private String getNextOrcid() {
    String baseOrcid = StringUtils.leftPad(String.valueOf(getRandomNumber()), 15, '0');
    String checkDigit = OrcidCheckDigitGenerator.generateCheckDigit(baseOrcid);
    return formatOrcid(baseOrcid + checkDigit);
}

From source file:org.orcid.frontend.web.util.NumberList.java

private static List<String> createList(List<String> listToPrepend, int minimum, int maximum) {
    List<String> numberList = new ArrayList<String>();
    if (listToPrepend != null) {
        numberList.addAll(listToPrepend);
    }/* www. j  a v  a 2 s  .c  o m*/
    for (int i = minimum; i <= maximum; i++) {
        numberList.add(StringUtils.leftPad(String.valueOf(i), 2, '0'));
    }
    return numberList;
}

From source file:org.pepstock.catalog.gdg.GDGUtil.java

/**
 * Returns the generation index, passing the relative index and last version
 * (gen 0).//  w w w  . jav  a  2s. c  o  m
 * 
 * @param lastVersion generation 0 of GDGD
 * @param offset relative position
 * @return generation index string representation (format #####)
 */
private static String createGenerationIndex(int lastVersion, int offset) {
    // gets the index adding last version and offset (could be negative)
    int index = lastVersion + offset;

    // if index is less than zero, out ot bounds
    if (index < 0) {
        throw new IndexOutOfBoundsException(GDGMessage.JEMD005E.toMessage().getFormattedMessage(offset));
    }
    // formats the result, padding left with 0
    return StringUtils.leftPad(String.valueOf(index), 5, "0");
}

From source file:org.pepstock.jem.commands.util.Factory.java

/**
 * Creates the unique id for the job, normalizing generated id joined to started time stamp of job.<br>
 * it pads (on left) with '0' string representation of both id and timestamp
 * /* w  w  w  . jav  a2s.  c  om*/
 * @param job job instance, needs to extract started timestamp 
 * @param id job id
 * @return job id in job format
 */
public static String createJobId(Job job, long id) {
    StringBuilder sb = new StringBuilder();
    sb.append(StringUtils.leftPad(String.valueOf(id), LONG_LENGTH, "0")).append('-')
            .append(StringUtils.leftPad(String.valueOf(job.getSubmittedTime().getTime()), LONG_LENGTH, "0"));
    return sb.toString();
}

From source file:org.pesc.sdk.message.transcriptrequest.v1_4.TranscriptRequestValidator.java

/**
 * Create strict SourceDestinationType from relaxed SourceDestinationType.  Strict means that it will pass xsd validation.  Relaxed means that it doesn't need to pass xsd validation.
 * If relaxedOrganizationName is longer than 60 it will be abbreviated to 57 characters + '...'.  If relaxedOrganizationName is null or empty string strictOrganizationName will be null.
 * If relaxedCeeb is less than 6 characters it will be left padded.  If relaxedCeeb is more then 6 characters strictCeeb will be null.
 * @param relaxedSourceDestination//from  w w w  . j  av a2 s .  c o  m
 * @return
 */
public static SourceDestinationType relaxedToStrictSourceDestination(
        SourceDestinationType relaxedSourceDestination) {
    SourceDestinationType strictSourceDestination = academicRecordObjectFactory.createSourceDestinationType();
    String organizationName = null;//1-60 characters
    String ceeb = null;//6 characters

    if (relaxedSourceDestination != null) {
        OrganizationType relaxedOrganization = relaxedSourceDestination.getOrganization();
        if (relaxedOrganization != null) {
            List<String> relaxedOrganizationNames = relaxedOrganization.getOrganizationNames();
            if (relaxedOrganizationNames.size() > 0) {
                String name = relaxedOrganizationNames.get(0);
                if (name != null && name.length() > 1) {
                    organizationName = StringUtils.abbreviate(name, 60);//limit to 60 characters
                }
            }
            String relaxedCeeb = relaxedOrganization.getCEEBACT();
            if (relaxedCeeb != null && relaxedCeeb.length() < 7) {
                ceeb = StringUtils.leftPad(relaxedCeeb, 6, '0');
            }
        }
    }
    OrganizationType organization = academicRecordObjectFactory.createOrganizationType();
    organization.getOrganizationNames().add(organizationName);
    organization.setCEEBACT(ceeb);
    strictSourceDestination.setOrganization(organization);
    return strictSourceDestination;
}

From source file:org.protempa.backend.dsb.relationaldb.SQLGenLocalUniqueId.java

SQLGenLocalUniqueId(String entitySpecName, String[] dbIds, int[] maxWidths) {
    assert entitySpecName != null : "entitySpecName cannot be null";
    assert dbIds != null : "dbIds cannot be null";
    assert !ArrayUtils.contains(dbIds, null) : "dbIds cannot contain a null element";

    //The entity spec name is already interned, so don't bother redoing it.
    this.entitySpecName = entitySpecName;

    this.dbIds = dbIds.clone();
    if (this.dbIds.length == 1) {
        try {//  w  ww .java 2s.co m
            this.numericalId = Integer.parseInt(this.dbIds[0]);
        } catch (NumberFormatException ex) {
            synchronized (NEXT_NUMERICAL_ID_LOCK) {
                this.numericalId = NEXT_NUMERICAL_ID++;
            }
        }
    } else {
        if (maxWidths == null) {
            synchronized (NEXT_NUMERICAL_ID_LOCK) {
                this.numericalId = NEXT_NUMERICAL_ID++;
            }
        } else {
            assert dbIds.length == maxWidths.length : "maxWidths, if not null, must have the same number of values as dbIds";
            StringBuilder db = new StringBuilder();
            try {
                for (int i = 0; i < this.dbIds.length; i++) {
                    if (i == 0) {
                        db.append(this.dbIds[i]);
                    } else {
                        db.append(StringUtils.leftPad(this.dbIds[i], maxWidths[i], '0'));
                    }
                }
                this.numericalId = Integer.parseInt(db.toString());
            } catch (NumberFormatException ex) {
                synchronized (NEXT_NUMERICAL_ID_LOCK) {
                    this.numericalId = NEXT_NUMERICAL_ID++;
                }
            }
        }
    }
}