View Javadoc

1   package com.atlassian.pageobjects.elements.query;
2   
3   import com.atlassian.pageobjects.elements.timeout.TimeoutType;
4   import com.atlassian.pageobjects.elements.timeout.Timeouts;
5   import com.google.common.base.Supplier;
6   
7   import static com.google.common.base.Preconditions.checkNotNull;
8   
9   /**
10   * Utilities to create and manipulate timed queries
11   *
12   * @since 2.1
13   */
14  public final class Queries
15  {
16  
17      private Queries()
18      {
19          throw new AssertionError("Don't instantiate me");
20      }
21  
22      /**
23       * Returns a timed query, with current evaluation is based on a value provided by given <tt>supplier</tt>. The supplier
24       * will be periodically called to compute the value of the query.
25       *
26       * @param timeouts an instance of timeouts to use for configured the new condition
27       * @param supplier supplier of the query evaluation
28       * @return new query based on supplier
29       */
30      public static <T> TimedQuery<T> forSupplier(Timeouts timeouts, final Supplier<T> supplier)
31      {
32          return forSupplier(timeouts, supplier, TimeoutType.DEFAULT);
33      }
34  
35      /**
36       * <p/>
37       * Returns a timed query, with current evaluation is based on a value provided by given <tt>supplier</tt>. The supplier
38       * will be periodically called to compute the value of the query.
39       *
40       * <p/>
41       * The resulting query's default timeout will be as specified by <tt>timeoutType</tt>.
42       *
43       * @param timeouts an instance of timeouts to use for configured the new condition
44       * @param supplier supplier of the query evaluation
45       * @param timeoutType timeout type for the resulting query
46       * @return new query based on supplier
47       */
48      public static <T> TimedQuery<T> forSupplier(Timeouts timeouts, final Supplier<T> supplier, TimeoutType timeoutType)
49      {
50          checkNotNull("timeouts", timeouts);
51          checkNotNull("timeoutType", timeoutType);
52          checkNotNull("supplier", supplier);
53          return new AbstractTimedQuery<T>(timeouts.timeoutFor(timeoutType),
54                  timeouts.timeoutFor(TimeoutType.EVALUATION_INTERVAL), ExpirationHandler.RETURN_CURRENT) {
55              @Override
56              protected T currentValue() {
57                  return supplier.get();
58              }
59  
60              @Override
61              protected boolean shouldReturn(T currentEval)
62              {
63                  return true;
64              }
65          };
66      }
67  }