org.apache.xpath.axes
Class NodeSequence.IteratorCache

java.lang.Object
  extended by org.apache.xpath.axes.NodeSequence.IteratorCache
Enclosing class:
NodeSequence

private static final class NodeSequence.IteratorCache
extends java.lang.Object

Each NodeSequence object has an iterator which is "walked". As an iterator is walked one obtains nodes from it. As those nodes are obtained they may be cached, making the next walking of a copy or clone of the iterator faster. This field (m_cache) is a reference to such a cache, which is populated as the iterator is walked.

Note that multiple NodeSequence objects may hold a reference to the same cache, and also (and this is important) the same iterator. The iterator and its cache may be shared among many NodeSequence objects.

If one of the NodeSequence objects walks ahead of the others it fills in the cache. As the others NodeSequence objects catch up they get their values from the cache rather than the iterator itself, so the iterator is only ever walked once and everyone benefits from the cache.

At some point the cache may be complete due to walking to the end of one of the copies of the iterator, and the cache is then marked as "complete". and the cache will have no more nodes added to it.

Its use-count is the number of NodeSequence objects that use it.


Field Summary
private  boolean m_isComplete2
          true if the associated iterator is exhausted and all nodes obtained from it are in the cache.
private  int m_useCount2
           
private  NodeVector m_vec2
          A list of nodes already obtained from the iterator.
 
Constructor Summary
NodeSequence.IteratorCache()
           
 
Method Summary
private  NodeVector getVector()
          Get the cached list of nodes obtained from the iterator so far.
private  void increaseUseCount()
          This method is called when yet another NodeSequence object uses, or shares this same cache.
private  boolean isComplete()
          Returns true if no cache is complete and immutable.
private  void setCacheComplete(boolean b)
          Call this method with 'true' if the iterator is exhausted and the cached list is complete, or no longer growing.
private  void setVector(NodeVector nv)
          Sets the NodeVector that holds the growing list of nodes as they are appended to the cached list.
private  int useCount()
          Returns count of how many NodeSequence objects share this IteratorCache object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_vec2

private NodeVector m_vec2
A list of nodes already obtained from the iterator. As the iterator is walked the nodes obtained from it are appended to this list.

Both an iterator and its corresponding cache can be shared by multiple NodeSequence objects.

For example, consider three NodeSequence objects ns1, ns2 and ns3 doing such sharing, and the nodes to be obtaind from the iterator being the sequence { 33, 11, 44, 22, 55 }.

If ns3.nextNode() is called 3 times the the underlying iterator will have walked through 33, 11, 55 and these three nodes will have been put in the cache.

If ns2.nextNode() is called 2 times it will return 33 and 11 from the cache, leaving the iterator alone.

If ns1.nextNode() is called 6 times it will return 33 and 11 from the cache, then get 44, 22, 55 from the iterator, and appending 44, 22, 55 to the cache. On the sixth call it is found that the iterator is exhausted and the cache is marked complete.

Should ns2 or ns3 have nextNode() called they will know that the cache is complete, and they will obtain all subsequent nodes from the cache.

Note that the underlying iterator, though shared is only ever walked once.


m_isComplete2

private boolean m_isComplete2
true if the associated iterator is exhausted and all nodes obtained from it are in the cache.


m_useCount2

private int m_useCount2
Constructor Detail

NodeSequence.IteratorCache

NodeSequence.IteratorCache()
Method Detail

useCount

private int useCount()
Returns count of how many NodeSequence objects share this IteratorCache object.


increaseUseCount

private void increaseUseCount()
This method is called when yet another NodeSequence object uses, or shares this same cache.


setVector

private void setVector(NodeVector nv)
Sets the NodeVector that holds the growing list of nodes as they are appended to the cached list.


getVector

private NodeVector getVector()
Get the cached list of nodes obtained from the iterator so far.


setCacheComplete

private void setCacheComplete(boolean b)
Call this method with 'true' if the iterator is exhausted and the cached list is complete, or no longer growing.


isComplete

private boolean isComplete()
Returns true if no cache is complete and immutable.