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