Class N08_Part2_DataStructureAccess.TaskAbstract

java.lang.Object
  extended by N08_Part2_DataStructureAccess.TaskAbstract
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
N08_Part2_DataStructureAccess.ArrayAccess, N08_Part2_DataStructureAccess.ListAccess, N08_Part2_DataStructureAccess.MapAccess, N08_Part2_DataStructureAccess.ThreadLocalAccess
Enclosing class:
N08_Part2_DataStructureAccess

protected abstract static class N08_Part2_DataStructureAccess.TaskAbstract
extends Object
implements Runnable

Abstract base class for Tasks which measure the access time of "array-like" data structures: arrays, array-like Lists (e.g. ArrayList and Vector), and associative arrays (i.e. Maps).

Subclasses must obey all the rules below.

Rule #1: a dedicated subclass must written for each type of data structure.

Rule #2: each subclass must implement its run method like:


                public void run() {
                        for (int i = 0; i < size; i++) {
                                index = [the value of the element at the current value of index in the data structure];
                        }
                }
 

Rule #3: all subclass data structures must be isomorphic to the result of makeIntegers, namely, their (index/key, value) pairs must be identical to makeIntegers. This requirement is automatically satisfied by N08_Part2_DataStructureAccess.ArrayAccess and its subclasses. Those subclasses which are Lists should clear themselves and then add all the elements of makeIntegers. Those subclasses which are Maps should clear themselves and then in a loop over the elements of makeIntegers do map.put( integer, integer );.

The reason for these rules is that the values of makeIntegers are cleverly choosen so that the loop in run accesses the data structure's elements in a specific pattern (either sequential or random).

To see how this works for the sequential case, suppose that the data structure is the result of makeIntegers, so that the code is


                public void run() {
                        for (int i = 0; i < integers.length; i++) {
                                index = integers[index.intValue()];
                        }
                }
 
(This is how N08_Part2_DataStructureAccess.ArrayAccess is implemented.) If all (but the last) elements of integers satisfy integers[i] equals i + 1, then the loop in run accesses the elements of integers sequentially (assuming that index is initialized to 0, and that the last element of integers also is 0).

The other functionality defined in this base class is DCE prevention, given that subclasses will be Runnable task classes.


Field Summary
protected  Integer index
          Index/key of the next element of the data structure that will be accessed.
protected  N08_Part2_DataStructureAccess.Mode mode
          Access pattern mode of the data structure.
protected  int size
          Size of the data structure.
 
Constructor Summary
protected N08_Part2_DataStructureAccess.TaskAbstract(int size, N08_Part2_DataStructureAccess.Mode mode)
          Constructor.
 
Method Summary
protected  void checkIntegers(Integer[] integers, Integer[] indices)
          Checks that integers is suitable for use in the run method described in the class javadocs (i.e. every element is accessed in an iteration identical to the run loop in the order specified by indices).
protected  Integer[] makeIndices()
          Returns an Integer[] that specifies the sequence of indices of the relevant data structure that will be accessed by the loop in {kink #run run}.
protected  Integer[] makeIntegers()
          Returns an Integer[] that always contains the numbers [0, size - 1].
protected  void postBenchmarkCheck()
           
 String toString()
          Returns the String representation of index to prevent DCE.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Field Detail

mode

protected final N08_Part2_DataStructureAccess.Mode mode
Access pattern mode of the data structure.


size

protected final int size
Size of the data structure.


index

protected Integer index
Index/key of the next element of the data structure that will be accessed.

Additionally, since the instances that will be benchmarked are Runnables, this field stores the value of some computation done in run to prevent DCE.

Constructor Detail

N08_Part2_DataStructureAccess.TaskAbstract

protected N08_Part2_DataStructureAccess.TaskAbstract(int size,
                                                     N08_Part2_DataStructureAccess.Mode mode)
                                              throws IllegalArgumentException
Constructor.

Parameters:
size - specifies the number of elements in the data structure
mode - specifies the data structure access pattern:
Throws:
IllegalArgumentException - if size < 1; mode is null
Method Detail

makeIntegers

protected Integer[] makeIntegers()
Returns an Integer[] that always contains the numbers [0, size - 1]. The order of the result's elements are chosen so that if it is used in a run loop as described in this class's javadocs, then its elements will be accessed in the order specified by mode.

Side effect: assigns index to the 0th element of the result.


makeIndices

protected Integer[] makeIndices()
Returns an Integer[] that specifies the sequence of indices of the relevant data structure that will be accessed by the loop in {kink #run run}. For example, if the result equals {2, 1, 3, 0}, then means that access element 2, then element 1, then element 3, then element 0 IN THAT ORDER.


checkIntegers

protected void checkIntegers(Integer[] integers,
                             Integer[] indices)
                      throws IllegalStateException
Checks that integers is suitable for use in the run method described in the class javadocs (i.e. every element is accessed in an iteration identical to the run loop in the order specified by indices).

Throws:
IllegalStateException

toString

public String toString()
Returns the String representation of index to prevent DCE.

Overrides:
toString in class Object

postBenchmarkCheck

protected void postBenchmarkCheck()
                           throws IllegalStateException
Throws:
IllegalStateException