View Javadoc

1   package com.atlassian.pageobjects.elements;
2   
3   import com.google.common.base.Function;
4   
5   /**
6    * {@link Option} objects factory.
7    *
8    */
9   public final class Options
10  {
11      static abstract class AbstractOption implements Option
12      {
13          private final String id, value, text;
14  
15          public AbstractOption(String id, String value, String text)
16          {
17              if (value == null && id == null && text == null)
18              {
19                  throw new IllegalArgumentException("One of the option identifiers must be non-null");
20              }
21              this.value = value;
22              this.id = id;
23              this.text = text;
24          }
25  
26          public String id()
27          {
28              return id;
29          }
30  
31          public String value()
32          {
33              return value;
34          }
35  
36          public String text()
37          {
38              return text;
39          }
40  
41          @Override
42          public boolean equals(Object obj)
43          {
44              if (obj == null)
45              {
46                  return false;
47              }
48              if (!Option.class.isAssignableFrom(obj.getClass()))
49              {
50                  return false;
51              }
52              AbstractOption other = (AbstractOption) obj;
53              if (id != null && !id.equals(other.id))
54              {
55                  return false;
56              }
57              if (value != null && !value.equals(other.value))
58              {
59                  return false;
60              }
61              if(text != null && !text.equals(other.text))
62              {
63                  return false;
64              }
65              return true;
66          }
67  
68          @Override
69          public int hashCode()
70          {
71              int result = id != null ? id.hashCode() : 0;
72              result = 31 * result + (value != null ? value.hashCode() : 0);
73              result = 31 * result + (text != null ? text.hashCode(): 0);
74              return result;
75          }
76      }
77  
78  
79      public static class IdOption extends AbstractOption
80      {
81          IdOption(String id) { super(id, null, null); }
82  
83      }
84  
85      public static class ValueOption extends AbstractOption
86      {
87          ValueOption(String value) { super(null, value, null); }
88      }
89  
90      public static class TextOption extends AbstractOption
91      {
92          TextOption(String text) { super(null,null,text); }
93      }
94  
95      public static class FullOption extends AbstractOption
96      {
97          FullOption(String id, String value, String text) { super(id, value, text); }
98      }
99  
100     /**
101      * New option distinguishable by its HTML ID.
102      *
103      * @param id HTML id of the option
104      * @return new option
105      */
106     public static IdOption id(String id)
107     {
108         return new IdOption(id);
109     }
110 
111     /**
112      * New option distinguishable by its HTML value attribute.
113      *
114      * @param value HTML value attribute of the option
115      * @return new option
116      */
117     public static ValueOption value(String value)
118     {
119         return new ValueOption(value);
120     }
121 
122     /**
123      * New option distinguishable by its text between tags
124      *
125      * @param text Text between the option tags
126      * @return new option
127      */
128     public static TextOption text(String text)
129     {
130         return new TextOption(text);
131     }
132 
133     /**
134      * New option distinguishable all identifiers. Some of the identifiers may be <code>null</code>, but at least
135      * one has to be non-<code>null</code>
136      *
137      * @param id HTML id of the option
138      * @param value HTML value attribute of the option
139      * @param text Text between the option tags
140      * @return new option
141      */
142     public static Option full(String id, String value, String text)
143     {
144         return new FullOption(id, value, text);
145     }
146 
147     public static Function<Option,String> getValue()
148     {
149         return new Function<Option, String>()
150         {
151             @Override
152             public String apply(Option option)
153             {
154                 return option.value();
155             }
156         };
157     }
158 
159     public static Function<Option,String> getId()
160     {
161         return new Function<Option, String>()
162         {
163             @Override
164             public String apply(Option option)
165             {
166                 return option.id();
167             }
168         };
169     }
170 
171     public static Function<Option,String> getText()
172     {
173         return new Function<Option, String>()
174         {
175             @Override
176             public String apply(Option option)
177             {
178                 return option.text();
179             }
180         };
181     }
182 }