View Javadoc

1   package com.atlassian.pageobjects.elements.mock.clock;
2   
3   import com.atlassian.pageobjects.elements.query.util.Clock;
4   import com.google.common.collect.ImmutableList;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import java.util.Arrays;
9   import java.util.List;
10  
11  /**
12   * A mock {@link com.atlassian.pageobjects.elements.query.util.Clock} that will return predefined dates and will
13   * throw exception, if called more times than the number of predefined values.
14   *
15   */
16  public class StrictMockClock implements Clock
17  {
18      private static final Logger log = LoggerFactory.getLogger(StrictMockClock.class);
19  
20      private final List<Long> times;
21      private int current = 0;
22  
23      public StrictMockClock(List<Long> times)
24      {
25          this.times = ImmutableList.copyOf(times);
26      }
27  
28      public StrictMockClock(Long... times)
29      {
30          this.times = Arrays.asList(times);
31      }
32  
33      public List<Long> times()
34      {
35          return ImmutableList.copyOf(times);
36      }
37  
38      public long first()
39      {
40          if (times.isEmpty())
41          {
42              return -1L;
43          }
44          return times.get(0);
45      }
46  
47      public long last()
48      {
49          if (times.isEmpty())
50          {
51              return -1L;
52          }
53          return times.get(times.size()-1);
54      }
55  
56      public long currentTime()
57      {
58          log.debug("#getCurrentDate: times=" + times + ",current=" + current);        
59          if (current >= times.size())
60          {
61              throw new IllegalStateException("Called too many times, only supports " + times.size() + " invocations");
62          }
63          return times.get(current++);
64      }
65  }