Package com.twelvemonkeys.util
Class StringTokenIterator
java.lang.Object
com.twelvemonkeys.util.AbstractTokenIterator
com.twelvemonkeys.util.StringTokenIterator
- All Implemented Interfaces:
TokenIterator
,Enumeration<String>
,Iterator<String>
StringTokenIterator, a drop-in replacement for
StringTokenizer
.
StringTokenIterator has the following features:
- Iterates over a strings, 20-50% faster than
StringTokenizer
(and magnitudes faster thanString.split(..)
orPattern.split(..)
) - Implements the
Iterator
interface - Optionally returns delimiters
- Optionally returns empty elements
- Optionally iterates in reverse
- Resettable
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/StringTokenIterator.java#1 $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final char[]
static final int
private final boolean
private final boolean
private final char
Stores the value of the delimiter character with the highest value.private final int
private String
private String
private int
private final boolean
static final int
private final String
-
Constructor Summary
ConstructorsModifierConstructorDescriptionStringTokenIterator
(String pString) Creates a StringTokenIteratorprivate
StringTokenIterator
(String pString, char[] pDelimiters, int pDirection, boolean pIncludeDelimiters, boolean pIncludeEmpty) Implementation.StringTokenIterator
(String pString, String pDelimiters) Creates a StringTokenIteratorStringTokenIterator
(String pString, String pDelimiters, boolean pIncludeDelimiters) Creates a StringTokenIteratorStringTokenIterator
(String pString, String pDelimiters, int pDirection) Creates a StringTokenIteratorStringTokenIterator
(String pString, String pDelimiters, int pDirection, boolean pIncludeDelimiters, boolean pIncludeEmpty) Creates a StringTokenIterator -
Method Summary
Modifier and TypeMethodDescriptionprivate String
private String
private String
boolean
hasNext()
Returnstrue
if the iteration has more elements.private static char
initMaxDelimiter
(char[] pDelimiters) Returns the highest char in the delimiter set.next()
Returns the next element in the iteration.void
reset()
Resets this iterator.private int
private int
private static char[]
toCharArray
(String pDelimiters) Methods inherited from class com.twelvemonkeys.util.AbstractTokenIterator
hasMoreElements, hasMoreTokens, nextElement, nextToken, remove
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Enumeration
asIterator
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Field Details
-
string
-
delimiters
private final char[] delimiters -
position
private int position -
maxPosition
private final int maxPosition -
next
-
nextDelimiter
-
includeDelimiters
private final boolean includeDelimiters -
includeEmpty
private final boolean includeEmpty -
reverse
private final boolean reverse -
FORWARD
public static final int FORWARD- See Also:
-
REVERSE
public static final int REVERSE- See Also:
-
maxDelimiter
private final char maxDelimiterStores the value of the delimiter character with the highest value. It is used to optimize the detection of delimiter characters.
-
-
Constructor Details
-
StringTokenIterator
Creates a StringTokenIterator- Parameters:
pString
- the string to be parsed.
-
StringTokenIterator
Creates a StringTokenIterator- Parameters:
pString
- the string to be parsed.pDelimiters
- the delimiters.
-
StringTokenIterator
Creates a StringTokenIterator- Parameters:
pString
- the string to be parsed.pDelimiters
- the delimiters.pDirection
- iteration direction.
-
StringTokenIterator
Creates a StringTokenIterator- Parameters:
pString
- the string to be parsed.pDelimiters
- the delimiters.pIncludeDelimiters
- flag indicating whether to return delimiters as tokens.
-
StringTokenIterator
public StringTokenIterator(String pString, String pDelimiters, int pDirection, boolean pIncludeDelimiters, boolean pIncludeEmpty) Creates a StringTokenIterator- Parameters:
pString
- the string to be parsed.pDelimiters
- the delimiters.pDirection
- iteration direction.pIncludeDelimiters
- flag indicating whether to return delimiters as tokens.pIncludeEmpty
- flag indicating whether to return empty tokens
-
StringTokenIterator
private StringTokenIterator(String pString, char[] pDelimiters, int pDirection, boolean pIncludeDelimiters, boolean pIncludeEmpty) Implementation.- Parameters:
pString
- the string to be parsed.pDelimiters
- the delimiters.pDirection
- iteration direction.pIncludeDelimiters
- flag indicating whether to return delimiters as tokens.pIncludeEmpty
- flag indicating whether to return empty tokens
-
-
Method Details
-
toCharArray
-
initMaxDelimiter
private static char initMaxDelimiter(char[] pDelimiters) Returns the highest char in the delimiter set.- Parameters:
pDelimiters
- the delimiter set- Returns:
- the highest char
-
reset
public void reset()Resets this iterator. -
hasNext
public boolean hasNext()Returnstrue
if the iteration has more elements. (In other words, returnstrue
ifnext
would return an element rather than throwing an exception.)- Returns:
true
if the iterator has more elements.
-
fetchNext
-
fetchReverse
-
fetchForward
-
scanForNext
private int scanForNext() -
scanForPrev
private int scanForPrev() -
next
Returns the next element in the iteration.- Returns:
- the next element in the iteration.
- Throws:
NoSuchElementException
- iteration has no more elements.
-