de.dfki.lt.tools.tokenizer.annotate
Class FastAnnotatedString

java.lang.Object
  extended byde.dfki.lt.tools.tokenizer.annotate.FastAnnotatedString
All Implemented Interfaces:
AnnotatedString, java.text.CharacterIterator, java.lang.Cloneable

public class FastAnnotatedString
extends java.lang.Object
implements AnnotatedString

FastAnnotatedString is a fast implementation of the AnnotatedString interface. It reserves an array of objects and an array of booleans for each newly introduced annotation key. This provides fast access at the cost of memory. So only introduce new annotation keys if neccessary.

Version:
$Id: FastAnnotatedString.java,v 1.4 2005/04/12 08:47:37 steffen Exp $
Author:
Joerg Steffen, DFKI

Field Summary
 
Fields inherited from interface java.text.CharacterIterator
DONE
 
Constructor Summary
FastAnnotatedString(java.lang.String anInputText)
          This create a new instance of FastAnnotatedString for a text in inputString.
 
Method Summary
 void annotate(java.lang.String key, java.lang.Object value, int aBeginIndex, int anEndIndex)
          Adds an annotation to a subrange of the string.
 char charAt(int charIndex)
          This returns the character from the specified position without changing the index.
 java.lang.Object clone()
          This create a copy of this object.
 char current()
          This gets the character at the current position (as returned by getIndex()).
 int findNextAnnotation(java.lang.String key)
          This returns the index of the first character annotated with the given annotation key following the run containing the current character with respect to the given annotation key.
 char first()
          This sets the position to getBeginIndex() and returns the character at that position.
 java.lang.Object getAnnotation(java.lang.String key)
          This returns the annotation value of the string at the current index for a given key.
 int getBeginIndex()
          This returns the start index of the text.
 int getEndIndex()
          This returns the end index of the text.
 int getIndex()
          This returns the current index.
 int getRunLimit(java.lang.String key)
          This returns the index of the first character following the run with respect to the given annotation key containing the current character.
 int getRunStart(java.lang.String key)
          This returns the index of the first character of the run with respect to the given annotation key containing the current character.
 char last()
          This sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position
 char next()
          This increments the index by one and returns the character at the new index.
 char previous()
          This decrements the index by one and returns the character at the new index.
 char setIndex(int anIndex)
          This sets the position to the specified position in the text and returns that character.
 java.lang.String substring(int aBeginIndex, int anEndIndex)
          This returns the substring between the specified indices.
 java.lang.String toString()
          This returns the surface string of the annotated string.
 java.lang.String toString(java.lang.String key)
          This returns a string representation of the annotated string with the annotation for the given attribute key.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FastAnnotatedString

public FastAnnotatedString(java.lang.String anInputText)
This create a new instance of FastAnnotatedString for a text in inputString.

Parameters:
anInputText - a String with the text to annotate
Method Detail

first

public char first()
This sets the position to getBeginIndex() and returns the character at that position.

Specified by:
first in interface java.text.CharacterIterator
Returns:
the first character in the text, or DONE if the text is empty

last

public char last()
This sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position

Specified by:
last in interface java.text.CharacterIterator
Returns:
the last character in the text, or DONE if the text is empty

current

public char current()
This gets the character at the current position (as returned by getIndex()).

Specified by:
current in interface java.text.CharacterIterator
Returns:
the character at the current position or DONE if the current position is off the end of the text

next

public char next()
This increments the index by one and returns the character at the new index. If the resulting index is greater or equal to getEndIndex(), the current index is reset to getEndIndex() and a value of DONE is returned.

Specified by:
next in interface java.text.CharacterIterator
Returns:
the character at the new position or DONE if the new position is off the end of the text range

previous

public char previous()
This decrements the index by one and returns the character at the new index. If the current index is getBeginIndex(), the index remains at getBeginIndex() and a value of DONE is returned.

Specified by:
previous in interface java.text.CharacterIterator
Returns:
the character at the new position or DONE if the current position is equal to getBeginIndex()

getBeginIndex

public int getBeginIndex()
This returns the start index of the text.

Specified by:
getBeginIndex in interface java.text.CharacterIterator
Returns:
the index at which the text begins

getEndIndex

public int getEndIndex()
This returns the end index of the text. This index is the index following the last character of the text.

Specified by:
getEndIndex in interface java.text.CharacterIterator
Returns:
the index after the last character in the text

getIndex

public int getIndex()
This returns the current index.

Specified by:
getIndex in interface java.text.CharacterIterator
Returns:
the current index

setIndex

public char setIndex(int anIndex)
This sets the position to the specified position in the text and returns that character.

Specified by:
setIndex in interface java.text.CharacterIterator
Parameters:
anIndex - the index within the text; valid values range from getBeginIndex() to getEndIndex(); an IllegalArgumentException is thrown if an invalid value is supplied
Returns:
the character at the specified position or DONE if the specified position is equal to getEndIndex()

clone

public java.lang.Object clone()
This create a copy of this object.

Specified by:
clone in interface java.text.CharacterIterator
Returns:
a copy of this

charAt

public char charAt(int charIndex)
This returns the character from the specified position without changing the index.

Specified by:
charAt in interface AnnotatedString
Parameters:
charIndex - the index within the text; valid values range from getBeginIndex() to getEndIndex(); an IllegalArgumentException is thrown if an invalid value is supplied
Returns:
the character at the specified position or DONE if the specified position is equal to getEndIndex()

substring

public java.lang.String substring(int aBeginIndex,
                                  int anEndIndex)
This returns the substring between the specified indices.

Specified by:
substring in interface AnnotatedString
Parameters:
aBeginIndex - an int with the index of the first character of the range
anEndIndex - an int with the index of the character following the last character of the range
Returns:
a String with the substring
Throws:
java.lang.IllegalArgumentException - if aBeginIndex is less then 0, anEndIndex is greater than the length of the string, or aBeginIndex and anEndIndex together don't define a non-empty subrange of the string

annotate

public void annotate(java.lang.String key,
                     java.lang.Object value,
                     int aBeginIndex,
                     int anEndIndex)
Adds an annotation to a subrange of the string.

Specified by:
annotate in interface AnnotatedString
Parameters:
key - a String with the annotation key
value - a Object with the annotation value
aBeginIndex - an int with the index of the first character of the range
anEndIndex - an int with the index of the character following the last character of the range
Throws:
java.lang.IllegalArgumentException - if aBeginIndex is less then 0, anEndIndex is greater than the length of the string, or aBeginIndex and anEndIndex together don't define a non-empty subrange of the string

getAnnotation

public java.lang.Object getAnnotation(java.lang.String key)
This returns the annotation value of the string at the current index for a given key.

Specified by:
getAnnotation in interface AnnotatedString
Parameters:
key - a String with the annotation key
Returns:
an Object with the annotation value or null if there is no annotation with the given key at that position

getRunStart

public int getRunStart(java.lang.String key)
This returns the index of the first character of the run with respect to the given annotation key containing the current character.

Specified by:
getRunStart in interface AnnotatedString
Parameters:
key - String with an annotation key
Returns:
an int with the index

getRunLimit

public int getRunLimit(java.lang.String key)
This returns the index of the first character following the run with respect to the given annotation key containing the current character.

Specified by:
getRunLimit in interface AnnotatedString
Parameters:
key - String with an attribute key
Returns:
an int with the index

findNextAnnotation

public int findNextAnnotation(java.lang.String key)
This returns the index of the first character annotated with the given annotation key following the run containing the current character with respect to the given annotation key.

Specified by:
findNextAnnotation in interface AnnotatedString
Parameters:
key - a String with the annotation key
Returns:
an int with the index

toString

public java.lang.String toString(java.lang.String key)
This returns a string representation of the annotated string with the annotation for the given attribute key.

Specified by:
toString in interface AnnotatedString
Parameters:
key - String with an attribute key
Returns:
a String

toString

public java.lang.String toString()
This returns the surface string of the annotated string.

Specified by:
toString in interface AnnotatedString
Returns:
a String