return truncate(str, 0, maxWidth);
@Test public void testTruncate_StringInt() { assertNull(StringUtils.truncate(null, 12)); try { StringUtils.truncate(null, -1); fail("maxWith cannot be negative"); } catch (final Exception e) { StringUtils.truncate(null, -10); fail("maxWith cannot be negative"); } catch (final Exception e) { StringUtils.truncate(null, Integer.MIN_VALUE); fail("maxWith cannot be negative"); } catch (final Exception e) { assertTrue(e instanceof IllegalArgumentException); assertEquals("", StringUtils.truncate("", 10)); assertEquals("", StringUtils.truncate("", 10)); assertEquals("abc", StringUtils.truncate("abcdefghij", 3)); assertEquals("abcdef", StringUtils.truncate("abcdefghij", 6)); assertEquals("", StringUtils.truncate("abcdefghij", 0)); try { StringUtils.truncate("abcdefghij", -1); fail("maxWith cannot be negative"); } catch (final Exception e) { StringUtils.truncate("abcdefghij", -100); fail("maxWith cannot be negative"); } catch (final Exception e) {
@Test public void testTruncate_StringIntInt() { assertNull(StringUtils.truncate(null, 0, 12)); try { StringUtils.truncate(null, -1, 0); fail("maxWith cannot be negative"); } catch (final Exception e) { StringUtils.truncate(null, -10, -4); fail("maxWith cannot be negative"); } catch (final Exception e) { StringUtils.truncate(null, Integer.MIN_VALUE, Integer.MIN_VALUE); fail("maxWith cannot be negative"); } catch (final Exception e) { assertTrue(e instanceof IllegalArgumentException); assertNull(StringUtils.truncate(null, 10, 12)); assertEquals("", StringUtils.truncate("", 0, 10)); assertEquals("", StringUtils.truncate("", 2, 10)); assertEquals("abc", StringUtils.truncate("abcdefghij", 0, 3)); assertEquals("fghij", StringUtils.truncate("abcdefghij", 5, 6)); assertEquals("", StringUtils.truncate("abcdefghij", 0, 0)); try { StringUtils.truncate("abcdefghij", 0, -1); fail("maxWith cannot be negative"); } catch (final Exception e) { StringUtils.truncate("abcdefghij", 0, -10); fail("maxWith cannot be negative");
public ProgressPrinter(int threads, String header) { this.threads = threads; if (header.length() > MAX_HEADER_LENGTH * 2) { this.header = StringUtils.truncate(header, MAX_HEADER_LENGTH) + "..." + StringUtils.truncate(header, header.length() - MAX_HEADER_LENGTH, Integer.MAX_VALUE) + " "; } else { this.header = header; } }
return truncate(str, 0, maxWidth);
return truncate(str, 0, maxWidth);
return truncate(str, 0, maxWidth);
private String truncateStringCleverly(String json) { return json.length() < LOG_JSON_MAX_LENGTH ? json : StringUtils.truncate(json, LOG_JSON_MAX_LENGTH / 2) + "..." + json.substring(json.length() - LOG_JSON_MAX_LENGTH / 2); } }
private String getBy(WebElement element) { String str = element.toString(); return getBy(truncate(str, str.length() - 1), "->"); }
public static String makeSafeName(String name) { String nowhitespace = WHITESPACE.matcher(name).replaceAll("-"); String normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD); String slug = sanitise(normalized); slug = slug.replaceAll("^[_]*", ""); slug = slug.replaceAll("[_]*$", ""); slug = StringUtils.truncate(slug, 200); return slug.toLowerCase(Locale.ENGLISH); }
public static String generate(String url, String prefix, String id, String extension) { String pathPart = Urls.urlToPathParts(URI.create(url)); pathPart = pathPart.equals("") ? "(root)" : sanitise(pathPart); if (pathPart.length() > 150) { pathPart = StringUtils.truncate(pathPart, 150); } return new StringBuilder(prefix) .append("-") .append(pathPart) .append("-") .append(id) .append(".") .append(extension) .toString(); }
public <T> T toObject(String json, Class<T> clazz) { if (json == null) { throw new SerializerException("Cannot deserialize null string to " + clazz.getCanonicalName()); } try { return objectMapper.readValue(json, clazz); } catch (IOException e) { String truncatedJson = truncateStringCleverly(json); String truncatedMessage = StringUtils.truncate(e.getMessage(), LOG_JSON_MAX_LENGTH); throw new SerializerException("Cannot deserialize string: '" + truncatedJson + "', to: " + clazz.getSimpleName() + " reason: " + truncatedMessage); } }
public RoleSet copyOf() { RoleSet roleSet = new RoleSet(); String copiedName = StringUtils.truncate("Copy of " + name, 150); roleSet.setName(copiedName); roleSet.setDescription(description); roleSet.setScope(scope); if(CollectionUtils.isNotEmpty(roles)) { List<Role> copyRoles = Lists.newArrayList(); for (Role role : roles) { Role copyRole = new Role(role.getName()); copyRole.setDefaultRole(role.getDefaultRole()); if(CollectionUtils.isNotEmpty(role.getPermissions())) { List<String> permNames = role.getPermissions().stream() .map(permission -> permission.getName()) .collect(Collectors.toList()); copyRole.setPermissionNames(permNames); } copyRoles.add(copyRole); } roleSet.setRoles(copyRoles); } return roleSet; }
else { body = in.readCharSequence(bodyLength, StandardCharsets.UTF_8).toString(); log.debug(" [x] {} = YES (Length: {}, Body: {})", desc, bodyLength, StringUtils.replaceAll(StringUtils.truncate(body, 30), "\n", "\\\\n"));
private String truncate(String value) { if (value == null || value.length() <= maxLength) { return value; } if (isAppendHash()) { return StringUtil.truncateWithHash( value, getMaxLength(), getSuffix()); } if (StringUtils.isNotEmpty(getSuffix())) { return StringUtils.abbreviate(value, getSuffix(), getMaxLength()); } return StringUtils.truncate(value, getMaxLength()); }
for (String newTag : newTags) { if (!StringUtils.isBlank(newTag)) { String tag = StringUtils.truncate(Utils.noSpaces(Utils.stripAndTrim(newTag, " "), "-"), 35); newTagsSet.add(tag); if (!oldTagsSet.contains(tag)) {
private void createTags() { if (getTags() == null || getTags().isEmpty()) { return; } ArrayList<Tag> tagz = new ArrayList<Tag>(); Pager tagged = new Pager(0); for (int i = 0; i < getTags().size(); i++) { String ntag = getTags().get(i); Tag t = new Tag(StringUtils.truncate(Utils.noSpaces(Utils.stripAndTrim(ntag, " "), "-"), 35)); if (!StringUtils.isBlank(t.getTag())) { tagged.setCount(0); client().findTagged(getType(), new String[]{t.getTag()}, tagged); t.setCount((int) tagged.getCount() + 1); getTags().set(i, t.getTag()); tagz.add(t); } } client().createAll(tagz); }