View Javadoc

1   package com.atlassian.pageobjects.elements.mock.clock;
2   
3   import com.atlassian.pageobjects.elements.query.util.Clock;
4   
5   import javax.annotation.concurrent.NotThreadSafe;
6   
7   import static com.google.common.base.Preconditions.checkArgument;
8   
9   /**
10   * A {@link com.atlassian.pageobjects.elements.query.util.Clock} implementation that periodically increments the returned value.
11   *
12   */
13  @NotThreadSafe
14  public class PeriodicClock implements Clock
15  {
16      private final long increment;
17      private final int periodLength;
18  
19      private long current;
20      private int currentInPeriod = 0;
21  
22      public PeriodicClock(long intitialValue, long increment, int periodLength)
23      {
24          checkArgument(increment > 0, "increment should be > 0");
25          checkArgument(periodLength > 0, "periodLength should be > 0");
26          this.current = intitialValue;
27          this.increment = increment;
28          this.periodLength = periodLength;
29      }
30  
31      public PeriodicClock(long increment, int periodLength)
32      {
33          this(0, increment, periodLength);
34      }
35  
36      public PeriodicClock(long increment)
37      {
38          this(0, increment, 1);
39      }
40  
41  
42      public long currentTime()
43      {
44          long answer = current;
45          if (periodFinished())
46          {
47              current += increment;
48          }
49          return answer;
50      }
51  
52      private boolean periodFinished()
53      {
54          currentInPeriod++;
55          if (currentInPeriod == periodLength)
56          {
57              currentInPeriod = 0;
58              return true;
59          }
60          return false;
61      }
62  }