View Javadoc

1   package com.atlassian.pageobjects.elements.query.webdriver;
2   
3   import com.atlassian.pageobjects.elements.query.AbstractTimedQuery;
4   import com.atlassian.pageobjects.elements.query.ExpirationHandler;
5   import com.atlassian.pageobjects.elements.query.util.Clock;
6   import com.atlassian.pageobjects.elements.query.util.SystemClock;
7   import com.google.common.base.Supplier;
8   
9   import javax.annotation.concurrent.NotThreadSafe;
10  
11  import static com.google.common.base.Preconditions.checkNotNull;
12  
13  /**
14   * <p>
15   * Generic, WebDriver-based implementation of {@link com.atlassian.pageobjects.elements.query.TimedQuery}.
16   *
17   * <p>
18   * It accepts a supplier of the target value and uses it to retrieve current value od the query. The functions are
19   * supposed to throw {@link com.atlassian.pageobjects.elements.query.webdriver.GenericWebDriverTimedQuery.InvalidValue}
20   * to indicate that the current value should not be accepted as valid and returned by the query.
21   *
22   */
23  @NotThreadSafe
24  public class GenericWebDriverTimedQuery<T> extends AbstractTimedQuery<T>
25  {
26      protected final Supplier<T> valueSupplier;
27      private boolean invalidValue;
28  
29      public GenericWebDriverTimedQuery(Supplier<T> supplier, Clock clock, long defTimeout,
30              long interval, ExpirationHandler eh)
31      {
32          super(clock, defTimeout, interval, eh);
33          this.valueSupplier = checkNotNull(supplier);
34      }
35  
36      public GenericWebDriverTimedQuery(Supplier<T> supplier, long defTimeout, long interval,
37              ExpirationHandler eh)
38      {
39          this(supplier, SystemClock.INSTANCE, defTimeout, interval, eh);
40      }
41  
42      public GenericWebDriverTimedQuery(Supplier<T> supplier, long defTimeout, long interval)
43      {
44          this(supplier, SystemClock.INSTANCE, defTimeout, interval, ExpirationHandler.RETURN_CURRENT);
45      }
46  
47      public GenericWebDriverTimedQuery(Supplier<T> supplier, long defTimeout)
48      {
49          this(supplier, SystemClock.INSTANCE, defTimeout, DEFAULT_INTERVAL, ExpirationHandler.RETURN_CURRENT);
50      }
51  
52      public GenericWebDriverTimedQuery(GenericWebDriverTimedQuery<T> origin, long timeout)
53      {
54          this(origin.valueSupplier, origin.clock(), timeout, origin.interval(), origin.expirationHandler());
55      }
56  
57      @Override
58      protected final boolean shouldReturn(final T currentEval)
59      {
60          return !invalidValue;
61      }
62  
63      @Override
64      protected final T currentValue()
65      {
66          invalidValue = false;
67          try
68          {
69              return valueSupplier.get();
70          }
71          catch (InvalidValue e)
72          {
73              invalidValue = true;
74              return (T)e.value;
75          }
76      }
77  
78      public static final class InvalidValue extends RuntimeException
79      {
80          private static final long serialVersionUID = -4972134972343443297L;
81  
82          private final Object value;
83  
84          public InvalidValue(final Object value)
85          {
86              this.value = value;
87          }
88  
89          @Override
90          public Throwable fillInStackTrace()
91          {
92              return this;
93          }
94          
95      }
96  }