KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > KeyedValue


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.audit;
11
12 import java.io.Serializable JavaDoc;
13 import org.jgap.util.*;
14
15 /**
16  * A (key, value) tupel.
17  *
18  * @author Klaus Meffert
19  * @since 2.3
20  */

21 public class KeyedValue
22     implements ICloneable, Serializable JavaDoc {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private static final String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
25
26   private Comparable JavaDoc m_key;
27
28   private Number JavaDoc m_value;
29
30   /**
31    * Creates a new (key, value) tupel.
32    *
33    * @param a_key the key
34    * @param a_value the value, could be null
35    *
36    * @author Klaus Meffert
37    * @since 2.3
38    */

39   public KeyedValue(final Comparable JavaDoc a_key, final Number JavaDoc a_value) {
40     m_key = a_key;
41     m_value = a_value;
42   }
43
44   /**
45    * @return key of the tupel
46    *
47    * @author Klaus Meffert
48    * @since 2.3
49    */

50   public Comparable JavaDoc getKey() {
51     return m_key;
52   }
53
54   /**
55    * @return value of the tupel
56    *
57    * @author Klaus Meffert
58    * @since 2.3
59    */

60   public synchronized Number JavaDoc getValue() {
61     return m_value;
62   }
63
64   /**
65    * Sets the value for the key
66    *
67    * @param a_value the value to set for the key
68    *
69    * @author Klaus Meffert
70    * @since 2.3
71    */

72   public synchronized void setValue(final Number JavaDoc a_value) {
73     m_value = a_value;
74   }
75
76   /**
77    * Tests if this object is equal to another.
78    *
79    * @param a_object the other object
80    *
81    * @return true: this object is equal to other one
82    *
83    * @author Klaus Meffert
84    * @since 2.3
85    */

86   public boolean equals(final Object JavaDoc a_object) {
87     if (this == a_object) {
88       return true;
89     }
90     if (! (a_object instanceof KeyedValue)) {
91       return false;
92     }
93     final KeyedValue defaultKeyedValue = (KeyedValue) a_object;
94     if (m_key != null ? !m_key.equals(defaultKeyedValue.m_key)
95         : defaultKeyedValue.m_key != null) {
96       return false;
97     }
98     if (m_value != null ? !m_value.equals(defaultKeyedValue.m_value)
99         : defaultKeyedValue.m_value != null) {
100       return false;
101     }
102     return true;
103   }
104
105   /**
106    * @return hash code of the instance
107    *
108    * @author Klaus Meffert
109    * @since 2.3
110    */

111   public int hashCode() {
112     int result;
113     if (m_key == null) {
114       result = -37;
115     }
116     else {
117       result = m_key.hashCode();
118     }
119     result = 41 * result;
120     if (m_value == null) {
121       result += -3;
122     }
123     else {
124       result += m_value.hashCode();
125     }
126     return result;
127   }
128
129   /**
130    * @return clone of the current instance
131    *
132    * @author Klaus Meffert
133    * @since 2.3
134    */

135   public Object JavaDoc clone() {
136     try {
137       KeyedValue clone = (KeyedValue)super.clone();
138       return clone;
139     } catch (CloneNotSupportedException JavaDoc cex) {
140       throw new CloneException(cex);
141     }
142   }
143 }
144
Popular Tags