dgraham 2003/09/21 18:18:03 Modified: src/share/org/apache/struts/util LabelValueBean.java Log: Implement Comparable interface for PR# 22467. Revision Changes Path 1.7 +36 -6 jakarta-struts/src/share/org/apache/struts/util/LabelValueBean.java Index: LabelValueBean.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/LabelValueBean.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- LabelValueBean.java 4 Jul 2003 18:26:19 -0000 1.6 +++ LabelValueBean.java 22 Sep 2003 01:18:03 -0000 1.7 @@ -62,19 +62,37 @@ package org.apache.struts.util; import java.io.Serializable; +import java.util.Comparator; /** * A simple JavaBean to represent label-value pairs. This is most commonly used * when constructing user interface elements which have a label to be displayed * to the user, and a corresponding value to be returned to the server. One * example is the <code><html:options></code> tag. - * + * + * <p> + * Note: this class has a natural ordering that is inconsistent with equals. + * </p> + * * @author Craig R. McClanahan * @author Martin F N Cooper * @author David Graham + * @author Paul Sundling * @version $Revision$ $Date$ */ -public class LabelValueBean implements Serializable { +public class LabelValueBean implements Comparable, Serializable { + + /** + * Comparator that can be used for a case insensitive sort of + * <code>LabelValueBean</code> objects. + */ + public static final Comparator CASE_INSENSITIVE_ORDER = new Comparator() { + public int compare(Object o1, Object o2) { + String label1 = ((LabelValueBean) o1).getLabel(); + String label2 = ((LabelValueBean) o2).getLabel(); + return label1.compareToIgnoreCase(label2); + } + }; // ----------------------------------------------------------- Constructors @@ -132,6 +150,18 @@ // --------------------------------------------------------- Public Methods + /** + * Compare LabelValueBeans based on the label, because that's the human + * viewable part of the object. + * @see Comparable + */ + public int compareTo(Object o) { + // Implicitly tests for the correct type, throwing + // ClassCastException as required by interface + String otherLabel = ((LabelValueBean) o).getLabel(); + + return this.getLabel().compareTo(otherLabel); + } /** * Return a string representation of this object.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]