edu.rice.cs.drjava.model.definitions.reducedmodel
Class AbstractReducedModel

java.lang.Object
  extended by edu.rice.cs.drjava.model.definitions.reducedmodel.AbstractReducedModel
All Implemented Interfaces:
ReducedModelStates
Direct Known Subclasses:
ReducedModelBrace, ReducedModelComment

public abstract class AbstractReducedModel
extends Object
implements ReducedModelStates

A refactoring of the common code between ReducedModelComment and ReducedModelBrace. Both of the refactored classes extend this class.

Version:
$Id: AbstractReducedModel.java 5175 2010-01-20 08:46:32Z mgricken $
Author:
JavaPLT

Field Summary
(package private)  TokenList.Iterator _cursor
          Keeps track of cursor position in document.
(package private)  TokenList _tokens
          The reduced model for a document is a list of ReducedTokens (braces and gaps).
static char PTR_CHAR
          The character that represents the cursor in toString().
 
Fields inherited from interface edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelStates
FREE, INSIDE_BLOCK_COMMENT, INSIDE_DOUBLE_QUOTE, INSIDE_LINE_COMMENT, INSIDE_SINGLE_QUOTE, STUTTER
 
Constructor Summary
AbstractReducedModel()
          Constructor.
 
Method Summary
protected  void _augmentCurrentGap(int length)
          Assuming there is a gap to the right, this function increases the size of that gap.
protected  void _augmentGapToLeft(int length)
          Assuming there is a gap to the left, this function increases the size of that gap.
protected  boolean _gapToLeft()
          Determines if there is a gap immediately to the left of the cursor.
protected  boolean _gapToRight()
          Determines if there is a Gap immediately to the right of the cursor.
 void _insertGap(int length)
          Inserts a block of text into the reduced model which has no special consideration in the reduced model.
protected  void _insertNewGap(int length)
          Helper function for _insertGap.
 int absOffset()
          Absolute offset for testing purposes.
 int absOffset(TokenList.Iterator cursor)
          Absolute offset of the specified iterator.
protected  ReducedToken current()
          Get the ReducedToken currently pointed at by the cursor.
(package private)  int getBlockOffset()
          Get the offset into the current ReducedToken.
 int getLength()
           
 ReducedModelState getState()
           
abstract  void insertChar(char ch)
          Inserts a character into the reduced model.
protected abstract  void insertGapBetweenMultiCharBrace(int length)
          Inserts a gap between a multiple character brace.
 TokenList.Iterator makeCopyCursor()
          Make a copy of the token list's iterator.
protected abstract  ReducedModelState moveWalkerGetState(int relLocation)
          Returns the state at the relLocation, where relLocation is the location relative to the walker.
protected  void next()
          Move to the token immediately right.
protected  void prev()
          Move to the token immediately left.
protected abstract  void resetWalkerLocationToCursor()
          Resets the walker to the current position in document.
(package private)  void setBlockOffset(int offset)
          Change the offset into the current ReducedToken.
 String simpleString()
          A toString() replacement for testing - easier to read.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PTR_CHAR

public static final char PTR_CHAR
The character that represents the cursor in toString(). @see #toString()

See Also:
Constant Field Values

_tokens

TokenList _tokens
The reduced model for a document is a list of ReducedTokens (braces and gaps).


_cursor

TokenList.Iterator _cursor
Keeps track of cursor position in document.

Constructor Detail

AbstractReducedModel

public AbstractReducedModel()
Constructor. Creates a new reduced model with the cursor at the start of a blank "page."

Method Detail

getBlockOffset

int getBlockOffset()
Get the offset into the current ReducedToken.

Returns:
the number of characters into the token where the cursor sits

setBlockOffset

void setBlockOffset(int offset)
Change the offset into the current ReducedToken.

Parameters:
offset - the number of characters into the token to set the cursor

absOffset

public int absOffset()
Absolute offset for testing purposes. We don't keep track of absolute offset as it causes too much confusion and trouble.


absOffset

public int absOffset(TokenList.Iterator cursor)
Absolute offset of the specified iterator. Inefficient so only used for testing purposes.


getLength

public int getLength()

getState

public ReducedModelState getState()

simpleString

public String simpleString()
A toString() replacement for testing - easier to read.


insertChar

public abstract void insertChar(char ch)
Inserts a character into the reduced model. A method to be implemented in each specific reduced sub-model.


_insertGap

public void _insertGap(int length)
Inserts a block of text into the reduced model which has no special consideration in the reduced model.
  1. atStart: if gap to right, augment first gap, else insert
  2. atEnd: if gap to left, augment left gap, else insert
  3. inside a gap: grow current gap, move offset by length
  4. inside a multiple character brace:
    1. break current brace
    2. insert new gap
  5. gap to left: grow that gap and set offset to zero
  6. gap to right: this case handled by inside gap (offset invariant)
  7. between two braces: insert new gap

    Parameters:
    length - the length of the inserted text

insertGapBetweenMultiCharBrace

protected abstract void insertGapBetweenMultiCharBrace(int length)
Inserts a gap between a multiple character brace. Because ReducedModelBrace does not keep track of multiple character braces, only (),{}, and [], it differed in its implementation of inserGap(int) from ReducedModelComment's. To pull out the otherwise identical code and place it here, we created this function to do something meaningful in ReducedModelComment and to throw an exception in ReducedModelBrace.


makeCopyCursor

public TokenList.Iterator makeCopyCursor()
Make a copy of the token list's iterator.


_gapToRight

protected boolean _gapToRight()
Determines if there is a Gap immediately to the right of the cursor.


_gapToLeft

protected boolean _gapToLeft()
Determines if there is a gap immediately to the left of the cursor.


_augmentGapToLeft

protected void _augmentGapToLeft(int length)
Assuming there is a gap to the left, this function increases the size of that gap.

Parameters:
length - the amount of increase

_augmentCurrentGap

protected void _augmentCurrentGap(int length)
Assuming there is a gap to the right, this function increases the size of that gap.

Parameters:
length - the amount of increase

_insertNewGap

protected void _insertNewGap(int length)
Helper function for _insertGap. Performs the actual insert and marks the offset appropriately.

Parameters:
length - size of gap to insert

moveWalkerGetState

protected abstract ReducedModelState moveWalkerGetState(int relLocation)
Returns the state at the relLocation, where relLocation is the location relative to the walker.

Parameters:
relLocation - distance from walker to get state at.

resetWalkerLocationToCursor

protected abstract void resetWalkerLocationToCursor()
Resets the walker to the current position in document.


current

protected ReducedToken current()
Get the ReducedToken currently pointed at by the cursor.

Returns:
the current token

next

protected void next()
Move to the token immediately right. This function forwards its responsibilities to the cursor. If the cursor is at the end, it will throw an exception.


prev

protected void prev()
Move to the token immediately left. This function forwards its responsibilities to the TokenList iterator. If the cursor is at the start, it will throw an exception.