Moving abbreviate + abbreviateMiddle to CharSequences

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1082051 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2011-03-16 05:04:45 +00:00
parent 5bdb7232cf
commit 0bc3750606
1 changed files with 31 additions and 28 deletions

View File

@ -5641,7 +5641,7 @@ public class StringUtils {
* <ul> * <ul>
* <li>If {@code str} is less than {@code maxWidth} characters * <li>If {@code str} is less than {@code maxWidth} characters
* long, return it.</li> * long, return it.</li>
* <li>Else abbreviate it to {@code (substring(str, 0, max-3) + "...")}.</li> * <li>Else abbreviate it to {@code (substring(seq, 0, max-3) + "...")}.</li>
* <li>If {@code maxWidth} is less than {@code 4}, throw an * <li>If {@code maxWidth} is less than {@code 4}, throw an
* {@code IllegalArgumentException}.</li> * {@code IllegalArgumentException}.</li>
* <li>In no case will it return a String of length greater than * <li>In no case will it return a String of length greater than
@ -5659,14 +5659,14 @@ public class StringUtils {
* StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException * StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException
* </pre> * </pre>
* *
* @param str the String to check, may be null * @param seq the CharSequence to check, may be null
* @param maxWidth maximum length of result String, must be at least 4 * @param maxWidth maximum length of result String, must be at least 4
* @return abbreviated String, {@code null} if null String input * @return abbreviated String, {@code null} if null String input
* @throws IllegalArgumentException if the width is too small * @throws IllegalArgumentException if the width is too small
* @since 2.0 * @since 2.0
*/ */
public static String abbreviate(String str, int maxWidth) { public static String abbreviate(CharSequence seq, int maxWidth) {
return abbreviate(str, 0, maxWidth); return abbreviate(seq, 0, maxWidth);
} }
/** /**
@ -5697,40 +5697,40 @@ public class StringUtils {
* StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException * StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException
* </pre> * </pre>
* *
* @param str the String to check, may be null * @param seq the CharSequence to check, may be null
* @param offset left edge of source String * @param offset left edge of source String
* @param maxWidth maximum length of result String, must be at least 4 * @param maxWidth maximum length of result String, must be at least 4
* @return abbreviated String, {@code null} if null String input * @return abbreviated String, {@code null} if null String input
* @throws IllegalArgumentException if the width is too small * @throws IllegalArgumentException if the width is too small
* @since 2.0 * @since 2.0
*/ */
public static String abbreviate(String str, int offset, int maxWidth) { public static String abbreviate(CharSequence seq, int offset, int maxWidth) {
if (str == null) { if (seq == null) {
return null; return null;
} }
if (maxWidth < 4) { if (maxWidth < 4) {
throw new IllegalArgumentException("Minimum abbreviation width is 4"); throw new IllegalArgumentException("Minimum abbreviation width is 4");
} }
if (str.length() <= maxWidth) { if (seq.length() <= maxWidth) {
return str; return seq.toString();
} }
if (offset > str.length()) { if (offset > seq.length()) {
offset = str.length(); offset = seq.length();
} }
if ((str.length() - offset) < (maxWidth - 3)) { if ((seq.length() - offset) < (maxWidth - 3)) {
offset = str.length() - (maxWidth - 3); offset = seq.length() - (maxWidth - 3);
} }
final String abrevMarker = "..."; final String abrevMarker = "...";
if (offset <= 4) { if (offset <= 4) {
return str.substring(0, maxWidth - 3) + abrevMarker; return seq.subSequence(0, maxWidth - 3) + abrevMarker;
} }
if (maxWidth < 7) { if (maxWidth < 7) {
throw new IllegalArgumentException("Minimum abbreviation width with offset is 7"); throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
} }
if ((offset + (maxWidth - 3)) < str.length()) { if ((offset + (maxWidth - 3)) < seq.length()) {
return abrevMarker + abbreviate(str.substring(offset), maxWidth - 3); return abrevMarker + abbreviate(StringUtils.subSequence(seq, offset), maxWidth - 3);
} }
return abrevMarker + str.substring(str.length() - (maxWidth - 3)); return abrevMarker + StringUtils.subSequence(seq, seq.length() - (maxWidth - 3));
} }
/** /**
@ -5756,29 +5756,32 @@ public class StringUtils {
* StringUtils.abbreviateMiddle("abcdef", ".", 4) = "ab.f" * StringUtils.abbreviateMiddle("abcdef", ".", 4) = "ab.f"
* </pre> * </pre>
* *
* @param str the String to abbreviate, may be null * @param seq the CharSequence to abbreviate, may be null
* @param middle the String to replace the middle characters with, may be null * @param middle the CharSequence to replace the middle characters with, may be null
* @param length the length to abbreviate {@code str} to. * @param length the length to abbreviate {@code seq} to.
* @return the abbreviated String if the above criteria is met, or the original String supplied for abbreviation. * @return the abbreviated String if the above criteria is met, or the original String supplied for abbreviation.
* @since 2.5 * @since 2.5
*/ */
public static String abbreviateMiddle(String str, String middle, int length) { public static String abbreviateMiddle(CharSequence seq, CharSequence middle, int length) {
if (isEmpty(str) || isEmpty(middle)) { if (seq == null) {
return str; return null;
}
if (isEmpty(seq) || isEmpty(middle)) {
return seq.toString();
} }
if (length >= str.length() || length < (middle.length()+2)) { if (length >= seq.length() || length < (middle.length()+2)) {
return str; return seq.toString();
} }
int targetSting = length-middle.length(); int targetSting = length-middle.length();
int startOffset = targetSting/2+targetSting%2; int startOffset = targetSting/2+targetSting%2;
int endOffset = str.length()-targetSting/2; int endOffset = seq.length()-targetSting/2;
StringBuilder builder = new StringBuilder(length); StringBuilder builder = new StringBuilder(length);
builder.append(str.substring(0,startOffset)); builder.append(seq.subSequence(0,startOffset));
builder.append(middle); builder.append(middle);
builder.append(str.substring(endOffset)); builder.append(StringUtils.subSequence(seq, endOffset));
return builder.toString(); return builder.toString();
} }