Javolution 6.0.0 java
javolution.text.Cursor Class Reference
Collaboration diagram for javolution.text.Cursor:
[legend]

Public Member Functions

 Cursor ()
 
final int getIndex ()
 
final void setIndex (int i)
 
final boolean atEnd (CharSequence csq)
 
final boolean at (char c, CharSequence csq)
 
final boolean at (CharSet charSet, CharSequence csq)
 
final boolean at (String str, CharSequence csq)
 
final char currentChar (CharSequence csq)
 
final char nextChar (CharSequence csq)
 
final boolean skipAny (char c, CharSequence csq)
 
final boolean skipAny (CharSet charSet, CharSequence csq)
 
final boolean skip (char c, CharSequence csq)
 
final boolean skip (CharSet charSet, CharSequence csq)
 
final boolean skip (String str, CharSequence csq)
 
final CharSequence nextToken (CharSequence csq, char c)
 
final CharSequence nextToken (CharSequence csq, CharSet charSet)
 
final CharSequence head (CharSequence csq)
 
final CharSequence tail (CharSequence csq)
 
final Cursor increment ()
 
final Cursor increment (int i)
 
String toString ()
 
boolean equals (Object obj)
 
int hashCode ()
 

Private Attributes

int index
 

Detailed Description

A parsing cursor over the characters read. Cursor allows for token iterations over any CharSequence. [code] String str = "this is a test"; Cursor cursor = new Cursor(); for (CharSequence token; (token=cursor.nextToken(str, ' '))!= null;) System.out.println(token); [/code] Prints the following output:

       this
       is
       a
       test

Cursors are typically used with TextFormat instances. [code] // Parses decimal number (e.g. "xxx.xxxxxExx" or "NaN") public Decimal parse(CharSequence csq, Cursor cursor) throws IllegalArgumentException { TextFormat<LargeInteger> largeIntegerFormat = TextContext.getFormat(LargeInteger.class); if (cursor.skip("NaN", csq)) return Decimal.NaN; LargeInteger significand = LargeIntegerFormat.parse(csq, cursor); LargeInteger fraction = cursor.skip('.', csq) ? largeIntegerFormat.parse(csq, cursor) : LargeInteger.ZERO; int exponent = cursor.skip(CharSet.valueOf('E', 'e'), csq) ? TypeFormat.parseInt(csq, cursor) : 0; int fractionDigits = fraction.digitLength(); return Decimal.valueOf(significand.E(fractionDigits).plus(fraction), exponent - fractionDigits); } [/code]

Author
Jean-Marie Dautelle
Version
5.4, November 19, 2009

Definition at line 44 of file Cursor.java.

Constructor & Destructor Documentation

◆ Cursor()

javolution.text.Cursor.Cursor ( )

Default constructor.

Definition at line 54 of file Cursor.java.

54 {}

Referenced by javolution.text.Cursor.equals().

Here is the caller graph for this function:

Member Function Documentation

◆ at() [1/3]

final boolean javolution.text.Cursor.at ( char  c,
CharSequence  csq 
)

Indicates if this cursor points to the specified character in the specified character sequence.

Parameters
cthe character to test.
csqthe character sequence iterated by this cursor.
Returns
csq.charAt(this.getIndex()) == c

Definition at line 93 of file Cursor.java.

93  {
94  return index < csq.length() ? csq.charAt(index) == c : false;
95  }

References javolution.text.Cursor.index.

Referenced by javolution.text.Cursor.skip().

Here is the caller graph for this function:

◆ at() [2/3]

final boolean javolution.text.Cursor.at ( CharSet  charSet,
CharSequence  csq 
)

Indicates if this cursor points to any of the specified character in the specified character sequence.

Parameters
charSetany of the character to test.
csqthe character sequence iterated by this cursor.
Returns
csq.charAt(this.getIndex()) == c

Definition at line 105 of file Cursor.java.

105  {
106  return index < csq.length() ? charSet.contains(csq.charAt(index))
107  : false;
108  }

References javolution.text.CharSet.contains(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ at() [3/3]

final boolean javolution.text.Cursor.at ( String  str,
CharSequence  csq 
)

Indicates if this cursor points to the specified characters in the specified sequence.

Parameters
strthe characters to test.
csqthe character sequence iterated by this cursor.
Returns
true if this cursor points to the specified characters; false otherwise.

Definition at line 119 of file Cursor.java.

119  {
120  int i = index;
121  int length = csq.length();
122  for (int j = 0; j < str.length();) {
123  if ((i >= length) || (str.charAt(j++) != csq.charAt(i++)))
124  return false;
125  }
126  return true;
127  }

References javolution.text.Cursor.index.

◆ atEnd()

final boolean javolution.text.Cursor.atEnd ( CharSequence  csq)

Indicates if this cursor points to the end of the specified character sequence.

Parameters
csqthe character sequence iterated by this cursor.
Returns
getIndex() >= csq.length()

Definition at line 81 of file Cursor.java.

81  {
82  return index >= csq.length();
83  }

References javolution.text.Cursor.index.

Referenced by javolution.text.TextFormat< FastCollection<?> >.parse(), javolution.text.TypeFormat.parseBoolean(), javolution.text.TypeFormat.parseByte(), javolution.text.TypeFormat.parseDouble(), javolution.text.TypeFormat.parseInt(), javolution.text.TypeFormat.parseLong(), and javolution.text.TypeFormat.parseShort().

Here is the caller graph for this function:

◆ currentChar()

final char javolution.text.Cursor.currentChar ( CharSequence  csq)

Returns the current character at this cursor position.

Parameters
csqthe character sequence iterated by this cursor.
Returns
the current character this cursor points to.
Exceptions
IndexOutOfBoundsExceptionif this.atEnd(csq)

Definition at line 136 of file Cursor.java.

136  {
137  return csq.charAt(index);
138  }

References javolution.text.Cursor.index.

◆ equals()

boolean javolution.text.Cursor.equals ( Object  obj)

Indicates if this cursor is equals to the specified object.

Returns
true if the specified object is a cursor at the same index; false otherwise.

Definition at line 380 of file Cursor.java.

380  {
381  if (obj == null)
382  return false;
383  if (!(obj instanceof Cursor))
384  return false;
385  return index == ((Cursor) obj).index;
386  }

References javolution.text.Cursor.Cursor(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ getIndex()

final int javolution.text.Cursor.getIndex ( )

Returns this cursor index.

Returns
the index of the next character to parse.

Definition at line 61 of file Cursor.java.

61  {
62  return index;
63  }

References javolution.text.Cursor.index.

Referenced by javolution.text.TypeFormat.parseBoolean(), javolution.text.TypeFormat.parseInt(), and javolution.text.TypeFormat.parseLong().

Here is the caller graph for this function:

◆ hashCode()

int javolution.text.Cursor.hashCode ( )

Returns the hash code for this cursor.

Returns
the hash code value for this object

Definition at line 394 of file Cursor.java.

394  {
395  return index;
396  }

References javolution.text.Cursor.index.

◆ head()

final CharSequence javolution.text.Cursor.head ( CharSequence  csq)

Returns the head of the specified character sequence until
this cursor position.

Returns
the corresponding sub-sequence.

Definition at line 329 of file Cursor.java.

329  {
330  return csq.subSequence(0, index);
331  }

References javolution.text.Cursor.index.

◆ increment() [1/2]

final Cursor javolution.text.Cursor.increment ( )

Increments the cursor index by one.

Returns
this

Definition at line 348 of file Cursor.java.

348  {
349  return increment(1);
350  }

Referenced by javolution.text.TypeFormat.parseBoolean(), javolution.text.TypeFormat.parseInt(), and javolution.text.TypeFormat.parseLong().

Here is the caller graph for this function:

◆ increment() [2/2]

final Cursor javolution.text.Cursor.increment ( int  i)

Increments the cursor index by the specified value.

Parameters
ithe increment value.
Returns
this

Definition at line 358 of file Cursor.java.

358  {
359  index += i;
360  return this;
361  }

References javolution.text.Cursor.index.

◆ nextChar()

final char javolution.text.Cursor.nextChar ( CharSequence  csq)

Returns the next character at this cursor position.The cursor position is incremented by one.

Parameters
csqthe character sequence iterated by this cursor.
Returns
the next character this cursor points to.
Exceptions
IndexOutOfBoundsExceptionif this.atEnd(csq)

Definition at line 148 of file Cursor.java.

148  {
149  return csq.charAt(index++);
150  }

References javolution.text.Cursor.index.

◆ nextToken() [1/2]

final CharSequence javolution.text.Cursor.nextToken ( CharSequence  csq,
char  c 
)

Returns the subsequence from the specified cursor position not holding the specified character. For example:[code] CharSequence csq = "This is a test"; for (CharSequence token; (token=cursor.nextToken(csq, ' '))!= null;) { System.out.println(token); // Prints one word at a time. }[/code]

Parameters
csqthe character sequence iterated by this cursor.
cthe character being skipped.
Returns
the subsequence not holding the specified character or null if none.

Definition at line 278 of file Cursor.java.

278  {
279  int n = csq.length();
280  for (int i = index; i < n; i++) {
281  if (csq.charAt(i) != c) {
282  int j = i;
283  for (; (++j < n) && (csq.charAt(j) != c);) {
284  // Loop until j at the end of sequence or at specified character.
285  }
286  index = j;
287  return csq.subSequence(i, j);
288  }
289  }
290  index = n;
291  return null;
292  }

References javolution.text.Cursor.index.

◆ nextToken() [2/2]

final CharSequence javolution.text.Cursor.nextToken ( CharSequence  csq,
CharSet  charSet 
)

Returns the subsequence from the specified cursor position not holding any of the characters specified. For example:[code] 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. }[/code]

Parameters
csqthe character sequence iterated by this cursor.
charSetthe characters being skipped.
Returns
the subsequence not holding the specified character or null if none.

Definition at line 307 of file Cursor.java.

307  {
308  int n = csq.length();
309  for (int i = index; i < n; i++) {
310  if (!charSet.contains(csq.charAt(i))) {
311  int j = i;
312  for (; (++j < n) && !charSet.contains(csq.charAt(j));) {
313  // Loop until j at the end of sequence or at specified characters.
314  }
315  index = j;
316  return csq.subSequence(i, j);
317  }
318  }
319  index = n;
320  return null;
321  }

References javolution.text.CharSet.contains(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ setIndex()

final void javolution.text.Cursor.setIndex ( int  i)

Sets the cursor current index.

Parameters
ithe index of the next character to parse.

Definition at line 70 of file Cursor.java.

70  {
71  index = i;
72  }

References javolution.text.Cursor.index.

◆ skip() [1/3]

final boolean javolution.text.Cursor.skip ( char  c,
CharSequence  csq 
)

Moves this cursor forward only if at the specified character. This method is equivalent to: [code] if (at(c, csq)) increment(); [/code]

Parameters
cthe character to skip.
csqthe character sequence iterated by this cursor.
Returns
true if this cursor has skipped the specified character;false otherwise.

Definition at line 211 of file Cursor.java.

211  {
212  if (this.at(c, csq)) {
213  index++;
214  return true;
215  } else {
216  return false;
217  }
218  }

References javolution.text.Cursor.at(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ skip() [2/3]

final boolean javolution.text.Cursor.skip ( CharSet  charSet,
CharSequence  csq 
)

Moves this cursor forward only if at any of the specified character. This method is equivalent to: [code] if (at(charSet, csq)) increment(); [/code]

Parameters
charSetholding the characters to skip.
csqthe character sequence iterated by this cursor.
Returns
true if this cursor has skipped any the specified character;false otherwise.

Definition at line 233 of file Cursor.java.

233  {
234  if (this.at(charSet, csq)) {
235  index++;
236  return true;
237  } else {
238  return false;
239  }
240  }

References javolution.text.Cursor.at(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ skip() [3/3]

final boolean javolution.text.Cursor.skip ( String  str,
CharSequence  csq 
)

Moves this cursor forward only if at the specified string. This method is equivalent to: [code] if (at(str, csq)) increment(str.length()); [/code]

Parameters
strthe string to skip.
csqthe character sequence iterated by this cursor.
Returns
true if this cursor has skipped the specified string;false otherwise (e.g. end of sequence reached).

Definition at line 256 of file Cursor.java.

256  {
257  if (this.at(str, csq)) {
258  index += str.length();
259  return true;
260  } else {
261  return false;
262  }
263  }

References javolution.text.Cursor.at(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ skipAny() [1/2]

final boolean javolution.text.Cursor.skipAny ( char  c,
CharSequence  csq 
)

Moves this cursor forward until it points to a character different from the specified character.

Parameters
cthe character to skip.
csqthe character sequence iterated by this cursor.
Returns
true if this cursor has skipped at least one character;false otherwise (e.g. end of sequence reached).

Definition at line 162 of file Cursor.java.

162  {
163  int i = index;
164  int n = csq.length();
165  for (; (i < n) && (csq.charAt(i) == c); i++) {}
166  if (i == index)
167  return false; // Cursor did not moved.
168  index = i;
169  return true;
170  }

References javolution.text.Cursor.index.

◆ skipAny() [2/2]

final boolean javolution.text.Cursor.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. For example: [code] // 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)); }[/code]

Parameters
charSetthe character to skip.
csqthe character sequence iterated by this cursor.
Returns
true if this cursor has skipped at least one character;false otherwise (e.g. end of sequence reached).

Definition at line 188 of file Cursor.java.

188  {
189  int i = index;
190  int n = csq.length();
191  for (; (i < n) && charSet.contains(csq.charAt(i)); i++) {}
192  if (i == index)
193  return false; // Cursor did not moved.
194  index = i;
195  return true;
196  }

References javolution.text.CharSet.contains(), and javolution.text.Cursor.index.

Here is the call graph for this function:

◆ tail()

final CharSequence javolution.text.Cursor.tail ( CharSequence  csq)

Returns the tail of the specified character sequence starting at this cursor position.

Returns
the corresponding sub-sequence.

Definition at line 339 of file Cursor.java.

339  {
340  return csq.subSequence(index, csq.length());
341  }

References javolution.text.Cursor.index.

Referenced by javolution.text.TextFormat< FastCollection<?> >.parse(), javolution.text.TypeFormat.parseBoolean(), javolution.text.TypeFormat.parseByte(), javolution.text.TypeFormat.parseDouble(), javolution.text.TypeFormat.parseInt(), javolution.text.TypeFormat.parseLong(), and javolution.text.TypeFormat.parseShort().

Here is the caller graph for this function:

◆ toString()

String javolution.text.Cursor.toString ( )

Returns the string representation of this cursor.

Returns
the index value as a string.

Definition at line 369 of file Cursor.java.

369  {
370  return "Cursor: " + index;
371  }

References javolution.text.Cursor.index.

Member Data Documentation

◆ index


The documentation for this class was generated from the following file:
javolution.text.Cursor.at
final boolean at(char c, CharSequence csq)
Definition: Cursor.java:93
javolution.text.Cursor.index
int index
Definition: Cursor.java:49
javolution.text.Cursor.Cursor
Cursor()
Definition: Cursor.java:54
javolution.text.Cursor.increment
final Cursor increment()
Definition: Cursor.java:348