Splitter 分割字符串单匣,修正了java字符串分割的問題幔摸。(策略模式)
如",a,,b,".split(",") 會(huì)被分為:"", "a", "", "b"
語法:
Splitter.on(',')? ? .trimResults()? ? .omitEmptyStrings()? ? .split("foo,bar,, qux");
會(huì)得到"foo", "bar", "qux"。
1.靜態(tài)的構(gòu)造方法
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() {
//采用策略模式,根據(jù)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() {
//采用策略模式,根據(jù)字符串分割;
????????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")
根據(jù)匹配模式分割
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都是用通過內(nèi)部抽象類
SplittingIterator實(shí)現(xiàn)的 便利字符串內(nèi)數(shù)據(jù)
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});
//加入長(zhǎng)度限制
????return?new?Splitter(this.strategy,?this.omitEmptyStrings,?this.trimmer, limit);
}
trimResults()
@CheckReturnValue
public?Splitter trimResults() {
????return?this.trimResults(CharMatcher.WHITESPACE);
}
//把空白字符當(dāng)作分隔符重新返回Spliite
@CheckReturnValue
public?Splitter trimResults(CharMatcher trimmer) {
????Preconditions.checkNotNull(trimmer);
????return?new?Splitter(this.strategy,?this.omitEmptyStrings, trimmer,?this.limit);
}