View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.base.utility;
17  
18  import java.util.Iterator;
19  
20  
21  /**
22   * Split given string into parts delimited by white space.
23   *
24   * @author  Michael Meyling
25   */
26  public final class Splitter implements Iterator {
27  
28      /** Text to split. */
29      private final String text;
30  
31      /** Last found position. */
32      private int found;
33  
34      /** Start searching from here. */
35      private int start;
36  
37      /** Current found separator string. Consists of (perhaps several) delimiters. */
38      private String separator;
39  
40      /** Current found token. */
41      private String token;
42  
43      /**
44       * Constructor.
45       *
46       * @param   text        Text to split.
47       */
48      public Splitter(final String text) {
49          this.text = text;
50          iterate();
51      }
52  
53      /**
54       * Split String by given delimiter.
55       * "a b c" is converted to "a", " ", "b", " ", "c".
56       * "a b c " is converted to "a", " ", "b", " ", "c", " ".
57       * "a  b  c" is converted to "a", "  ", "b", "  ", "c".
58       *
59       * @return  Split text.
60       */
61      public Object next() {
62          return nextToken();
63      }
64  
65      /**
66       * Qualified next method.
67       *
68       * @return  Get next token.
69       */
70      public String nextToken() {
71          if (token != null) {
72              final String result = token;
73              token = null;
74              return result;
75          }
76          if (separator != null) {
77              final String result = separator;
78              separator = null;
79              iterate();
80              return result;
81          }
82          return null;
83      }
84  
85      /**
86       * Iterate token and whitespace.
87       */
88      private void iterate() {
89          found = start;
90          while (found < text.length() && !Character.isWhitespace(text.charAt(found))) {
91              found++;
92          }
93          if (found < text.length()) {
94              token = text.substring(start, found);
95              start = found;
96              while (found < text.length() && Character.isWhitespace(text.charAt(found))) {
97                  found++;
98              }
99              separator = text.substring(start, found);
100             start = found;
101         } else {
102             separator = null;
103             token = text.substring(start);
104         }
105         if (token.length() == 0) {
106             token = null;
107         }
108     }
109 
110     public boolean hasNext() {
111         return token != null  || separator != null;
112     }
113 
114 
115     public void remove() {
116         throw new UnsupportedOperationException("this iterator doesn't support this method");
117     }
118 
119 }