001: /*
002: * $Id: LabelValueBean.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.util;
022:
023: import java.io.Serializable;
024:
025: import java.util.Comparator;
026:
027: /**
028: * A simple JavaBean to represent label-value pairs. This is most commonly
029: * used when constructing user interface elements which have a label to be
030: * displayed to the user, and a corresponding value to be returned to the
031: * server. One example is the <code><html:options></code> tag.
032: *
033: * <p> Note: this class has a natural ordering that is inconsistent with
034: * equals. </p>
035: *
036: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
037: * $
038: */
039: public class LabelValueBean implements Comparable, Serializable {
040: /**
041: * Comparator that can be used for a case insensitive sort of
042: * <code>LabelValueBean</code> objects.
043: */
044: public static final Comparator CASE_INSENSITIVE_ORDER = new Comparator() {
045: public int compare(Object o1, Object o2) {
046: String label1 = ((LabelValueBean) o1).getLabel();
047: String label2 = ((LabelValueBean) o2).getLabel();
048:
049: return label1.compareToIgnoreCase(label2);
050: }
051: };
052:
053: // ------------------------------------------------------------- Properties
054:
055: /**
056: * The property which supplies the option label visible to the end user.
057: */
058: private String label = null;
059:
060: /**
061: * The property which supplies the value returned to the server.
062: */
063: private String value = null;
064:
065: // ----------------------------------------------------------- Constructors
066:
067: /**
068: * Default constructor.
069: */
070: public LabelValueBean() {
071: super ();
072: }
073:
074: /**
075: * Construct an instance with the supplied property values.
076: *
077: * @param label The label to be displayed to the user.
078: * @param value The value to be returned to the server.
079: */
080: public LabelValueBean(String label, String value) {
081: this .label = label;
082: this .value = value;
083: }
084:
085: public String getLabel() {
086: return this .label;
087: }
088:
089: public void setLabel(String label) {
090: this .label = label;
091: }
092:
093: public String getValue() {
094: return this .value;
095: }
096:
097: public void setValue(String value) {
098: this .value = value;
099: }
100:
101: // --------------------------------------------------------- Public Methods
102:
103: /**
104: * Compare LabelValueBeans based on the label, because that's the human
105: * viewable part of the object.
106: *
107: * @see Comparable
108: */
109: public int compareTo(Object o) {
110: // Implicitly tests for the correct type, throwing
111: // ClassCastException as required by interface
112: String otherLabel = ((LabelValueBean) o).getLabel();
113:
114: return this .getLabel().compareTo(otherLabel);
115: }
116:
117: /**
118: * Return a string representation of this object.
119: */
120: public String toString() {
121: StringBuffer sb = new StringBuffer("LabelValueBean[");
122:
123: sb.append(this .label);
124: sb.append(", ");
125: sb.append(this .value);
126: sb.append("]");
127:
128: return (sb.toString());
129: }
130:
131: /**
132: * LabelValueBeans are equal if their values are both null or equal.
133: *
134: * @see Object#equals(Object)
135: */
136: public boolean equals(Object obj) {
137: if (obj == this ) {
138: return true;
139: }
140:
141: if (!(obj instanceof LabelValueBean)) {
142: return false;
143: }
144:
145: LabelValueBean bean = (LabelValueBean) obj;
146: int nil = (this .getValue() == null) ? 1 : 0;
147:
148: nil += ((bean.getValue() == null) ? 1 : 0);
149:
150: if (nil == 2) {
151: return true;
152: } else if (nil == 1) {
153: return false;
154: } else {
155: return this .getValue().equals(bean.getValue());
156: }
157: }
158:
159: /**
160: * The hash code is based on the object's value.
161: *
162: * @see Object#hashCode()
163: */
164: public int hashCode() {
165: return (this .getValue() == null) ? 17 : this.getValue()
166: .hashCode();
167: }
168: }
|