001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licencing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.audit;
011:
012: import java.io.Serializable;
013: import org.jgap.util.*;
014:
015: /**
016: * A (key, value) tupel.
017: *
018: * @author Klaus Meffert
019: * @since 2.3
020: */
021: public class KeyedValue implements ICloneable, Serializable {
022: /** String containing the CVS revision. Read out via reflection!*/
023: private static final String CVS_REVISION = "$Revision: 1.3 $";
024:
025: private Comparable m_key;
026:
027: private Number m_value;
028:
029: /**
030: * Creates a new (key, value) tupel.
031: *
032: * @param a_key the key
033: * @param a_value the value, could be null
034: *
035: * @author Klaus Meffert
036: * @since 2.3
037: */
038: public KeyedValue(final Comparable a_key, final Number a_value) {
039: m_key = a_key;
040: m_value = a_value;
041: }
042:
043: /**
044: * @return key of the tupel
045: *
046: * @author Klaus Meffert
047: * @since 2.3
048: */
049: public Comparable getKey() {
050: return m_key;
051: }
052:
053: /**
054: * @return value of the tupel
055: *
056: * @author Klaus Meffert
057: * @since 2.3
058: */
059: public synchronized Number getValue() {
060: return m_value;
061: }
062:
063: /**
064: * Sets the value for the key
065: *
066: * @param a_value the value to set for the key
067: *
068: * @author Klaus Meffert
069: * @since 2.3
070: */
071: public synchronized void setValue(final Number a_value) {
072: m_value = a_value;
073: }
074:
075: /**
076: * Tests if this object is equal to another.
077: *
078: * @param a_object the other object
079: *
080: * @return true: this object is equal to other one
081: *
082: * @author Klaus Meffert
083: * @since 2.3
084: */
085: public boolean equals(final Object a_object) {
086: if (this == a_object) {
087: return true;
088: }
089: if (!(a_object instanceof KeyedValue)) {
090: return false;
091: }
092: final KeyedValue defaultKeyedValue = (KeyedValue) a_object;
093: if (m_key != null ? !m_key.equals(defaultKeyedValue.m_key)
094: : defaultKeyedValue.m_key != null) {
095: return false;
096: }
097: if (m_value != null ? !m_value
098: .equals(defaultKeyedValue.m_value)
099: : 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: } else {
116: result = m_key.hashCode();
117: }
118: result = 41 * result;
119: if (m_value == null) {
120: result += -3;
121: } else {
122: result += m_value.hashCode();
123: }
124: return result;
125: }
126:
127: /**
128: * @return clone of the current instance
129: *
130: * @author Klaus Meffert
131: * @since 2.3
132: */
133: public Object clone() {
134: try {
135: KeyedValue clone = (KeyedValue) super .clone();
136: return clone;
137: } catch (CloneNotSupportedException cex) {
138: throw new CloneException(cex);
139: }
140: }
141: }
|