lapisx.util
Class History

java.lang.Object
  extended bylapisx.util.History
Direct Known Subclasses:
HistoryComboBoxModel

public class History
extends Object

A History is a list of objects with a current object. One application of History is a browser history, in which the objects are web pages and one page is currently displayed.

A History has a finite maximum size. When adding an object would exceed the maximum size, the oldest object is removed. The maximum size can be increased with expand().


Field Summary
protected  int curr
          Index of current object.
protected  int end
          Index after newest object in history.
protected  Object[] history
          Array of history objects, arranged as circular queue.
protected  int start
          Index of oldest object in history.
 
Constructor Summary
History(History h)
          Make a duplicate of another History.
History(int max)
          Make a History.
 
Method Summary
 void add(Object obj)
          Add an object to the end of the history, moving the current point to it.
 Object back()
          Move back one object in the history, if possible.
 Enumeration backElements()
          Get the objects BEFORE the current object.
 boolean canBack()
          Test whether back() will succeed.
 boolean canForward()
          Test whether forward() will succeed.
 void clear()
          Clear the history.
 boolean contains(Object obj)
          Test whether history already contains an object.
 Enumeration elements()
          Get the objects in the history.
 void expand()
          Double the capacity of the history.
protected  void fireAdded(int i, int j)
          Called when new elements are added at history[i..j].
protected  void fireChanged(int i, int j)
          Called when elements at history[i..j] are replaced.
protected  void fireRemoved(int i, int j)
          Called when history[i..j] is removed.
 Object forward()
          Move forward one object in the history, if possible.
 Enumeration forwardElements()
          Get the objects AFTER the current object.
 Object get()
          Get the current object of the history.
 boolean isEmpty()
          Test whether history is empty.
 boolean isFull()
          Test whether history is full.
 void jumpTo(Object obj)
          Set the current object of the history.
 Object peekBack()
          Get the object that would be returned by back(), without actually changing the current object.
 Object peekForward()
          Get the object that would be returned by forward(), without actually changing the current object.
 void put(Object obj)
          Add an object to the history after the current point (deleting all objects forward of this point).
 void replace(Object obj)
          Replace the current object of the history.
 int size()
          Get number of objects currently in history.
 Object toEnd()
          Move to last (newest) object in the history, if possible.
 Object toStart()
          Move to first (oldest) object in the history, if possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

history

protected Object[] history
Array of history objects, arranged as circular queue.


start

protected int start
Index of oldest object in history.


end

protected int end
Index after newest object in history.


curr

protected int curr
Index of current object. If history is empty, then curr == start == end.

Constructor Detail

History

public History(int max)
Make a History.

Parameters:
max - Maximum length of history

History

public History(History h)
Make a duplicate of another History.

Parameters:
h - History to copy
Method Detail

clear

public void clear()
Clear the history.


expand

public void expand()
Double the capacity of the history.


put

public void put(Object obj)
Add an object to the history after the current point (deleting all objects forward of this point). If history overflows, the oldest object is thrown away.

Parameters:
obj - Object to add to history

add

public void add(Object obj)
Add an object to the end of the history, moving the current point to it. If history overflows, the oldest object is thrown away.

Parameters:
obj - Object to add to history

get

public Object get()
Get the current object of the history.

Returns:
current object of history, or null if history is empty.

jumpTo

public void jumpTo(Object obj)
Set the current object of the history.

Parameters:
obj - Object that should be the new current object.
Throws:
NoSuchElementException - if this doesn't contain obj.

peekBack

public Object peekBack()
Get the object that would be returned by back(), without actually changing the current object.

Returns:
object before current object, or null if at beginning of history or history is empty.

peekForward

public Object peekForward()
Get the object that would be returned by forward(), without actually changing the current object.

Returns:
object after current object, or null if at end of history or history is empty.

replace

public void replace(Object obj)
Replace the current object of the history. The rest of the history is unaffected, and the current pointer stays where it is.

If the history is empty, then this call is equivalent to put(obj).

Parameters:
obj - object to substitute

back

public Object back()
Move back one object in the history, if possible.

Returns:
previous object in the history, or null if at start.

forward

public Object forward()
Move forward one object in the history, if possible.

Returns:
next object in the history, or null if at end of history.

toStart

public Object toStart()
Move to first (oldest) object in the history, if possible.

Returns:
first object in the history, or null if history empty.

toEnd

public Object toEnd()
Move to last (newest) object in the history, if possible.

Returns:
last object in the history, or null if history empty.

canBack

public boolean canBack()
Test whether back() will succeed.

Returns:
true if and only if there are objects before the current object

canForward

public boolean canForward()
Test whether forward() will succeed.

Returns:
true if and only if there are objects after the current object

size

public int size()
Get number of objects currently in history.

Returns:
number of objects that would be returned by elements().

isEmpty

public boolean isEmpty()
Test whether history is empty.

Returns:
true if and only if history contains no objects

isFull

public boolean isFull()
Test whether history is full.

Returns:
true if and only if history contains max objects

contains

public boolean contains(Object obj)
Test whether history already contains an object.

Parameters:
obj - Object to search for
Returns:
true if and only if history contains an object that equals() obj

elements

public Enumeration elements()
Get the objects in the history. enumeration yielding the history objects in order from oldest to newest.


forwardElements

public Enumeration forwardElements()
Get the objects AFTER the current object. enumeration yielding the history objects after current, in order from oldest to newest.


backElements

public Enumeration backElements()
Get the objects BEFORE the current object. enumeration yielding the history objects before current, in order from oldest to newest.


fireRemoved

protected void fireRemoved(int i,
                           int j)
Called when history[i..j] is removed. Used by subclasses that need to generate events. Default implementation does nothing.

Parameters:
i - index of first removed element
j - index of last removed element

fireAdded

protected void fireAdded(int i,
                         int j)
Called when new elements are added at history[i..j]. Used by subclasses that need to generate events. Default implementation does nothing.

Parameters:
i - index of first added element
j - index of last added element

fireChanged

protected void fireChanged(int i,
                           int j)
Called when elements at history[i..j] are replaced. Also called when history[i] becomes the current element (in which case i == j). Used by subclasses that need to generate events. Default implementation does nothing.

Parameters:
i - index of first changed element
j - index of last changed element