View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.model;
24  
25  import java.io.Serializable;
26  import java.util.Comparator;
27  
28  /**
29   * A simple JavaBean to represent label-value pairs. This is most commonly used
30   * when constructing user interface elements which have a label to be displayed
31   * to the user, and a corresponding value to be returned to the server. One
32   * example is the <code>&lt;html:options&gt;</code> tag.
33   * <p>
34   * Note: this class has a natural ordering that is inconsistent with equals.
35   */
36  public class LabelValue implements Comparable<LabelValue>, Serializable {
37  
38      private static final long serialVersionUID = 3689355407466181430L;
39  
40      /**
41       * Comparator that can be used for a case insensitive sort of
42       * <code>LabelValue</code> objects.
43       */
44      public static final Comparator<LabelValue> CASE_INSENSITIVE_ORDER = new Comparator<LabelValue>() {
45  	public int compare(LabelValue o1, LabelValue o2) {
46  	    String label1 = o1.getLabel();
47  	    String label2 = o2.getLabel();
48  	    return label1.compareToIgnoreCase(label2);
49  	}
50      };
51  
52      /**
53       * Default constructor.
54       */
55      public LabelValue() {
56  	super();
57      }
58  
59      /**
60       * Construct an instance with the supplied property values.
61       * 
62       * @param label
63       *                The label to be displayed to the user.
64       * @param value
65       *                The value to be returned to the server.
66       */
67      public LabelValue(final String label, final String value) {
68  	this.label = label;
69  	this.value = value;
70      }
71  
72      // ------------------------------------------------------------- Properties
73  
74      /**
75       * The property which supplies the option label visible to the end user.
76       */
77      private String label;
78  
79      public String getLabel() {
80  	return this.label;
81      }
82  
83      public void setLabel(String label) {
84  	this.label = label;
85      }
86  
87      /**
88       * The property which supplies the value returned to the server.
89       */
90      private String value;
91  
92      public String getValue() {
93  	return this.value;
94      }
95  
96      public void setValue(String value) {
97  	this.value = value;
98      }
99  
100     // --------------------------------------------------------- Public Methods
101 
102     /**
103      * Compare LabelValueBeans based on the label, because that's the human
104      * viewable part of the object.
105      * 
106      * @see Comparable
107      * @param o
108      *                LabelValue object to compare to
109      * @return 0 if labels match for compared objects
110      */
111     public int compareTo(LabelValue o) {
112 	// Implicitly tests for the correct type, throwing
113 	// ClassCastException as required by interface
114 	String otherLabel = ((LabelValue) o).getLabel();
115 
116 	return this.getLabel().compareTo(otherLabel);
117     }
118 
119     /**
120      * Return a string representation of this object.
121      * 
122      * @return object as a string
123      */
124     @Override
125     public String toString() {
126 	StringBuffer sb = new StringBuffer("LabelValue[");
127 	sb.append(this.label);
128 	sb.append(", ");
129 	sb.append(this.value);
130 	sb.append("]");
131 	return (sb.toString());
132     }
133 
134     /**
135      * LabelValueBeans are equal if their values are both null or equal.
136      * 
137      * @see java.lang.Object#equals(java.lang.Object)
138      * @param obj
139      *                object to compare to
140      * @return true/false based on whether values match or not
141      */
142     @Override
143     public boolean equals(Object obj) {
144 	if (obj == this) {
145 	    return true;
146 	}
147 
148 	if (!(obj instanceof LabelValue)) {
149 	    return false;
150 	}
151 
152 	LabelValue bean = (LabelValue) obj;
153 	int nil = (this.getValue() == null) ? 1 : 0;
154 	nil += (bean.getValue() == null) ? 1 : 0;
155 
156 	if (nil == 2) {
157 	    return true;
158 	} else if (nil == 1) {
159 	    return false;
160 	} else {
161 	    return this.getValue().equals(bean.getValue());
162 	}
163 
164     }
165 
166     /**
167      * The hash code is based on the object's value.
168      * 
169      * @see java.lang.Object#hashCode()
170      * @return hashCode
171      */
172     @Override
173     public int hashCode() {
174 	return (this.getValue() == null) ? 17 : this.getValue().hashCode();
175     }
176 }