|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectParsePosition
Cursor
public class Cursor
This class represents a parsing cursor over characters. Cursor
allows for token iterations over any CharSequence.
Prints the following output:
CharSequence csq = "this is a test";
Cursor cursor = Cursor.newInstance();
try {
for (CharSequence token; (token=cursor.nextToken(csq, ' '))!= null;)
System.out.println(token);
} finally {
Cursor.recycle(cursor);
}
this
is
a
test
Cursors are typically used with TextFormat instances.
// Parses decimal number (e.g. "xxx.xxxxxExx" or "NaN")
public Decimal parse(CharSequence csq, Cursor cursor) {
if (cursor.skip("NaN", csq))
return Decimal.NaN;
LargeInteger significand = LargeInteger.TEXT_FORMAT.parse(csq, cursor);
LargeInteger fraction = cursor.skip('.', csq) ? LargeInteger.TEXT_FORMAT.parse(csq, cursor) : LargeInteger.ZERO;
int exponent = cursor.skip(CharSet.valueOf('E', 'e'), csq) ? TypeFormat.parseInt(csq, 10, cursor) : 0;
int fractionDigits = fraction.digitLength();
return Decimal.valueOf(significand.E(fractionDigits).plus(fraction), exponent - fractionDigits);
}
| Constructor Summary | |
|---|---|
Cursor()
Default constructor. |
|
| Method Summary | |
|---|---|
boolean |
at(char c,
CharSequence csq)
Indicates if this cursor points to the specified character in the specified character sequence. |
boolean |
at(CharSet charSet,
CharSequence csq)
Indicates if this cursor points to any of the specified character in the specified character sequence. |
boolean |
at(String str,
CharSequence csq)
Indicates if this cursor points to the specified characters in the specified sequence. |
boolean |
atEnd(CharSequence csq)
Indicates if this cursor points to the end of the specified character sequence. |
boolean |
equals(Object obj)
Indicates if this cursor is equals to the specified object. |
int |
getIndex()
Returns this cursor index. |
int |
hashCode()
Returns the hash code for this cursor. |
Cursor |
increment()
Increments the cursor index by one. |
Cursor |
increment(int i)
Increments the cursor index by the specified value. |
static Cursor |
newInstance()
Returns a factory produced instance which can be recycled
after usage. |
char |
nextChar(CharSequence csq)
Returns the next character at this cursor position.The cursor position is incremented by one. |
CharSequence |
nextToken(CharSequence csq,
char c)
Returns the subsequence from the specified cursor position not holding the specified character. |
CharSequence |
nextToken(CharSequence csq,
CharSet charSet)
Returns the subsequence from the specified cursor position not holding any of the characters specified. |
static void |
recycle(Cursor cursor)
Recycles the specified factory produced cursor. |
void |
reset()
Resets this cursor instance. |
void |
setIndex(int i)
Sets the cursor current index. |
boolean |
skip(char c,
CharSequence csq)
Moves this cursor forward only if at the specified character. |
boolean |
skip(CharSet charSet,
CharSequence csq)
Moves this cursor forward only if at any of the specified character. |
boolean |
skip(String str,
CharSequence csq)
Moves this cursor forward only if at the specified string. |
boolean |
skipAny(char c,
CharSequence csq)
Moves this cursor forward until it points to a character different from the specified character. |
boolean |
skipAny(CharSet charSet,
CharSequence csq)
Moves this cursor forward until it points to a character different from any of the character in the specified set. |
String |
toString()
Returns the string representation of this cursor. |
| Methods inherited from class ParsePosition |
|---|
getErrorIndex, setErrorIndex |
| Methods inherited from class Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Cursor()
| Method Detail |
|---|
public static Cursor newInstance()
recycled
after usage.
public static void recycle(Cursor cursor)
produced cursor.
cursor - the cursor to recycle.public final int getIndex()
getIndex in class ParsePositionpublic void setIndex(int i)
setIndex in class ParsePositioni - the index of the next character to parse.public final boolean atEnd(CharSequence csq)
csq - the character sequence iterated by this cursor.
getIndex() >= csq.length()
public final boolean at(char c,
CharSequence csq)
c - the character to test.csq - the character sequence iterated by this cursor.
csq.charAt(this.getIndex()) == c
public final boolean at(CharSet charSet,
CharSequence csq)
charSet - any of the character to test.csq - the character sequence iterated by this cursor.
csq.charAt(this.getIndex()) == c
public final boolean at(String str,
CharSequence csq)
str - the characters to test.csq - the character sequence iterated by this cursor.
true if this cursor points to the specified
characters; false otherwise.public final char nextChar(CharSequence csq)
csq - the character sequence iterated by this cursor.
IndexOutOfBoundsException - if this.atEnd(csq)
public final boolean skipAny(char c,
CharSequence csq)
c - the character to skip.csq - the character sequence iterated by this cursor.
true if this cursor has skipped at least one
character;false otherwise (e.g. end of sequence
reached).
public final boolean skipAny(CharSet charSet,
CharSequence csq)
// Reads numbers separated by tabulations or spaces.
FastTable<Integer> numbers = new FastTable<Integer>();
while (cursor.skipAny(CharSet.SPACE_OR_TAB, csq)) {
numbers.add(TypeFormat.parseInt(csq, cursor));
}
charSet - the character to skip.csq - the character sequence iterated by this cursor.
true if this cursor has skipped at least one
character;false otherwise (e.g. end of sequence
reached).
public final boolean skip(char c,
CharSequence csq)
if (at(c, csq))
increment();
c - the character to skip.csq - the character sequence iterated by this cursor.
true if this cursor has skipped the specified
character;false otherwise.
public final boolean skip(CharSet charSet,
CharSequence csq)
if (at(charSet, csq))
increment();
charSet - holding the characters to skip.csq - the character sequence iterated by this cursor.
true if this cursor has skipped any the specified
character;false otherwise.
public final boolean skip(String str,
CharSequence csq)
if (at(str, csq))
increment(str.length());
str - the string to skip.csq - the character sequence iterated by this cursor.
true if this cursor has skipped the specified
string;false otherwise (e.g. end of sequence
reached).
public final CharSequence nextToken(CharSequence csq,
char c)
CharSequence csq = "This is a test";
for (CharSequence token; (token=cursor.nextToken(csq, ' '))!= null;) {
System.out.println(token); // Prints one word at a time.
}
csq - the character sequence iterated by this cursor.c - the character being skipped.
null if none.
public final CharSequence nextToken(CharSequence csq,
CharSet charSet)
CharSequence csq = "This is a test";
for (CharSequence token; (token=cursor.nextToken(csq, CharSet.WHITESPACE))!= null;) {
System.out.println(token); // Prints one word at a time.
}
csq - the character sequence iterated by this cursor.charSet - the characters being skipped.
null if none.public final Cursor increment()
thispublic final Cursor increment(int i)
i - the increment value.
thispublic String toString()
toString in class ParsePositionpublic boolean equals(Object obj)
equals in class ParsePositiontrue if the specified object is a cursor
at the same index; false otherwise.public int hashCode()
hashCode in class ParsePositionpublic void reset()
reset in interface ReusableReusable
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||