View Javadoc

1   package com.atlassian.webdriver.waiter.webdriver;
2   
3   import com.atlassian.annotations.ExperimentalApi;
4   import com.atlassian.webdriver.AtlassianWebDriver;
5   import com.atlassian.webdriver.waiter.webdriver.function.ConditionFunction;
6   import com.atlassian.webdriver.waiter.Query;
7   import com.google.common.base.Function;
8   import org.openqa.selenium.WebDriver;
9   
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  /**
14   * <strong>WARNING</strong>: This API is still experimental and may be changed between versions.
15   * 
16   * @since 2.1
17   */
18  @ExperimentalApi
19  class WebDriverQueryBuilder
20  {
21      private final AtlassianWebDriver driver;
22      private final long timeout;
23      List<Query> queries = new ArrayList<Query>();
24  
25      public WebDriverQueryBuilder(AtlassianWebDriver driver, long timeout)
26      {
27          this.driver = driver;
28          this.timeout = timeout;
29      }
30  
31      public WebDriverQueryBuilder add(Query query) {
32          this.queries.add(query);
33          return this;
34      }
35  
36      public Function<WebDriver, Boolean> build() {
37  
38          ConditionFunction builtFunction = null;
39          for(int i = 0; i < queries.size(); i++)
40          {
41              Query query = queries.get(i);
42  
43              ConditionFunction func;
44              if (isAndQuery(query))
45              {
46                  i++; // We want to skip the next function as we already consumed it.
47                  ConditionFunction nextFunc = queries.get(i).build();
48                  func = new AndFunction(builtFunction, nextFunc);
49              }
50              else if (isOrQuery(query))
51              {
52                  i++; // We want to skip the next function as we already consumed it.
53                  ConditionFunction nextFunc = queries.get(i).build();
54                  func = new OrFunction(builtFunction, nextFunc);
55              }
56              else
57              {
58                   func = query.build();
59              }
60  
61              builtFunction = func;
62          }
63  
64          return builtFunction;
65      }
66  
67      public AtlassianWebDriver getDriver()
68      {
69          return driver;
70      }
71  
72      public long getTimeout()
73      {
74          return timeout;
75      }
76  
77      private boolean isAndQuery(Query query)
78      {
79          return query.getClass().isAssignableFrom(WebDriverWaiterQuery.AndQuery.class);
80      }
81  
82      private boolean isOrQuery(Query query)
83      {
84          return query.getClass().isAssignableFrom(WebDriverWaiterQuery.OrQuery.class);
85      }
86  
87      private static class AndFunction implements ConditionFunction
88      {
89          private final Function<WebDriver, Boolean> func1;
90          private final Function<WebDriver, Boolean> func2;
91  
92          public AndFunction(Function<WebDriver, Boolean> func1, Function<WebDriver, Boolean> func2)
93          {
94              this.func1 = func1;
95              this.func2 = func2;
96          }
97  
98          public Boolean apply(WebDriver driver)
99          {
100             return func1.apply(driver) && func2.apply(driver);
101         }
102     }
103 
104     private static class OrFunction implements ConditionFunction
105     {
106         private final Function<WebDriver, Boolean> func1;
107         private final Function<WebDriver, Boolean> func2;
108 
109         public OrFunction(Function<WebDriver, Boolean> func1, Function<WebDriver, Boolean> func2)
110         {
111             this.func1 = func1;
112             this.func2 = func2;
113         }
114 
115         public Boolean apply(WebDriver driver)
116         {
117             return func1.apply(driver) || func2.apply(driver);
118         }
119     }
120 }