omitEmptyStrings 无法正常工作

发布于 2024-09-08 12:56:41 字数 2806 浏览 3 评论 0原文

我被困在中间了。

我正在创建包装类 com.google.common.base.Splitter。

但 omitEmptyStrings 方法无法正常工作。

请给我你的精彩指导。

这是[调用者]

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

 }
}

这是结果: [ 富 ]
[11]
[栏1]
[quux]

下面是我的包装类。

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

I'm stuck in the middle.

I'm creating wrapper class for
com.google.common.base.Splitter.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2024-09-15 12:56:41

看起来除了 omitEmptyStrings 之外,您还想要 trimResults,不是吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文