public abstract class SearchIterator extends Object
SearchIterator defines a protocol for text searching. Subclasses provide concrete implementations of various search algorithms. For example, StringSearch implements language-sensitive pattern matching based on the comparison rules defined in a RuleBasedCollator object.
Other options for searching include using a BreakIterator to restrict the points at which matches are detected.
SearchIterator provides an API that is similar to that of other text iteration classes such as BreakIterator. Using this class, it is easy to scan through text looking for all occurrences of a given pattern. The following example uses a StringSearch object to find all instances of "fox" in the target string. Any other subclass of SearchIterator can be used in an identical manner.
String target = "The quick brown fox jumped over the lazy fox";
String pattern = "fox";
SearchIterator iter = new StringSearch(pattern, target);
for (int pos = iter.first(); pos != SearchIterator.DONE;
pos = iter.next()) {
System.out.println("Found match at " + pos +
", length is " + iter.getMatchLength());
}
BreakIterator
,
RuleBasedCollator
Modifier and Type | Class and Description |
---|---|
static class |
SearchIterator.ElementComparisonType
Option to control how collation elements are compared.
|
Modifier and Type | Field and Description |
---|---|
protected BreakIterator |
breakIterator
The BreakIterator to define the boundaries of a logical match.
|
static int |
DONE
DONE is returned by previous() and next() after all valid matches have
been returned, and by first() and last() if there are no matches at all.
|
protected int |
matchLength
Length of the most current match in target text.
|
protected CharacterIterator |
targetText
Target text for searching.
|
Modifier | Constructor and Description |
---|---|
protected |
SearchIterator(CharacterIterator target,
BreakIterator breaker)
Protected constructor for use by subclasses.
|
Modifier and Type | Method and Description |
---|---|
int |
first()
Returns the first index at which the string text matches the search
pattern.
|
int |
following(int position)
Returns the first index equal or greater than position at which the
string text matches the search pattern.
|
BreakIterator |
getBreakIterator()
Returns the BreakIterator that is used to restrict the indexes at which
matches are detected.
|
SearchIterator.ElementComparisonType |
getElementComparisonType()
Returns the collation element comparison type.
|
abstract int |
getIndex()
Return the current index in the text being searched.
|
String |
getMatchedText()
Returns the text that was matched by the most recent call to
first() , next() , previous() , or last() . |
int |
getMatchLength()
Returns the length of text in the string which matches the search
pattern.
|
int |
getMatchStart()
Returns the index to the match in the text string that was searched.
|
CharacterIterator |
getTarget()
Return the string text to be searched.
|
protected abstract int |
handleNext(int start)
Abstract method which subclasses override to provide the mechanism
for finding the next match in the target text.
|
protected abstract int |
handlePrevious(int startAt)
Abstract method which subclasses override to provide the mechanism for
finding the previous match in the target text.
|
boolean |
isOverlapping()
Return true if the overlapping property has been set.
|
int |
last()
Returns the last index in the target text at which it matches the
search pattern.
|
int |
next()
Returns the index of the next point at which the text matches the
search pattern, starting from the current position
The iterator is adjusted so that its current index (as returned by
getIndex() ) is the match position if one was found. |
int |
preceding(int position)
Returns the first index less than position at which the string
text matches the search pattern.
|
int |
previous()
Returns the index of the previous point at which the string text
matches the search pattern, starting at the current position.
|
void |
reset()
Resets the iteration.
|
void |
setBreakIterator(BreakIterator breakiter)
Set the BreakIterator that will be used to restrict the points
at which matches are detected.
|
void |
setElementComparisonType(SearchIterator.ElementComparisonType type)
Sets the collation element comparison type.
|
void |
setIndex(int position)
Sets the position in the target text at which the next search will start.
|
protected void |
setMatchLength(int length)
Sets the length of the most recent match in the target text.
|
protected void |
setMatchNotFound()
Deprecated.
This API is ICU internal only.
|
void |
setOverlapping(boolean allowOverlap)
Determines whether overlapping matches are returned.
|
void |
setTarget(CharacterIterator text)
Set the target text to be searched.
|
protected BreakIterator breakIterator
setBreakIterator(BreakIterator)
,
getBreakIterator()
,
BreakIterator
protected CharacterIterator targetText
setTarget(CharacterIterator)
,
getTarget()
protected int matchLength
setMatchLength(int)
,
getMatchLength()
public static final int DONE
previous()
,
next()
,
Constant Field Valuesprotected SearchIterator(CharacterIterator target, BreakIterator breaker)
BreakIterator
.target
- The target text to be searched.breaker
- A BreakIterator
that is used to determine the
boundaries of a logical match. This argument can be null.IllegalArgumentException
- thrown when argument target is null,
or of length 0BreakIterator
public void setIndex(int position)
Sets the position in the target text at which the next search will start. This method clears any previous match.
position
- position from which to start the next searchIndexOutOfBoundsException
- thrown if argument position is out
of the target text range.getIndex()
public void setOverlapping(boolean allowOverlap)
The default setting of this property is false
allowOverlap
- flag indicator if overlapping matches are allowedisOverlapping()
public void setBreakIterator(BreakIterator breakiter)
breakiter
- A BreakIterator that will be used to restrict the
points at which matches are detected. If a match is
found, but the match's start or end index is not a
boundary as determined by the BreakIterator
,
the match will be rejected and another will be searched
for. If this parameter is null, no break
detection is attempted.BreakIterator
public void setTarget(CharacterIterator text)
text
- new text iterator to look for match,IllegalArgumentException
- thrown when text is null or has
0 lengthgetTarget()
public int getMatchStart()
first()
, next()
, previous()
, or last()
.
Just after construction, or after a searching method returns
DONE
, this method will return DONE
.
Use getMatchLength()
to get the matched string length.
first()
,
next()
,
previous()
,
last()
public abstract int getIndex()
DONE
is returned.public int getMatchLength()
first()
, next()
, previous()
, or last()
.
Just after construction, or after a searching method returns
DONE
, this method will return 0.first()
,
next()
,
previous()
,
last()
public BreakIterator getBreakIterator()
setBreakIterator(com.ibm.icu.text.BreakIterator)
.
If the BreakIterator
has not been set, null will be returned.
See setBreakIterator(com.ibm.icu.text.BreakIterator)
for more information.setBreakIterator(com.ibm.icu.text.BreakIterator)
,
BreakIterator
public CharacterIterator getTarget()
public String getMatchedText()
first()
, next()
, previous()
, or last()
.
If the iterator is not pointing at a valid match (e.g. just after
construction or after DONE
has been returned,
returns an empty string.first()
,
next()
,
previous()
,
last()
public int next()
getIndex()
) is the match position if one was found.
If a match is not found, DONE
will be returned and
the iterator will be adjusted to a position after the end of the text
string.DONE
if there are no more matches.getIndex()
public int previous()
getIndex()
) is the match position if one was found.
If a match is not found, DONE
will be returned and
the iterator will be adjusted to the index DONE
.DONE
if there are no more matches.getIndex()
public boolean isOverlapping()
setOverlapping(boolean)
for more information.setOverlapping(boolean)
public void reset()
public final int first()
getIndex()
) is the match position if one
was found.
If a match is not found, DONE
will be returned and
the iterator will be adjusted to the index DONE
.DONE
if there are no matches.getIndex()
public final int following(int position)
getIndex()
) is the
match position if one was found.
If a match is not found, DONE
will be returned and the
iterator will be adjusted to the index DONE
.position
- where search if to start from.DONE
if there are no matches.IndexOutOfBoundsException
- If position is less than or greater
than the text range for searching.getIndex()
public final int last()
getIndex()
) is the match position if one was
found.
If a match is not found, DONE
will be returned and
the iterator will be adjusted to the index DONE
.DONE
if
there are no matches.getIndex()
public final int preceding(int position)
getIndex()
) is the match
position if one was found. If a match is not found,
DONE
will be returned and the iterator will be
adjusted to the index DONE
When the overlapping option (isOverlapping()
) is off, the last index of the
result match is always less than position.
When the overlapping option is on, the result match may span across
position.
position
- where search is to start from.DONE
if there are
no matches.IndexOutOfBoundsException
- If position is less than or greater than
the text range for searchinggetIndex()
protected void setMatchLength(int length)
length
- new length to sethandleNext(int)
,
handlePrevious(int)
protected abstract int handleNext(int start)
If a match is found, the implementation should return the index at
which the match starts and should call
setMatchLength(int)
with the number of characters
in the target text that make up the match. If no match is found, the
method should return DONE
.
start
- The index in the target text at which the search
should start.DONE
is returnedsetMatchLength(int)
protected abstract int handlePrevious(int startAt)
If a match is found, the implementation should return the index at
which the match starts and should call
setMatchLength(int)
with the number of characters
in the target text that make up the match. If no match is found, the
method should return DONE
.
startAt
- The index in the target text at which the search
should start.DONE
is returnedsetMatchLength(int)
@Deprecated protected void setMatchNotFound()
public void setElementComparisonType(SearchIterator.ElementComparisonType type)
The default comparison type is SearchIterator.ElementComparisonType.STANDARD_ELEMENT_COMPARISON
.
SearchIterator.ElementComparisonType
,
getElementComparisonType()
public SearchIterator.ElementComparisonType getElementComparisonType()
SearchIterator.ElementComparisonType
,
setElementComparisonType(ElementComparisonType)
Copyright © 2016 Unicode, Inc. and others.