Splitter.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.base.utility;
017 
018 import java.util.Iterator;
019 
020 
021 /**
022  * Split given string into parts delimited by white space.
023  *
024  @author  Michael Meyling
025  */
026 public final class Splitter implements Iterator {
027 
028     /** Text to split. */
029     private final String text;
030 
031     /** Last found position. */
032     private int found;
033 
034     /** Start searching from here. */
035     private int start;
036 
037     /** Current found separator string. Consists of (perhaps several) delimiters. */
038     private String separator;
039 
040     /** Current found token. */
041     private String token;
042 
043     /**
044      * Constructor.
045      *
046      @param   text        Text to split.
047      */
048     public Splitter(final String text) {
049         this.text = text;
050         iterate();
051     }
052 
053     /**
054      * Split String by given delimiter.
055      * "a b c" is converted to "a", " ", "b", " ", "c".
056      * "a b c " is converted to "a", " ", "b", " ", "c", " ".
057      * "a  b  c" is converted to "a", "  ", "b", "  ", "c".
058      *
059      @return  Split text.
060      */
061     public Object next() {
062         return nextToken();
063     }
064 
065     /**
066      * Qualified next method.
067      *
068      @return  Get next token.
069      */
070     public String nextToken() {
071         if (token != null) {
072             final String result = token;
073             token = null;
074             return result;
075         }
076         if (separator != null) {
077             final String result = separator;
078             separator = null;
079             iterate();
080             return result;
081         }
082         return null;
083     }
084 
085     /**
086      * Iterate token and whitespace.
087      */
088     private void iterate() {
089         found = start;
090         while (found < text.length() && !Character.isWhitespace(text.charAt(found))) {
091             found++;
092         }
093         if (found < text.length()) {
094             token = text.substring(start, found);
095             start = found;
096             while (found < text.length() && Character.isWhitespace(text.charAt(found))) {
097                 found++;
098             }
099             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 }