View Javadoc

1   package org.e2etrace.trace;
2   
3   /*
4    * Copyright 2006 Gunther Popp
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.e2etrace.timer.ITimer;
20  import org.e2etrace.timer.ITimerFactory;
21  
22  /**
23   * Factory for Mock-Timers.
24   * <p>
25   *
26   * The factory takes a list of predefined durations as parameter in the
27   * constructor. Each call to <code>newInstance</code> selects the next entry
28   * in the list and creates a {@link org.e2etrace.trace.MockTimer} that returns the
29   * resp. duration. When the last entry is reached, <code>newInstance</code>
30   * starts again with the first entry.
31   * <p>
32   *
33   * @author Gunther Popp
34   */
35  public class MockTimerFactory implements ITimerFactory {
36  
37    private long[] testDurations;
38    private int index = -1;
39  
40    /**
41     * Constructor
42     *
43     * @param testDurations forwarded to {@link MockTimer}
44     */
45    public MockTimerFactory(long[] testDurations) {
46      this.testDurations = testDurations;
47    }
48  
49    /** {@inheritDoc} */
50    public ITimer newInstance() {
51      index++;
52  
53      if (index >= testDurations.length) {
54        index = 0;
55      }
56  
57      return new MockTimer(this.testDurations[index]);
58    }
59  
60  }