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

java.lang.Object
  |
  +--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,v 1.8 2002/02/08 14:22:21 brianstoler Exp $
Author:
JavaPLT

Field Summary
(package private)  TokenList.Iterator _cursor
          keeps track of cursor position in document
(package private)  TokenList _tokens
          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 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.
(package private)  int absOffset()
          Package private absolute offset for tests.
protected  ReducedToken current()
          Get the ReducedToken currently pointed at by the cursor.
(package private)  int getBlockOffset()
          Get the offset into the current ReducedToken.
protected  ReducedModelState getStateAtCurrent()
          Wrapper for TokenList.Iterator.getStateAtCurrent that returns the current state for some iterator.
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  void next()
          Move to the token immediately right.
protected  void prev()
          Move to the token immediately left.
protected abstract  void resetLocation()
          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.
protected abstract  ReducedModelState stateAtRelLocation(int relLocation)
          Returns the state at the relLocation, where relLocation is the location relative to the walker
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

PTR_CHAR

public static final char PTR_CHAR
The character that represents the cursor in toString().
See Also:
Object.toString()

_tokens

TokenList _tokens
A list of ReducedTokens (braces and gaps).
See Also:
ModelList

_cursor

TokenList.Iterator _cursor
keeps track of cursor position in document
See Also:
ModelList.Iterator
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

int absOffset()
Package private absolute offset for tests. We don't keep track of absolute offset as it causes too much confusion and trouble.

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. Be sure to dispose of the result of this method after you are finished with it, or there will be memory leaks as long as this ReducedModel is not garbage collected.

getStateAtCurrent

protected ReducedModelState getStateAtCurrent()
Wrapper for TokenList.Iterator.getStateAtCurrent that returns the current state for some iterator. Convenience method to return the current state in the cursor iterator.

_gapToRight

protected boolean _gapToRight()
Determines 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

stateAtRelLocation

protected abstract ReducedModelState stateAtRelLocation(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.

resetLocation

protected abstract void resetLocation()
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 TokenList iterator. 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.