开发者

omitEmptyStrings cannot be worked properly

开发者 https://www.devze.com 2023-01-05 09:45 出处:网络
I\'m stuck in the middle. I\'m creating wrapper class for com.google.common.base.Splitter开发者_JAVA技巧.

I'm stuck in the middle.

I'm creating wrapper class for com.google.common.base.Splitter开发者_JAVA技巧.

But the method omitEmptyStrings doesn't work properly.

Please, give your awesome guidances to me.

Here is [the caller]

public class Hoge {
 public static void main(String[] args) {
     Iterable<String> hoge =  
         Splitter.on(",")  
               .omitEmptyStrings()  
               .split(" foo ,,11,bar1 , quux");

 }
}

This is the result: [ foo ]

[11]

[bar1 ]

[ quux]

Below is my Wrapper class.

public final class Splitter extends Object {

 private static final Splitter splitter = new Splitter();

 private static com.google.common.base.Splitter guavaSplitter;

 private static Object separatorChars = null;

 private static boolean omitEmptyStringsFlg = false;

 private static CharMatcher trimmer = null;

 /**
  * Default Construction
  * This constructor is private to deny access
  * 
  */
 private Splitter() {
 }
  /**
   * setting guava Splitter to this class guavaSplitter
   * 
   * @param separatorMatcher
   * @return Splitter
   */
  public static Splitter on(final String separator) {

 if (StringUtils.isEmpty(separator)) {
  Splitter.guavaSplitter = null;
 } else {
  Splitter.guavaSplitter
      = com.google.common.base.Splitter.on(separator);

  Splitter.separatorChars = separator;
 } 

 return Splitter.splitter;

  }

 /**
  * setting guava Splitter to this class guavaSplitter
  * 
  * @return Splitter
  */

 public Splitter omitEmptyStrings() {

  if (null != Splitter.guavaSplitter) {

   if (Splitter.separatorChars instanceof String) {
    if (null != Splitter.trimmer) {
     Splitter.guavaSplitter
         = com.google.common.base.Splitter.on(
       (String)Splitter.separatorChars)
       .omitEmptyStrings()
       .trimResults(Splitter.trimmer);

    } else {
     Splitter.guavaSplitter
         = com.google.common.base.Splitter.on(
       (String)Splitter.separatorChars).omitEmptyStrings();

    }

   }

  }
  Splitter.omitEmptyStringsFlg = true;
     return Splitter.splitter;
 }

  /**
   * Splits the {@link CharSequence} passed in parameter.
   *
   * @param sequence the sequence of characters to split
   * @return an iteration over the segments split from the parameter.
   */
  public Iterable<String> split(final CharSequence sequence) {
 Iterable<String> retList = new List();

    if (null != sequence && null != Splitter.guavaSplitter) {
     retList = Splitter.guavaSplitter.split(sequence);

    }

    return retList;
  }

}


/**
 * 
 * Returns an iterator over a set of elements of type String.
 * 
 */
class List implements Iterable<String> {

 private java.util.List<String> splittedList
  = new java.util.ArrayList<String>();

 @Override
 public Iterator<String> iterator() {
  return this.splittedList.iterator();
 }
} 


It seems like you wanted trimResults in addition to omitEmptyStrings, didn't you?

0

精彩评论

暂无评论...
验证码 换一张
取 消