View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.service.locator.element;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  /**
23   * {@link GUIElementLocator} version which wraps a set of alternative GUIElementLocators. To retrieve a
24   * <code>GUIElementLocator</code> compatible object, invoke <code>newMutableInstance()</code>. You can (and should) retrieve
25   * multiple mutable instances from the same <code>ElementLocators</code> object, as every instance will be modified by the
26   * AludraTest framework when the actual element to use is searched and stored in the object.
27   * 
28   * @author Marcel Malitz
29   * @author Volker Bergmann
30   */
31  public class ElementLocators implements Iterable<GUIElementLocator> {
32  
33      // attributes --------------------------------------------------------------
34  
35      private List<GUIElementLocator> options;
36  
37      // constructors ------------------------------------------------------------
38  
39      /** Creates an {@link ElementLocators} containing a list of {@link IdLocator}s.
40       *  @param locators the alternative {@link IdLocator}s */
41      public ElementLocators(GUIElementLocator... locators) {
42          options = new ArrayList<GUIElementLocator>();
43          for (GUIElementLocator locator : locators) {
44              options.add(locator);
45          }
46      }
47  
48  
49      // Iterable interface implementation ---------------------------------------
50  
51      @Override
52      public Iterator<GUIElementLocator> iterator() {
53          return options.iterator();
54      }
55  
56      /**
57       * Creates a new mutable instance of this locators object which can be used as GUIElementLocator and will be modified by the
58       * AludraTest framework. Such instances may <b>not</b> be shared between Threads!
59       * 
60       * @return A new mutable instance of this locators object, which implements GUIElementLocator.
61       */
62      public GUIElementLocator newMutableInstance() {
63          return new ElementLocatorsGUI(options);
64      }
65  
66      // java.lang.Object overrides ----------------------------------------------
67  
68      @Override
69      public int hashCode() {
70          return super.hashCode() * 31 + options.hashCode(); // NOSONAR
71      }
72  
73      @Override
74      public boolean equals(Object obj) {
75          if (this == obj) {
76              return true;
77          }
78          if (!super.equals(obj) || getClass() != obj.getClass()) {
79              return false;
80          }
81          ElementLocators that = (ElementLocators) obj;
82          return (this.options.equals(that.options));
83      }
84  
85      @Override
86      public String toString() {
87          StringBuilder builder = new StringBuilder(getClass().getSimpleName()).append("[");
88          builder.append("options=[");
89          for (int i = 0; i < options.size(); i++) {
90              builder.append(i > 0 ? ", " : "");
91              builder.append("#").append(i).append(": ").append(options.get(i));
92          }
93          builder.append("]");
94          return builder.append("]").toString();
95      }
96  
97      public static class ElementLocatorsGUI extends GUIElementLocator implements Iterable<GUIElementLocator> {
98  
99          private List<GUIElementLocator> options;
100         private GUIElementLocator usedOption;
101 
102         private ElementLocatorsGUI(List<GUIElementLocator> options) {
103             super("");
104             this.options = new ArrayList<GUIElementLocator>(options);
105         }
106 
107         @Override
108         public Iterator<GUIElementLocator> iterator() {
109             return options.iterator();
110         }
111 
112         // properties --------------------------------------------------------------
113 
114         /**
115          * Sets the pointer to the given locator
116          * 
117          * @param option
118          *            the pointer to set
119          */
120         public final void setUsedOption(GUIElementLocator option) {
121             this.usedOption = option;
122         }
123 
124         /** @return the value of the pointer */
125         public GUIElementLocator getUsedOption() {
126             return usedOption;
127         }
128 
129         // private helpers ---------------------------------------------------------
130 
131         private int indexOf(GUIElementLocator option) {
132             for (int i = 0; i < options.size(); i++) {
133                 GUIElementLocator candidate = options.get(i);
134                 if (candidate == option) { // NOSONAR
135                     return i;
136                 }
137             }
138             return -1;
139         }
140 
141         // java.lang.Object overrides ----------------------------------------------
142         @Override
143         public int hashCode() {
144             return super.hashCode() * 31 + options.hashCode(); // NOSONAR
145         }
146 
147         @Override
148         public boolean equals(Object obj) {
149             if (this == obj) {
150                 return true;
151             }
152             if (!super.equals(obj) || getClass() != obj.getClass()) {
153                 return false;
154             }
155             ElementLocatorsGUI that = (ElementLocatorsGUI) obj;
156             return (this.options.equals(that.options) && (this.usedOption == null ? that.usedOption == null : this.usedOption
157                     .equals(that.usedOption)));
158         }
159 
160         @Override
161         public String toString() {
162             StringBuilder builder = new StringBuilder(getClass().getSimpleName()).append("[");
163             builder.append("options=[");
164             for (int i = 0; i < options.size(); i++) {
165                 builder.append(i > 0 ? ", " : "");
166                 builder.append("#").append(i).append(": ").append(options.get(i));
167             }
168             builder.append("]");
169             builder.append(", usedLocator=");
170             if (usedOption != null) {
171                 builder.append(usedOption.toString());
172             }
173             else {
174                 builder.append("none");
175             }
176             return builder.append("]").toString();
177         }
178 
179     }
180 }