View Javadoc

1   /*
2    * Copyright 2007-2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.util;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.EqualsBuilder;
23  import org.apache.commons.lang.builder.HashCodeBuilder;
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  
26  /**
27   * A class for associating a text label with a given key.
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class KeyLabelPair implements Serializable, Comparable<KeyLabelPair>{
32  	private static final long serialVersionUID = 7997396592230527472L;
33      public Object key;
34      public String label;
35      public int numPaddedSpaces;
36      
37      public KeyLabelPair() {
38  
39      }
40  
41      public KeyLabelPair(Object key, String label) {
42          this.key = key;
43          this.label = label;
44      }
45  
46      public Object getKey() {
47          return key;
48      }
49  
50      public String getLabel() {
51          return label;
52      }
53  
54      public void setLabel(String label) {
55          this.label = label;
56      }
57  
58      public void setKey(Object key) {
59          this.key = key;
60      }
61      
62      public void setNumPaddedSpaces(int numPaddedSpaces) {
63      	this.numPaddedSpaces = numPaddedSpaces;
64      }
65   
66      public String getHtmlSpacePadding() {
67      	return StringUtils.repeat("&nbsp;", numPaddedSpaces);
68      }
69  
70      /** {inheritDoc} */
71  	@Override
72  	public int hashCode() {
73  		return new HashCodeBuilder(19, 39)
74  		.append(this.label)
75  		.append(this.key)
76  		.append(this.numPaddedSpaces)
77  		.toHashCode();
78  	}
79  
80  	/** {inheritDoc} */
81  	@Override
82  	public boolean equals(Object obj) {
83  		if (this == obj) {
84  			return true;
85  		}
86  		if (obj == null) {
87  			return false;
88  		}
89  		if (!(obj instanceof KeyLabelPair)) {
90  			return false;
91  		}
92  		
93  		final KeyLabelPair other = (KeyLabelPair) obj;
94  		return new EqualsBuilder()
95  		.append(this.label, other.label)
96  		.append(this.key, other.key)
97  		.append(this.numPaddedSpaces, other.numPaddedSpaces)
98  		.isEquals();
99  	}
100 
101 	/** {inheritDoc} */
102 	@Override
103 	public String toString() {
104 		return new ToStringBuilder(this)
105 		.append("label", this.label)
106 		.append("key", this.key)
107 		.append("numPaddedSpaces", this.numPaddedSpaces)
108 		.toString();
109 	}
110 
111 	/** {inheritDoc} */
112 	public int compareTo(KeyLabelPair o) {
113 		if (o == null) {
114 			throw new NullPointerException("the object to compare to is null");
115 		}
116 		final CompareToBuilder builder = new CompareToBuilder();
117 		builder.append(this.label, o.label, String.CASE_INSENSITIVE_ORDER);
118 		
119 		if (this.key instanceof String && o.key instanceof String) {
120 			builder.append(this.key, o.key, String.CASE_INSENSITIVE_ORDER);	
121 		} else {
122 			builder.append(this.key, o.key);
123 		}
124 		
125 		builder.append(this.numPaddedSpaces, o.numPaddedSpaces);
126 		return builder.toComparison();
127 	}
128 }