View Javadoc

1   /*
2    * $Id: LabelValueBean.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.util;
19  
20  import java.io.Serializable;
21  
22  import java.util.Comparator;
23  
24  /***
25   * A simple JavaBean to represent label-value pairs. This is most commonly
26   * used when constructing user interface elements which have a label to be
27   * displayed to the user, and a corresponding value to be returned to the
28   * server. One example is the <code>&lt;html:options&gt;</code> tag.
29   *
30   * <p> Note: this class has a natural ordering that is inconsistent with
31   * equals. </p>
32   *
33   * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
34   *          $
35   */
36  public class LabelValueBean implements Comparable, Serializable {
37      /***
38       * Comparator that can be used for a case insensitive sort of
39       * <code>LabelValueBean</code> objects.
40       */
41      public static final Comparator CASE_INSENSITIVE_ORDER =
42          new Comparator() {
43              public int compare(Object o1, Object o2) {
44                  String label1 = ((LabelValueBean) o1).getLabel();
45                  String label2 = ((LabelValueBean) o2).getLabel();
46  
47                  return label1.compareToIgnoreCase(label2);
48              }
49          };
50  
51      // ------------------------------------------------------------- Properties
52  
53      /***
54       * The property which supplies the option label visible to the end user.
55       */
56      private String label = null;
57  
58      /***
59       * The property which supplies the value returned to the server.
60       */
61      private String value = null;
62  
63      // ----------------------------------------------------------- Constructors
64  
65      /***
66       * Default constructor.
67       */
68      public LabelValueBean() {
69          super();
70      }
71  
72      /***
73       * Construct an instance with the supplied property values.
74       *
75       * @param label The label to be displayed to the user.
76       * @param value The value to be returned to the server.
77       */
78      public LabelValueBean(String label, String value) {
79          this.label = label;
80          this.value = value;
81      }
82  
83      public String getLabel() {
84          return this.label;
85      }
86  
87      public void setLabel(String label) {
88          this.label = label;
89      }
90  
91      public String getValue() {
92          return this.value;
93      }
94  
95      public void setValue(String value) {
96          this.value = value;
97      }
98  
99      // --------------------------------------------------------- Public Methods
100 
101     /***
102      * Compare LabelValueBeans based on the label, because that's the human
103      * viewable part of the object.
104      *
105      * @see Comparable
106      */
107     public int compareTo(Object o) {
108         // Implicitly tests for the correct type, throwing
109         // ClassCastException as required by interface
110         String otherLabel = ((LabelValueBean) o).getLabel();
111 
112         return this.getLabel().compareTo(otherLabel);
113     }
114 
115     /***
116      * Return a string representation of this object.
117      */
118     public String toString() {
119         StringBuffer sb = new StringBuffer("LabelValueBean[");
120 
121         sb.append(this.label);
122         sb.append(", ");
123         sb.append(this.value);
124         sb.append("]");
125 
126         return (sb.toString());
127     }
128 
129     /***
130      * LabelValueBeans are equal if their values are both null or equal.
131      *
132      * @see Object#equals(Object)
133      */
134     public boolean equals(Object obj) {
135         if (obj == this) {
136             return true;
137         }
138 
139         if (!(obj instanceof LabelValueBean)) {
140             return false;
141         }
142 
143         LabelValueBean bean = (LabelValueBean) obj;
144         int nil = (this.getValue() == null) ? 1 : 0;
145 
146         nil += ((bean.getValue() == null) ? 1 : 0);
147 
148         if (nil == 2) {
149             return true;
150         } else if (nil == 1) {
151             return false;
152         } else {
153             return this.getValue().equals(bean.getValue());
154         }
155     }
156 
157     /***
158      * The hash code is based on the object's value.
159      *
160      * @see Object#hashCode()
161      */
162     public int hashCode() {
163         return (this.getValue() == null) ? 17 : this.getValue().hashCode();
164     }
165 }