com.topologi.diffx.algorithm
Class ElementState

java.lang.Object
  extended by com.topologi.diffx.algorithm.ElementState

public final class ElementState
extends Object

Maintains the state of open and closed elements during the processing the Diff-X algorithm.

This class has two purposes, firstly to provide an object that is more specialised than the generic lists and stack for use by the DiffX algorithms. Second, to delegate some of the complexity of algorithm.

This class has several methods that are similar to List interface but does not implement it.

This class is not synchronised and is not meant to be serializable.

Version:
12 May 2005
Author:
Christophe Lauret

Constructor Summary
ElementState()
          Constructs an empty stack with an initial capacity of 12.
ElementState(int initialCapacity)
          Constructs an stack of elements with the specified initial capacity.
 
Method Summary
 void clear()
          Removes all of the elements from this list.
 boolean contains(OpenElementEvent element)
          Returns true if this list contains the specified element.
 OpenElementEvent current()
          Returns the current open element.
 char currentChange()
          Returns the change of the current open element.
 void delete(DiffXEvent e)
          Updates the state from the deleted event.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this class instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
 void format(DiffXEvent e)
          Updates the state from the formatted event.
 OpenElementEvent get(int index)
          Returns the open element at the specified position in this list.
 boolean hasPriorityOver(DiffXEvent e1, DiffXEvent e2)
          Indicates whether the first specified event has priority over the second element.
 int indexOf(OpenElementEvent element)
          Searches for the first occurrence of the given argument, testing for equality using the equals method.
 void insert(DiffXEvent e)
          Updates the state from the inserted event.
 boolean isEmpty()
          Tests if this list has no elements.
 int lastIndexOf(OpenElementEvent element)
          Returns the index of the last occurrence of the specified object in this list.
 boolean matchCurrent(DiffXEvent e)
          Indicates whether the specified event is a close element that matches the name and URI of the current open element.
 boolean okDelete(DiffXEvent e)
          Indicates whether the specified event is a close element that matches the name and URI of the current open element.
 boolean okFormat(DiffXEvent e)
          Indicates whether the specified event is a close element that matches the name and URI of the current open element.
 boolean okInsert(DiffXEvent e)
          Indicates whether the specified event is a close element that matches the name and URI of the current open element.
 OpenElementEvent pop()
          Removes the last element from the top of the stack.
 OpenElementEvent remove(int index)
          Removes the element at the specified position in this list.
 int size()
          Returns the number of elements in this stack.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ElementState

public ElementState(int initialCapacity)
             throws IllegalArgumentException
Constructs an stack of elements with the specified initial capacity.

Parameters:
initialCapacity - The initial capacity of the list.
Throws:
IllegalArgumentException - if the specified initial capacity is negative.

ElementState

public ElementState()
Constructs an empty stack with an initial capacity of 12.

Method Detail

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this class instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity - The desired minimum capacity.

size

public int size()
Returns the number of elements in this stack.

Returns:
the number of elements in this stack.

isEmpty

public boolean isEmpty()
Tests if this list has no elements.

Returns:
true if this list has no elements; false otherwise.

contains

public boolean contains(OpenElementEvent element)
Returns true if this list contains the specified element.

Parameters:
element - Element whose presence is to be tested.
Returns:
true if the specified element is present; false otherwise.

indexOf

public int indexOf(OpenElementEvent element)
Searches for the first occurrence of the given argument, testing for equality using the equals method.

Parameters:
element - The open elemnt to find.
Returns:
The index of the first occurrence of the argument in this list; returns -1See Also:
DiffXEvent.equals(DiffXEvent)

lastIndexOf

public int lastIndexOf(OpenElementEvent element)
Returns the index of the last occurrence of the specified object in this list.

Parameters:
element - The desired element.
Returns:
The index of the last occurrence of the specified open element; or -1 if not found.

current

public OpenElementEvent current()
Returns the current open element.

Returns:
The current open element; or null if none.

currentChange

public char currentChange()
Returns the change of the current open element.

Returns:
The change of the current open element; or ' ' if none.

matchCurrent

public boolean matchCurrent(DiffXEvent e)
Indicates whether the specified event is a close element that matches the name and URI of the current open element.

Parameters:
e - The event to check.
Returns:
true if it matches the current element; false otherwise.

insert

public void insert(DiffXEvent e)
Updates the state from the inserted event.

Parameters:
e - The inserted event.

format

public void format(DiffXEvent e)
Updates the state from the formatted event.

Parameters:
e - The formatted event.

delete

public void delete(DiffXEvent e)
Updates the state from the deleted event.

Parameters:
e - The deleted event.

okFormat

public boolean okFormat(DiffXEvent e)
Indicates whether the specified event is a close element that matches the name and URI of the current open element.

Parameters:
e - The event to check.
Returns:
true if it matches the current element; false otherwise.

okInsert

public boolean okInsert(DiffXEvent e)
Indicates whether the specified event is a close element that matches the name and URI of the current open element.

Parameters:
e - The event to check.
Returns:
true if it matches the current element; false otherwise.

okDelete

public boolean okDelete(DiffXEvent e)
Indicates whether the specified event is a close element that matches the name and URI of the current open element.

Parameters:
e - The event to check.
Returns:
true if it matches the current element; false otherwise.

hasPriorityOver

public boolean hasPriorityOver(DiffXEvent e1,
                               DiffXEvent e2)
Indicates whether the first specified event has priority over the second element. It only seem to be the case when the algorithm has the choice between an attribute and another element.

Parameters:
e1 - The element assumed to have priority.
e2 - The other element.
Returns:
true if first specified event has priority over the second element; false otherwise.

pop

public OpenElementEvent pop()
Removes the last element from the top of the stack.

Returns:
The last element from the top of the stack.

get

public OpenElementEvent get(int index)
                     throws IndexOutOfBoundsException
Returns the open element at the specified position in this list.

Parameters:
index - index of element to return.
Returns:
The element at the specified position in this list.
Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).

remove

public OpenElementEvent remove(int index)
                        throws IndexOutOfBoundsException
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Parameters:
index - The index of the element to removed.
Returns:
The element that was removed from the list.
Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.