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