KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > util > ValueLabelPair


1 /**
2  * Nov 23, 2004
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.util;
19
20 import java.util.Map JavaDoc;
21
22 /**
23  * Simple bean for grouping together a <i>value</i> and its <i>label</i>.
24  *
25  * @author jonni
26  * @author hgani
27  * @version $Id: ValueLabelPair.java,v 1.1.1.1 2006/02/26 11:39:21 hgani Exp $
28  */

29 public final class ValueLabelPair implements Map.Entry JavaDoc {
30   /**
31    * Value attribute.
32    */

33   private Object JavaDoc value;
34   /**
35    * Label attribute.
36    */

37   private Object JavaDoc label;
38
39   /**
40    * Creates a new instance.
41    *
42    * @param value the value
43    * @param label the associated label
44    */

45   public ValueLabelPair(Object JavaDoc value, Object JavaDoc label) {
46     super();
47     if (value == null) {
48       throw new NullPointerException JavaDoc("Value cannot be null");
49     }
50     this.value = value;
51     this.label = label;
52   }
53
54   /**
55    * Sets the value (Map.Entry interface).
56    *
57    * @param value the new value
58    * @return the new value
59    */

60   public Object JavaDoc setValue(Object JavaDoc value) {
61     this.value = value;
62     return this.value;
63   }
64
65   /**
66    * Returns the value (Map.Entry interface).
67    *
68    * @return the value
69    */

70   public Object JavaDoc getKey() {
71     return this.value;
72   }
73
74   /**
75    * Returns the label (Map.Entry interface).
76    *
77    * @return the label
78    */

79   public Object JavaDoc getValue() {
80     return this.label;
81   }
82
83   /**
84    * Returns the value as <code>String</code>.
85    * If the value is <code>null</code>, the string "null" will
86    * be returned instead.
87    *
88    * @return the value as <code>String</code>
89    */

90   public String JavaDoc getValueAsString() {
91     return String.valueOf(this.value);
92   }
93
94   /**
95    * Returns the label as <code>String</code>.
96    * If the label is <code>null</code>, the string "null" will
97    * be returned instead.
98    *
99    * @return the label as <code>String</code>
100    */

101   public String JavaDoc getLabelAsString() {
102     return String.valueOf(this.label);
103   }
104
105   /** {@inheritDoc} */
106   public boolean equals(Object JavaDoc obj) {
107     if (this == obj) {
108       return true;
109     }
110
111     if (!(obj instanceof ValueLabelPair)) {
112       return false;
113     }
114
115     ValueLabelPair temp = (ValueLabelPair) obj;
116     return (this.value == null ?
117         temp.value == null : this.value.equals(temp.value));
118   }
119
120   /** {@inheritDoc} */
121   public int hashCode() {
122     int hashCode = (this.value == null ? 0 : this.value.hashCode());
123     return hashCode;
124   }
125 }
126
Popular Tags