Splitter 分割字符串,修正了java字符串分割的问题。(策略模式)
如",a,,b,".split(",") 会被分为:"", "a", "", "b"
语法:
Splitter.on(',') .trimResults() .omitEmptyStrings() .split("foo,bar,, qux");
会得到"foo", "bar", "qux"。
1.静态的构造方法
public static Splitter on(char separator) {
return on(CharMatcher.is(separator));
}
public static Splitter on(final CharMatcher separatorMatcher) {
Preconditions.checkNotNull(separatorMatcher);
return new Splitter(new Splitter.Strategy() {
//采用策略模式,根据CharMatcher分割;
public Splitter.SplittingIterator iterator(Splitter splitter, CharSequence toSplit) {
return new Splitter.SplittingIterator(splitter, toSplit) {
int separatorStart(int start) {
return separatorMatcher.indexIn(this.toSplit, start);
}
int separatorEnd(int separatorPosition) {
return separatorPosition + 1;
}
};
}
});
}
public static Splitter on(final String separator) {
Preconditions.checkArgument(separator.length() != 0, "The separator may not be the empty string.");
return new Splitter(new Splitter.Strategy() {
//采用策略模式,根据字符串分割;
public Splitter.SplittingIterator iterator(Splitter splitter, CharSequence toSplit) {
return new Splitter.SplittingIterator(splitter, toSplit) {
public int separatorStart(int start) {
int separatorLength = separator.length();
int p = start;
label24:
for(int last = this.toSplit.length() - separatorLength; p <= last; ++p) {
for(int i = 0; i < separatorLength; ++i) {
if (this.toSplit.charAt(i + p) != separator.charAt(i)) {
continue label24;
}
}
return p;
}
return -1;
}
public int separatorEnd(int separatorPosition) {
return separatorPosition + separator.length();
}
};
}
});
}
@GwtIncompatible("java.util.regex")
根据匹配模式分割
public static Splitter on(final Pattern separatorPattern) {
Preconditions.checkNotNull(separatorPattern);
Preconditions.checkArgument(!separatorPattern.matcher("").matches(), "The pattern may not match the empty string: %s", new Object[]{separatorPattern});
return new Splitter(new Splitter.Strategy() {
public Splitter.SplittingIterator iterator(Splitter splitter, CharSequence toSplit) {
final Matcher matcher = separatorPattern.matcher(toSplit);
return new Splitter.SplittingIterator(splitter, toSplit) {
public int separatorStart(int start) {
return matcher.find(start) ? matcher.start() : -1;
}
public int separatorEnd(int separatorPosition) {
return matcher.end();
}
};
}
});
}
以上方法返回Splitter都是用通过内部抽象类
SplittingIterator实现的 便利字符串内数据
private abstract static class SplittingIterator extends AbstractIterator<String> {
final CharSequence toSplit;
final CharMatcher trimmer;
final boolean omitEmptyStrings;
int offset = 0;
int limit;
abstract int separatorStart(int var1);
abstract int separatorEnd(int var1);
protected SplittingIterator(Splitter splitter, CharSequence toSplit) {
this.trimmer = splitter.trimmer;
this.omitEmptyStrings = splitter.omitEmptyStrings;
this.limit = splitter.limit;
this.toSplit = toSplit;
}
protected String computeNext() {
int nextStart = this.offset;
while(true) {
while(this.offset != -1) {
int start = nextStart;
int separatorPosition = this.separatorStart(this.offset);
int end;
if (separatorPosition == -1) {
end = this.toSplit.length();
this.offset = -1;
} else {
end = separatorPosition;
this.offset = this.separatorEnd(separatorPosition);
}
if (this.offset != nextStart) {
while(start < end && this.trimmer.matches(this.toSplit.charAt(start))) {
++start;
}
while(end > start && this.trimmer.matches(this.toSplit.charAt(end - 1))) {
--end;
}
if (!this.omitEmptyStrings || start != end) {
if (this.limit == 1) {
end = this.toSplit.length();
for(this.offset = -1; end > start && this.trimmer.matches(this.toSplit.charAt(end - 1)); --end) {
;
}
} else {
--this.limit;
}
return this.toSplit.subSequence(start, end).toString();
}
nextStart = this.offset;
} else {
++this.offset;
if (this.offset >= this.toSplit.length()) {
this.offset = -1;
}
}
}
return (String)this.endOfData();
}
}
}
2.重要方法
limit
public Splitter limit(int limit) {
Preconditions.checkArgument(limit > 0, "must be greater than zero: %s", new Object[]{limit});
//加入长度限制
return new Splitter(this.strategy, this.omitEmptyStrings, this.trimmer, limit);
}
trimResults()
@CheckReturnValue
public Splitter trimResults() {
return this.trimResults(CharMatcher.WHITESPACE);
}
//把空白字符当作分隔符重新返回Spliite
@CheckReturnValue
public Splitter trimResults(CharMatcher trimmer) {
Preconditions.checkNotNull(trimmer);
return new Splitter(this.strategy, this.omitEmptyStrings, trimmer, this.limit);
}
网友评论