Guava——Splitter

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);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市躺率,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌万矾,老刑警劉巖肥照,帶你破解...
    沈念sama閱讀 212,454評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異勤众,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鲤脏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門们颜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人猎醇,你說我怎么就攤上這事窥突。” “怎么了硫嘶?”我有些...
    開封第一講書人閱讀 157,921評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵阻问,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我沦疾,道長(zhǎng)称近,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,648評(píng)論 1 284
  • 正文 為了忘掉前任哮塞,我火速辦了婚禮刨秆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘忆畅。我一直安慰自己衡未,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評(píng)論 6 386
  • 文/花漫 我一把揭開白布家凯。 她就那樣靜靜地躺著缓醋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪绊诲。 梳的紋絲不亂的頭發(fā)上送粱,一...
    開封第一講書人閱讀 49,950評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音驯镊,去河邊找鬼葫督。 笑死竭鞍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的橄镜。 我是一名探鬼主播偎快,決...
    沈念sama閱讀 39,090評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼洽胶!你這毒婦竟也來了晒夹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,817評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤姊氓,失蹤者是張志新(化名)和其女友劉穎丐怯,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翔横,經(jīng)...
    沈念sama閱讀 44,275評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡读跷,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了禾唁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片效览。...
    茶點(diǎn)故事閱讀 38,724評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖荡短,靈堂內(nèi)的尸體忽然破棺而出丐枉,到底是詐尸還是另有隱情,我是刑警寧澤掘托,帶...
    沈念sama閱讀 34,409評(píng)論 4 333
  • 正文 年R本政府宣布瘦锹,位于F島的核電站,受9級(jí)特大地震影響闪盔,放射性物質(zhì)發(fā)生泄漏弯院。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評(píng)論 3 316
  • 文/蒙蒙 一泪掀、第九天 我趴在偏房一處隱蔽的房頂上張望抽兆。 院中可真熱鬧,春花似錦族淮、人聲如沸辫红。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贴妻。三九已至,卻和暖如春蝙斜,著一層夾襖步出監(jiān)牢的瞬間名惩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工孕荠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留娩鹉,地道東北人攻谁。 一個(gè)月前我還...
    沈念sama閱讀 46,503評(píng)論 2 361
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像弯予,于是被迫代替她去往敵國(guó)和親戚宦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容