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

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

Introduction

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

Prototype

public static String left(final String str, final int len) 

Source Link

Document

Gets the leftmost len characters of a String.

If len characters are not available, or the String is null , the String will be returned without an exception.

Usage

From source file:com.francetelecom.clara.cloud.coremodel.ConfigRole.java

/**
 * @param lastModificationComment//from w ww.  ja va 2s .  c  o m
 *            the lastModificationComment to set
 */
public void setLastModificationComment(String lastModificationComment) {
    this.lastModificationComment = StringUtils.left(lastModificationComment, MAX_COMMENT_SIZE);
}

From source file:com.sangupta.shire.domain.RenderableResource.java

/**
 * Update the extension of this resource - based on the mapping
 * as provided by the converted used.//www  . j av  a  2  s.co  m
 * 
 * @param extensionMappings
 */
public void updateExtension(List<String> extensionMappings) {
    if (extensionMappings == null) {
        return;
    }

    for (String extension : extensionMappings) {
        if (this.path.endsWith(extension)) {
            this.path = StringUtils.left(this.path, this.path.length() - extension.length()) + ".html";
            return;
        }
    }
}

From source file:com.medlog.webservice.vo.PatientVO.java

/**
 * @return the addressPostalcode
 */
public String getAddressPostalcode() {
    return StringUtils.left(StrUtl.toS(addressPostalcode).trim(), 9);
}

From source file:com.francetelecom.clara.cloud.activation.plugin.cf.infrastructure.AbstractCfAdapterIT.java

protected static String defaultNamespace(String email) {
    String name_without_domain = email.substring(0, email.indexOf('@')).replaceAll("\\.", "-")
            .replaceAll("\\+", "-").replaceAll("_", "-");
    // Keep it short, 5 chars max
    name_without_domain = StringUtils.left(name_without_domain, 5);
    return name_without_domain;
}

From source file:alfio.manager.EventNameManager.java

private Optional<String> getCroppedName(String cleanDisplayName) {
    String candidate = Arrays.stream(cleanDisplayName.split(SPACES_AND_PUNCTUATION))
            .map(w -> Pair.of(NUMBER_MATCHER.matcher(w).matches(), w))
            .map(p -> p.getKey() ? p.getValue() : StringUtils.left(p.getValue(), 1))
            .collect(Collectors.joining());
    if (isUnique(candidate)) {
        return Optional.of(candidate);
    }// w ww.  j a  v  a2  s  . co m
    return Optional.empty();
}

From source file:info.magnolia.ui.form.field.transformer.multi.MultiValueSubChildrenNodeTransformer.java

@Override
protected String createChildItemName(Set<String> childNames, Object value, JcrNodeAdapter rootItem) {
    return Path.getValidatedLabel(StringUtils.left(value.toString(), valueItemNameSize));
}

From source file:com.medlog.webservice.vo.PatientVO.java

/**
 * @return the addressStreet
 */
public String getAddressStreet() {
    return StringUtils.left(StrUtl.toS(addressStreet), 145);
}

From source file:com.francetelecom.clara.cloud.commons.InternetDomainNameCleaner.java

/**
 * Helper method for {@link #fixParts(List)}. Validates that one part of
 * a domain name is valid.//from   ww  w. jav  a  2  s. co m
 *
 * @param part The domain name part to be validated
 * @param isFinalPart Is this the final (rightmost) domain part?
 * @return Whether the part is valid
 */
public static String getFixedPart(String part, boolean isFinalPart) {

    // These tests could be collapsed into one big boolean expression, but
    // they have been left as independent tests for clarity.

    if (part.length() > MAX_DOMAIN_PART_LENGTH) {
        part = StringUtils.left(part, MAX_DOMAIN_PART_LENGTH);
    }

    /*
     * GWT claims to support java.lang.Character's char-classification methods,
     * but it actually only works for ASCII. So for now, assume any non-ASCII
     * characters are valid. The only place this seems to be documented is here:
     * http://osdir.com/ml/GoogleWebToolkitContributors/2010-03/msg00178.html
     *
     * <p>ASCII characters in the part are expected to be valid per RFC 1035,
     * with underscore also being allowed due to widespread practice.
     */

    String asciiChars = CharMatcher.ASCII.retainFrom(part);

    if (!part.isEmpty() && !PART_CHAR_MATCHER.matchesAllOf(asciiChars)) {
        part = PART_CHAR_MATCHER.retainFrom(asciiChars);
    }

    // No initial or final dashes or underscores.

    if (!part.isEmpty() && DASH_MATCHER.matches(part.charAt(0))) {
        part = part.substring(1);
    } else if (!part.isEmpty() && DASH_MATCHER.matches(part.charAt(part.length() - 1))) {
        part = part.substring(0, part.length() - 1);
    }

    /*
     * Note that we allow (in contravention of a strict interpretation of the
     * relevant RFCs) domain parts other than the last may begin with a digit
     * (for example, "3com.com"). It's important to disallow an initial digit in
     * the last part; it's the only thing that stops an IPv4 numeric address
     * like 127.0.0.1 from looking like a valid domain name.
     */

    if (!part.isEmpty() && isFinalPart && CharMatcher.DIGIT.matches(part.charAt(0))) {
        part = part.substring(1);
    }

    return part;
}

From source file:com.jeans.iservlet.service.rest.impl.SoftwareRestServiceImpl.java

@Override
@Transactional/*from   w w  w. j  ava  2s .c  om*/
public SoftwareResource create(SoftwareResource resource) {
    if (resource.getId() < 0) {
        resource.setId(0);
        return resource;
    } else {
        Software sw = new Software();
        // 1. company?
        Company company = compDao.getById(Company.class, resource.getCompanyId());
        if (null == company) {
            return null;
        }
        sw.setCompany(company);
        sw.setType(AssetConstants.SOFTWARE_ASSET);
        // 2. catalog?
        byte catalog = resource.getCatalog();
        if ((catalog < AssetConstants.OPERATING_SYSTEM_SOFTWARE
                || catalog > AssetConstants.APPLICATION_SOFTWARE) && catalog != AssetConstants.OTHER_SOFTWARE) {
            return null;
        }
        sw.setCatalog(catalog);
        // 3. name?
        if (StringUtils.isBlank(resource.getName())) {
            return null;
        }
        sw.setName(StringUtils.left(StringUtils.trimToEmpty(resource.getName()), 32));
        sw.setVendor(StringUtils.left(StringUtils.trimToEmpty(resource.getVendor()), 32));
        sw.setModelOrVersion(StringUtils.left(StringUtils.trimToEmpty(resource.getModelOrVersion()), 64));
        sw.setAssetUsage(StringUtils.left(StringUtils.trimToEmpty(resource.getAssetUsage()), 255));
        sw.setPurchaseTime(resource.getPurchaseTime());
        // 4. quantity?0
        if (resource.getQuantity() <= 0) {
            return null;
        }
        sw.setQuantity(resource.getQuantity());
        // 5. cost?
        if (resource.getCost().doubleValue() < 0) {
            return null;
        }
        sw.setCost(resource.getCost());
        // 6. state?IN_USEIDLE
        byte state = resource.getState();
        if (state != AssetConstants.IN_USE && state != AssetConstants.IDLE) {
            return null;
        }
        sw.setState(state);
        sw.setComment(StringUtils.left(StringUtils.trimToEmpty(resource.getComment()), 255));
        // 7. softwareType?
        byte softwareType = resource.getSoftwareType();
        if (softwareType < AssetConstants.COMMERCIAL_SOFTWARE
                || softwareType > AssetConstants.OTHER_TYPE_SOFTWARE) {
            return null;
        }
        sw.setSoftwareType(softwareType);
        sw.setLicense(StringUtils.left(StringUtils.trimToEmpty(resource.getLicense()), 64));
        sw.setExpiredTime(resource.getExpiredTime());
        swDao.save(sw);

        return ResourceFactory.createResource(Software.class, SoftwareResource.class, sw);
    }
}

From source file:com.norconex.commons.lang.unit.DataUnitFormatter.java

/**
 * Formats a data amount of the given unit to a human-readable 
 * representation.//w ww. ja  va 2  s  . co m
 * @param amount the amount to format
 * @param unit the data unit type of the amount
 * @return formatted string
 */
public String format(long amount, DataUnit unit) {

    // If no unit specified, return as string without a suffix
    if (unit == null) {
        return Long.toString(amount);
    }

    // Use coarser unit if applicable to make value more human-readable
    DataUnit finalUnit = unit;
    long finalAmount = amount;
    int ordinalShift = 0;
    if (!fixedUnit) {
        ordinalShift = (int) (Math.log(amount) / Math.log(K));
        if (ordinalShift > 0) {
            finalUnit = DATA_UNITS[Math.min(unit.ordinal() + ordinalShift, DATA_UNITS.length - 1)];
            finalAmount = finalUnit.convert(amount, unit);
        }
    }

    // Find out decimals
    long decimals = 0;
    if (decimalPrecision > 0 && unit.ordinal() < finalUnit.ordinal()) {
        int previousOrdinal = finalUnit.ordinal() - 1;
        if (previousOrdinal >= 0) {
            long originalBytes = unit.toBytes(amount);
            long finalBytes = finalUnit.toBytes(finalAmount);
            long diff = originalBytes - finalBytes;
            DataUnit previousUnit = DATA_UNITS[previousOrdinal];
            long remainder = previousUnit.convert(diff, DataUnit.B);
            long base = remainder * (long) Math.pow(D, decimalPrecision);
            decimals = base / K;
        }
    }

    Locale finalLocale = locale;
    if (finalLocale == null) {
        finalLocale = Locale.getDefault();
    }
    StringBuilder b = new StringBuilder();
    b.append(NumberFormat.getIntegerInstance(finalLocale).format(finalAmount));
    if (decimals > 0) {
        b.append(DecimalFormatSymbols.getInstance(finalLocale).getDecimalSeparator());
        b.append(StringUtils.left(Long.toString(decimals), decimalPrecision));
    }
    b.append('\u00A0').append(finalUnit.toString());
    return b.toString();
}