1 package com.panogenesis.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><html:options></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, Serializable {
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
33
34
35
36 /***
37 * Default constructor.
38 */
39 public LabelValue() {
40 super();
41 }
42
43 /***
44 * Construct an instance with the supplied property values.
45 *
46 * @param label The label to be displayed to the user.
47 * @param value The value to be returned to the server.
48 */
49 public LabelValue(String label, String value) {
50 this.label = label;
51 this.value = value;
52 }
53
54
55
56
57
58 /***
59 * The property which supplies the option label visible to the end user.
60 */
61 private String label = null;
62
63 public String getLabel() {
64 return this.label;
65 }
66
67 public void setLabel(String label) {
68 this.label = label;
69 }
70
71
72 /***
73 * The property which supplies the value returned to the server.
74 */
75 private String value = null;
76
77 public String getValue() {
78 return this.value;
79 }
80
81 public void setValue(String value) {
82 this.value = value;
83 }
84
85
86
87
88 /***
89 * Compare LabelValueBeans based on the label, because that's the human
90 * viewable part of the object.
91 *
92 * @see Comparable
93 */
94 public int compareTo(Object o) {
95
96
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 */
105 public String toString() {
106 StringBuffer sb = new StringBuffer("LabelValue[");
107 sb.append(this.label);
108 sb.append(", ");
109 sb.append(this.value);
110 sb.append("]");
111 return (sb.toString());
112 }
113
114 /***
115 * LabelValueBeans are equal if their values are both null or equal.
116 *
117 * @see java.lang.Object#equals(java.lang.Object)
118 */
119 public boolean equals(Object obj) {
120 if (obj == this) {
121 return true;
122 }
123
124 if (!(obj instanceof LabelValue)) {
125 return false;
126 }
127
128 LabelValue bean = (LabelValue) obj;
129 int nil = (this.getValue() == null) ? 1 : 0;
130 nil += (bean.getValue() == null) ? 1 : 0;
131
132 if (nil == 2) {
133 return true;
134 } else if (nil == 1) {
135 return false;
136 } else {
137 return this.getValue().equals(bean.getValue());
138 }
139
140 }
141
142 /***
143 * The hash code is based on the object's value.
144 *
145 * @see java.lang.Object#hashCode()
146 */
147 public int hashCode() {
148 return (this.getValue() == null) ? 17 : this.getValue().hashCode();
149 }
150 }