/** as {@link #maxlenWithEllipsis(String, int, String) with "..." as the ellipsis */ public static String maxlenWithEllipsis(String s, int maxlen) { return maxlenWithEllipsis(s, maxlen, "..."); } /** as {@link #maxlenWithEllipsis(String, int) but replacing the last few chars with the given ellipsis */
/** returns up to maxlen characters from the start of s */ public static String maxlen(String s, int maxlen) { return maxlenWithEllipsis(s, maxlen, ""); }
private String shorten(String text) { return Strings.maxlenWithEllipsis(text, A_LINE); }
private String shorten(String text) { return Strings.maxlenWithEllipsis(text, A_LINE); }
@JsonProperty("streamContents") public String getStreamContentsAbbreviated() { // TODO would be nice to just get the first 80 chars but that's a refactoring // which might affect persistence. if stream is very large (100MB+) then we sometimes // get OOME without it, so let's abbreviate if (streamSize.get()>8192) { return "<contents-too-large>"; } return Strings.maxlenWithEllipsis(streamContents.get(), 80); } @Override
/** Returns canonicalized string from the given object, made "unique" by: * <li> putting sets into the toString order * <li> appending a hash code if it's longer than the max (and the max is bigger than 0) */ public static String toUniqueString(Object x, int optionalMax) { if (x instanceof Iterable && !(x instanceof List)) { // unsorted collections should have a canonical order imposed MutableList<String> result = MutableList.of(); for (Object xi: (Iterable<?>)x) { result.add(toUniqueString(xi, optionalMax)); } Collections.sort(result); x = result.toString(); } if (x==null) return "{null}"; String xs = x.toString(); if (xs.length()<=optionalMax || optionalMax<=0) return xs; return maxlenWithEllipsis(xs, optionalMax-8)+"/"+Integer.toHexString(xs.hashCode()); }
@Test public void testMaxlen() { Assert.assertEquals(Strings.maxlen("hello world", 5), "hello"); Assert.assertEquals(Strings.maxlenWithEllipsis("hello world", 9), "hello ..."); Assert.assertEquals(Strings.maxlenWithEllipsis("hello world", 7, "--"), "hello--"); }