KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > util > LabelValueBean


1 /*
2  * $Id: LabelValueBean.java 54929 2004-10-16 16:38:42Z germuska $
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
19 package org.apache.struts.util;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Comparator JavaDoc;
23
24 /**
25  * A simple JavaBean to represent label-value pairs. This is most commonly used
26  * when constructing user interface elements which have a label to be displayed
27  * to the user, and a corresponding value to be returned to the server. One
28  * example is the <code>&lt;html:options&gt;</code> tag.
29  *
30  * <p>
31  * Note: this class has a natural ordering that is inconsistent with equals.
32  * </p>
33  *
34  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
35  */

36 public class LabelValueBean implements Comparable JavaDoc, Serializable JavaDoc {
37
38     /**
39      * Comparator that can be used for a case insensitive sort of
40      * <code>LabelValueBean</code> objects.
41      */

42     public static final Comparator JavaDoc CASE_INSENSITIVE_ORDER = new Comparator JavaDoc() {
43         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
44             String JavaDoc label1 = ((LabelValueBean) o1).getLabel();
45             String JavaDoc label2 = ((LabelValueBean) o2).getLabel();
46             return label1.compareToIgnoreCase(label2);
47         }
48     };
49
50
51     // ----------------------------------------------------------- Constructors
52

53
54     /**
55      * Default constructor.
56      */

57     public LabelValueBean() {
58         super();
59     }
60
61     /**
62      * Construct an instance with the supplied property values.
63      *
64      * @param label The label to be displayed to the user.
65      * @param value The value to be returned to the server.
66      */

67     public LabelValueBean(String JavaDoc label, String JavaDoc value) {
68         this.label = label;
69         this.value = value;
70     }
71
72
73     // ------------------------------------------------------------- Properties
74

75
76     /**
77      * The property which supplies the option label visible to the end user.
78      */

79     private String JavaDoc label = null;
80
81     public String JavaDoc getLabel() {
82         return this.label;
83     }
84
85     public void setLabel(String JavaDoc label) {
86         this.label = label;
87     }
88
89
90     /**
91      * The property which supplies the value returned to the server.
92      */

93     private String JavaDoc value = null;
94
95     public String JavaDoc getValue() {
96         return this.value;
97     }
98
99     public void setValue(String JavaDoc value) {
100         this.value = value;
101     }
102
103
104     // --------------------------------------------------------- Public Methods
105

106     /**
107      * Compare LabelValueBeans based on the label, because that's the human
108      * viewable part of the object.
109      * @see Comparable
110      */

111     public int compareTo(Object JavaDoc o) {
112         // Implicitly tests for the correct type, throwing
113
// ClassCastException as required by interface
114
String JavaDoc otherLabel = ((LabelValueBean) o).getLabel();
115
116         return this.getLabel().compareTo(otherLabel);
117     }
118
119     /**
120      * Return a string representation of this object.
121      */

122     public String JavaDoc toString() {
123         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("LabelValueBean[");
124         sb.append(this.label);
125         sb.append(", ");
126         sb.append(this.value);
127         sb.append("]");
128         return (sb.toString());
129     }
130
131     /**
132      * LabelValueBeans are equal if their values are both null or equal.
133      * @see java.lang.Object#equals(java.lang.Object)
134      */

135     public boolean equals(Object JavaDoc obj) {
136         if (obj == this) {
137             return true;
138         }
139
140         if (!(obj instanceof LabelValueBean)) {
141             return false;
142         }
143
144         LabelValueBean bean = (LabelValueBean) obj;
145         int nil = (this.getValue() == null) ? 1 : 0;
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     /**
159      * The hash code is based on the object's value.
160      * @see java.lang.Object#hashCode()
161      */

162     public int hashCode() {
163         return (this.getValue() == null) ? 17 : this.getValue().hashCode();
164     }
165 }
166
Popular Tags