View Javadoc

1   package net.sf.tomp.djunit.simple;
2   
3   import net.sf.tomp.djunit.Record;
4   import net.sf.tomp.djunit.RecordIterator;
5   import net.sf.tomp.djunit.TestData;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   import java.util.Random;
10  
11  public class TestDataImpl implements TestData
12  {
13      protected List records = new ArrayList();
14      protected Random generator;
15      protected boolean locked = false;
16  
17      public void addRecord(Record r)
18      {
19          if (locked)
20          {
21              throw new IllegalStateException(
22                  "Cannot add record once iterator generated on " + this);
23          }
24  
25          records.add(r);
26      }
27  
28      protected Record getRecord(int i)
29      {
30          return (Record) records.get(i);
31      }
32  
33      public RecordIterator randomIterator(boolean repeating)
34      {
35          locked = true;
36          generator = new Random();
37  
38          if (repeating)
39          {
40              return new RandomRecordIteratorBase()
41                  {
42                      public boolean hasNext()
43                      {
44                          return recordCount() > 0;
45                      }
46  
47                      public Record nextRecord()
48                      {
49                          return getRecord(generator.nextInt(recordCount()));
50                      }
51                  };
52          }
53  
54          else
55          {
56              return new RandomRecordIteratorBase()
57                  {
58                      boolean[] used = new boolean[recordCount()];
59                      int remaining = recordCount();
60  
61                      public boolean hasNext()
62                      {
63                          return remaining > 0;
64                      }
65  
66                      public Record nextRecord()
67                      {
68                          int i = generator.nextInt(remaining);
69                          int j = 0;
70  
71                          while (i > 0)
72                          {
73                              while (used[j++])
74                              {
75                                  ;
76                              }
77  
78                              i--;
79                          }
80  
81                          used[j] = true;
82                          remaining--;
83  
84                          return getRecord(j);
85                      }
86                  };
87          }
88      }
89  
90      public RecordIterator iterator()
91      {
92          locked = true;
93  
94          return new RecordIteratorBase()
95              {
96                  public boolean hasNext()
97                  {
98                      return recordCount() > (this.index + 1);
99                  }
100 
101                 public Record nextRecord()
102                 {
103                     return getRecord(++this.index);
104                 }
105             };
106     }
107 
108     public int recordCount()
109     {
110         return records.size();
111     }
112 }