1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 package org.apache.struts.util; 22 23 import java.io.Serializable; 24 25 import java.util.Comparator; 26 27 /** 28 * A simple JavaBean to represent label-value pairs. This is most commonly 29 * used when constructing user interface elements which have a label to be 30 * displayed to the user, and a corresponding value to be returned to the 31 * server. One example is the <code><html:options></code> tag. 32 * 33 * <p> Note: this class has a natural ordering that is inconsistent with 34 * equals. </p> 35 * 36 * @version $Rev$ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005) 37 * $ 38 */ 39 public class LabelValueBean implements Comparable<LabelValueBean>, Serializable { 40 private static final long serialVersionUID = -4583610486738719417L; 41 42 /** 43 * Comparator that can be used for a case insensitive sort of 44 * <code>LabelValueBean</code> objects. 45 */ 46 public static final Comparator<LabelValueBean> CASE_INSENSITIVE_ORDER = 47 (lvb1, lvb2) -> lvb1.getLabel().compareToIgnoreCase(lvb2.getLabel()); 48 49 // ------------------------------------------------------------- Properties 50 51 /** 52 * The property which supplies the option label visible to the end user. 53 */ 54 private String label = null; 55 56 /** 57 * The property which supplies the value returned to the server. 58 */ 59 private String value = null; 60 61 // ----------------------------------------------------------- Constructors 62 63 /** 64 * Default constructor. 65 */ 66 public LabelValueBean() { 67 super(); 68 } 69 70 /** 71 * Construct an instance with the supplied property values. 72 * 73 * @param label The label to be displayed to the user. 74 * @param value The value to be returned to the server. 75 */ 76 public LabelValueBean(String label, String value) { 77 this.label = label; 78 this.value = value; 79 } 80 81 public String getLabel() { 82 return this.label; 83 } 84 85 public void setLabel(String label) { 86 this.label = label; 87 } 88 89 public String getValue() { 90 return this.value; 91 } 92 93 public void setValue(String value) { 94 this.value = value; 95 } 96 97 // --------------------------------------------------------- Public Methods 98 99 /** 100 * Compare LabelValueBeans based on the label, because that's the human 101 * viewable part of the object. 102 * 103 * @see Comparable 104 */ 105 public int compareTo(LabelValueBean o) { 106 // Implicitly tests for the correct type, throwing 107 // ClassCastException as required by interface 108 String otherLabel = o.getLabel(); 109 110 return this.getLabel().compareTo(otherLabel); 111 } 112 113 /** 114 * Return a string representation of this object. 115 */ 116 public String toString() { 117 StringBuilder sb = new StringBuilder("LabelValueBean["); 118 119 sb.append(this.label); 120 sb.append(", "); 121 sb.append(this.value); 122 sb.append("]"); 123 124 return (sb.toString()); 125 } 126 127 /** 128 * LabelValueBeans are equal if their values are both null or equal. 129 * 130 * @see Object#equals(Object) 131 */ 132 public boolean equals(Object obj) { 133 if (obj == this) { 134 return true; 135 } 136 137 if (!(obj instanceof LabelValueBean)) { 138 return false; 139 } 140 141 LabelValueBean bean = (LabelValueBean) obj; 142 int nil = (this.getValue() == null) ? 1 : 0; 143 144 nil += ((bean.getValue() == null) ? 1 : 0); 145 146 if (nil == 2) { 147 return true; 148 } else if (nil == 1) { 149 return false; 150 } else { 151 return this.getValue().equals(bean.getValue()); 152 } 153 } 154 155 /** 156 * The hash code is based on the object's value. 157 * 158 * @see Object#hashCode() 159 */ 160 public int hashCode() { 161 return (this.getValue() == null) ? 17 : this.getValue().hashCode(); 162 } 163 }