以下是我自己的答案,以寻求灵感:
public final class TextWrapper {
enum Strategy implements WrapStrategy {
HARD {
@Override
public String wrap(final Iterable<String> words, final int width) {
return Joiner.on('\n')
.join(Splitter
.fixedLength(width)
.split(
Joiner.on(' ').join(words)));
}
},
SOFT {
@Override
public String wrap(final Iterable<String> words, final int width) {
final StringBuilder sb = new StringBuilder();
int lineLength = 0;
final Iterator<String> iterator = words.iterator();
if (iterator.hasNext()) {
sb.append(iterator.next());
lineLength=sb.length();
while (iterator.hasNext()) {
final String word = iterator.next();
if(word.length()+1+lineLength>width) {
sb.append('\n');
lineLength=0;
} else {
lineLength++;
sb.append(' ');
}
sb.append(word);
lineLength+=word.length();
}
}
return sb.toString();
}
}
}
interface WrapStrategy {
String wrap(Iterable<String> words, int width);
}
public static TextWrapper forWidth(final int i) {
return new TextWrapper(Strategy.SOFT, CharMatcher.WHITESPACE, i);
}
private final WrapStrategy strategy;
private final CharMatcher delimiter;
private final int width;
TextWrapper(final WrapStrategy strategy,
final CharMatcher delimiter, final int width) {
this.strategy = strategy;
this.delimiter = delimiter;
this.width = width;
}
public TextWrapper hard(){
return new TextWrapper(Strategy.HARD, this.delimiter, this.width);
}
public TextWrapper respectExistingBreaks() {
return new TextWrapper(
this.strategy, CharMatcher.anyOf(" \t"), this.width);
}
public String wrap(final String text) {
return this.strategy.wrap(
Splitter.on(this.delimiter).split(text), this.width);
}
}
示例用法 1:(80 个字符处硬包装)
TextWrapper.forWidth(80)
.hard()
.wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" +
"Maecenas porttitor risus vitae urna hendrerit ac condimentum " +
"odio tincidunt.\nDonec porttitor felis quis nulla aliquet " +
"lobortis. Suspendisse mattis sapien ut metus congue tincidunt. " +
"Quisque gravida, augue sed congue tempor, tortor augue rhoncus " +
"leo, eget luctus nisl risus id erat. Nunc tempor pretium gravida.");
输出:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor risu
s vitae urna hendrerit ac condimentum odio tincidunt. Donec porttitor felis quis
nulla aliquet lobortis. Suspendisse mattis sapien ut metus congue tincidunt. Qu
isque gravida, augue sed congue tempor, tortor augue rhoncus leo, eget luctus ni
sl risus id erat. Nunc tempor pretium gravida.
示例用法 2:(在 60 个字符或之前软换行,保留现有换行符)
TextWrapper.forWidth(60)
.respectExistingBreaks()
.wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" +
"Maecenas porttitor risus vitae urna hendrerit ac condimentum " +
"odio tincidunt.\nDonec porttitor felis quis nulla aliquet " +
"lobortis. Suspendisse mattis sapien ut metus congue tincidunt. " +
"Quisque gravida, augue sed congue tempor, tortor augue rhoncus " +
"leo, eget luctus nisl risus id erat. Nunc tempor pretium gravida.");
输出:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit.
Maecenas porttitor risus vitae urna hendrerit ac
condimentum odio tincidunt.
Donec porttitor felis quis nulla
aliquet lobortis. Suspendisse mattis sapien ut metus congue
tincidunt. Quisque gravida, augue sed congue tempor, tortor
augue rhoncus leo, eget luctus nisl risus id erat. Nunc
tempor pretium gravida.