View Javadoc

1   package net.ramapuram.thomas.model;
2   
3   import java.io.Serializable;
4   import java.util.Comparator;
5   
6   /**
7    * A simple JavaBean to represent label-value pairs. This is most commonly used
8    * when constructing user interface elements which have a label to be displayed
9    * to the user, and a corresponding value to be returned to the server. One
10   * example is the <code>&lt;html:options&gt;</code> tag.
11   * 
12   * <p>Note: this class has a natural ordering that is inconsistent with equals.
13   *
14   * @see org.apache.struts.util.LabelValueBean
15   */
16  public class LabelValue implements Comparable, Serializable {
17  
18      private static final long serialVersionUID = 3689355407466181430L;
19  
20      /**
21       * Comparator that can be used for a case insensitive sort of
22       * <code>LabelValue</code> objects.
23       */
24      public static final Comparator CASE_INSENSITIVE_ORDER = new Comparator() {
25          public int compare(Object o1, Object o2) {
26              String label1 = ((LabelValue) o1).getLabel();
27              String label2 = ((LabelValue) o2).getLabel();
28              return label1.compareToIgnoreCase(label2);
29          }
30      };
31  
32      // ----------------------------------------------------------- Constructors
33  
34  
35      /**
36       * Default constructor.
37       */
38      public LabelValue() {
39          super();
40      }
41  
42      /**
43       * Construct an instance with the supplied property values.
44       *
45       * @param label The label to be displayed to the user.
46       * @param value The value to be returned to the server.
47       */
48      public LabelValue(final String label, final String value) {
49          this.label = label;
50          this.value = value;
51      }
52  
53      // ------------------------------------------------------------- Properties
54  
55  
56      /**
57       * The property which supplies the option label visible to the end user.
58       */
59      private String label;
60  
61      public String getLabel() {
62          return this.label;
63      }
64  
65      public void setLabel(String label) {
66          this.label = label;
67      }
68  
69  
70      /**
71       * The property which supplies the value returned to the server.
72       */
73      private String value;
74  
75      public String getValue() {
76          return this.value;
77      }
78  
79      public void setValue(String value) {
80          this.value = value;
81      }
82  
83  
84      // --------------------------------------------------------- Public Methods
85  
86      /**
87       * Compare LabelValueBeans based on the label, because that's the human
88       * viewable part of the object.
89       *
90       * @see Comparable
91       * @param o LabelValue object to compare to
92       * @return 0 if labels match for compared objects
93       */
94      public int compareTo(Object o) {
95          // Implicitly tests for the correct type, throwing
96          // ClassCastException as required by interface
97          String otherLabel = ((LabelValue) o).getLabel();
98  
99          return this.getLabel().compareTo(otherLabel);
100     }
101 
102     /**
103      * Return a string representation of this object.
104      * @return object as a string
105      */
106     public String toString() {
107         StringBuffer sb = new StringBuffer("LabelValue[");
108         sb.append(this.label);
109         sb.append(", ");
110         sb.append(this.value);
111         sb.append("]");
112         return (sb.toString());
113     }
114 
115     /**
116      * LabelValueBeans are equal if their values are both null or equal.
117      *
118      * @see java.lang.Object#equals(java.lang.Object)
119      * @param obj object to compare to
120      * @return true/false based on whether values match or not
121      */
122     public boolean equals(Object obj) {
123         if (obj == this) {
124             return true;
125         }
126 
127         if (!(obj instanceof LabelValue)) {
128             return false;
129         }
130 
131         LabelValue bean = (LabelValue) obj;
132         int nil = (this.getValue() == null) ? 1 : 0;
133         nil += (bean.getValue() == null) ? 1 : 0;
134 
135         if (nil == 2) {
136             return true;
137         } else if (nil == 1) {
138             return false;
139         } else {
140             return this.getValue().equals(bean.getValue());
141         }
142 
143     }
144 
145     /**
146      * The hash code is based on the object's value.
147      *
148      * @see java.lang.Object#hashCode()
149      * @return hashCode
150      */
151     public int hashCode() {
152         return (this.getValue() == null) ? 17 : this.getValue().hashCode();
153     }
154 }