net.sf.mmm.util.lang.api
Class StringTokenizer

java.lang.Object
  extended by net.sf.mmm.util.collection.base.AbstractIterator<String>
      extended by net.sf.mmm.util.lang.api.StringTokenizer
All Implemented Interfaces:
Iterable<String>, Iterator<String>

public class StringTokenizer
extends AbstractIterator<String>
implements Iterable<String>

This is a rewrite of the awkward StringTokenizer provided by the JDK. This implementation returns an empty String if a duplicate delimiter is detected. Further it implements Iterable and can be used in enhanced for-loops.
ATTENTION:
Returning an empty String also for duplicated delimited might NOT always be desired (especially when delimiter is whitespace).

Since:
1.0.2
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Field Summary
private  char[] delimiters
          The characters that will be detected as delimiters.
private  char[] escapeEnd
          The string to end escaping of a token.
private  char[] escapeStart
          The string to start escaping of a token.
private  int index
          The current index in string.
private  char[] string
          The string to be tokenized
 
Constructor Summary
StringTokenizer(char[] string, char... delimiters)
          The constructor.
StringTokenizer(char[] string, String escapeStart, String escapeEnd, char... delimiters)
          The constructor.
StringTokenizer(String string, char... delimiters)
          The constructor.
StringTokenizer(String string, String delimiters)
          The constructor.
StringTokenizer(String string, String escapeStart, String escapeEnd, char... delimiters)
          The constructor that allows escaping.
 
Method Summary
private static boolean containsDelimiter(char[] escape, char[] delimiters)
          This method checks that the given escape sequence does NOT contain any of the delimiters.
protected  boolean containsSubstring(char[] substring, int startIndex)
          This method tests if the string contains the given substring starting at the given index.
protected  String findNext()
          This method tries to find the next element.
 boolean hasMoreTokens()
           
 Iterator<String> iterator()
          
 String nextToken()
           
 String toString()
          
 
Methods inherited from class net.sf.mmm.util.collection.base.AbstractIterator
findFirst, hasNext, next, remove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

string

private final char[] string
The string to be tokenized


delimiters

private final char[] delimiters
The characters that will be detected as delimiters.


escapeStart

private final char[] escapeStart
The string to start escaping of a token.


escapeEnd

private final char[] escapeEnd
The string to end escaping of a token.


index

private int index
The current index in string.

Constructor Detail

StringTokenizer

public StringTokenizer(String string,
                       char... delimiters)
The constructor.

Parameters:
string - is the string to be tokenized.
delimiters - are the characters that will be detected as delimiters.

StringTokenizer

public StringTokenizer(String string,
                       String delimiters)
The constructor.

Parameters:
string - is the string to be tokenized.
delimiters - is a String with all the characters that will be detected as delimiters.

StringTokenizer

public StringTokenizer(String string,
                       String escapeStart,
                       String escapeEnd,
                       char... delimiters)
                throws NlsIllegalArgumentException
The constructor that allows escaping. The escaping can be cascaded:
 new StringTokenizer("{[foo,{[bar,thing]}]},some", "{[", "]}", ',').next()
 
will return "foo,{[bar,thing]}".

Parameters:
string - is the string to be tokenized.
escapeStart - is the string used to start escaping of a token. The string has to be free of delimiters.
escapeEnd - is the string used to end escaping of a token. The string has to be free of delimiters.
delimiters - are the characters that will be detected as delimiters.
Throws:
NlsIllegalArgumentException - if escapeStart or escapeEnd is an empty string or contains a character of delimiters, or one of them is null while the other is not, or both are not null but equal to each other.
Since:
2.0.0

StringTokenizer

public StringTokenizer(char[] string,
                       String escapeStart,
                       String escapeEnd,
                       char... delimiters)
                throws NlsIllegalArgumentException
The constructor.

Parameters:
string - is the string to be tokenized.
escapeStart - is the string used to start escaping of a token. May NOT be the empty string. The string has to be free of delimiters.
escapeEnd - is the string used to end escaping of a token. May NOT be the empty string.The string has to be free of delimiters.
delimiters - are the characters that will be detected as delimiters.
Throws:
NlsIllegalArgumentException - if escapeStart or escapeEnd is an empty string or contains a character of delimiters, or one of them is null while the other is not, or both are not null but equal to each other.
Since:
2.0.0
See Also:
StringTokenizer(String, String, String, char...)

StringTokenizer

public StringTokenizer(char[] string,
                       char... delimiters)
The constructor.

Parameters:
string - is the string to be tokenized.
delimiters - are the characters that will be detected as delimiters.
Method Detail

containsDelimiter

private static boolean containsDelimiter(char[] escape,
                                         char[] delimiters)
This method checks that the given escape sequence does NOT contain any of the delimiters.

Parameters:
escape - is the escape-sequence to check.
delimiters - are the delimiters that should NOT be contained in escape.
Returns:
true if escape contains a character of delimiters, false otherwise.

iterator

public Iterator<String> iterator()

Specified by:
iterator in interface Iterable<String>

findNext

protected String findNext()
This method tries to find the next element.

Specified by:
findNext in class AbstractIterator<String>
Returns:
the next element or null if done.

containsSubstring

protected boolean containsSubstring(char[] substring,
                                    int startIndex)
This method tests if the string contains the given substring starting at the given index.

Parameters:
substring - is the substring to check for.
startIndex - is the start index in string.
Returns:
true if the given substring was found at index.

hasMoreTokens

public boolean hasMoreTokens()
Returns:
true if AbstractIterator.next() is available, false otherwise.
See Also:
StringTokenizer.hasMoreTokens()

nextToken

public String nextToken()
Returns:
the next token.
See Also:
StringTokenizer.nextToken()

toString

public String toString()

Overrides:
toString in class Object


Copyright © 2001-2010 mmm-Team. All Rights Reserved.