KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > model > LabelValue


1 package org.appfuse.model;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Comparator JavaDoc;
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  * <p/>
12  * <p/>
13  * Note: this class has a natural ordering that is inconsistent with equals.
14  * </p>
15  *
16  * @see org.apache.struts.util.LabelValueBean
17  */

18 public class LabelValue implements Comparable JavaDoc, Serializable JavaDoc {
19     private static final long serialVersionUID = 3689355407466181430L;
20
21     /**
22      * Comparator that can be used for a case insensitive sort of
23      * <code>LabelValue</code> objects.
24      */

25     public static final Comparator JavaDoc CASE_INSENSITIVE_ORDER = new Comparator JavaDoc() {
26         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
27             String JavaDoc label1 = ((LabelValue) o1).getLabel();
28             String JavaDoc label2 = ((LabelValue) o2).getLabel();
29             return label1.compareToIgnoreCase(label2);
30         }
31     };
32
33
34     // ----------------------------------------------------------- Constructors
35

36
37     /**
38      * Default constructor.
39      */

40     public LabelValue() {
41         super();
42     }
43
44     /**
45      * Construct an instance with the supplied property values.
46      *
47      * @param label The label to be displayed to the user.
48      * @param value The value to be returned to the server.
49      */

50     public LabelValue(String JavaDoc label, String JavaDoc value) {
51         this.label = label;
52         this.value = value;
53     }
54
55
56     // ------------------------------------------------------------- Properties
57

58
59     /**
60      * The property which supplies the option label visible to the end user.
61      */

62     private String JavaDoc label = null;
63
64     public String JavaDoc getLabel() {
65         return this.label;
66     }
67
68     public void setLabel(String JavaDoc label) {
69         this.label = label;
70     }
71
72
73     /**
74      * The property which supplies the value returned to the server.
75      */

76     private String JavaDoc value = null;
77
78     public String JavaDoc getValue() {
79         return this.value;
80     }
81
82     public void setValue(String JavaDoc value) {
83         this.value = value;
84     }
85
86
87     // --------------------------------------------------------- Public Methods
88

89     /**
90      * Compare LabelValueBeans based on the label, because that's the human
91      * viewable part of the object.
92      *
93      * @see Comparable
94      */

95     public int compareTo(Object JavaDoc o) {
96         // Implicitly tests for the correct type, throwing
97
// ClassCastException as required by interface
98
String JavaDoc otherLabel = ((LabelValue) o).getLabel();
99
100         return this.getLabel().compareTo(otherLabel);
101     }
102
103     /**
104      * Return a string representation of this object.
105      */

106     public String JavaDoc toString() {
107         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("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      */

120     public boolean equals(Object JavaDoc obj) {
121         if (obj == this) {
122             return true;
123         }
124
125         if (!(obj instanceof LabelValue)) {
126             return false;
127         }
128
129         LabelValue bean = (LabelValue) obj;
130         int nil = (this.getValue() == null) ? 1 : 0;
131         nil += (bean.getValue() == null) ? 1 : 0;
132
133         if (nil == 2) {
134             return true;
135         } else if (nil == 1) {
136             return false;
137         } else {
138             return this.getValue().equals(bean.getValue());
139         }
140
141     }
142
143     /**
144      * The hash code is based on the object's value.
145      *
146      * @see java.lang.Object#hashCode()
147      */

148     public int hashCode() {
149         return (this.getValue() == null) ? 17 : this.getValue().hashCode();
150     }
151 }
Popular Tags