-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociation.java
More file actions
162 lines (150 loc) · 4.5 KB
/
Copy pathAssociation.java
File metadata and controls
162 lines (150 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// A class for binding key/value pairs.
// (c) 1998,2001 duane a. bailey
import java.util.Map;
/*
public class Association<K,V>
*/
/**
* A class implementing a key-value pair. This class associates an
* immutable key with a mutable value. Used in many other structures.
* <P>
* Example Usage:
* <P>
* To store the number of classes a student has taken from five different
* professors and to output this information, we could use the following.
* <P>
* <pre>
* public static void main(String[] argv){
* //store the number of classes taken by the student in an array of associations
* {@link Association} [] classesTaken = new Association[5];
* classesTaken[0] = new {@link #Association(Object,Object) Association("Andrea", new Integer(5))};
* classesTaken[1] = new Association("Barbara", new Integer(1));
* classesTaken[2] = new Association("Bill", new Integer(3));
* classesTaken[3] = new Association("Duane", new Integer(2));
* classesTaken[4] = new Association("Tom", new Integer(1));
*
* //print out each item in the array
* for (int i = 0; i< classesTaken.length; i++){
* System.out.println("This Student has taken " + classesTaken[i].{@link #getValue()} +
* " classes from " + classesTaken[i].{@link #getKey()}+ ".");
* }
* }
* </pre>
* @version $Id: Association.java 34 2007-08-09 14:43:44Z bailey $
* @author, 2001 duane a. bailey
*/
public class Association<K extends Comparable<K>, V> implements Comparable<Association<K, V>>
{
/**
* The immutable key. An arbitrary object.
*/
protected K theKey; // the key of the key-value pair
/**
* The mutable value. An arbitrary object.
*/
protected V theValue; // the value of the key-value pair
/*
for example:
Association<String,Integer> personAttribute =
new Assocation<String,Integer>("Age",34);
*/
/**
* Constructs a pair from a key and value.
*
* @pre key is non-null
* @post constructs a key-value pair
* @param key A non-null object.
* @param value A (possibly null) object.
*/
public Association(K key, V value)
{
theKey = key;
theValue = value;
}
/**
* Constructs a pair from a key; value is null.
*
* @pre key is non-null
* @post constructs a key-value pair; value is null
* @param key A non-null key value.
*/
public Association(K key)
{
this(key,null);
}
/**
* Standard comparison function. Comparison based on keys only.
*
* @pre other is non-null Association
* @post returns true iff the keys are equal
* @param other Another association.
* @return true iff the keys are equal.
*/
public boolean equals(Object other)
{
Association otherAssoc = (Association)other;
return getKey().equals(otherAssoc.getKey());
}
/**
* Standard hashcode function.
*
* @post return hash code association with this association
* @return A hash code for association.
* @see Hashtable
*/
public int hashCode()
{
return getKey().hashCode();
}
/**
* Fetch value from association. May return null.
*
* @post returns value from association
* @return The value field of the association.
*/
public V getValue()
{
return theValue;
}
/**
* Fetch key from association. Should not return null.
*
* @post returns key from association
* @return Key of the key-value pair.
*/
public K getKey()
{
return theKey;
}
/**
* Sets the value of the key-value pair.
*
* @post sets association's value to value
* @param value The new value.
*/
public V setValue(V value)
{
V oldValue = theValue;
theValue = value;
return oldValue;
}
/**
* Standard string representation of an association.
*
* @post returns string representation
* @return String representing key-value pair.
*/
public String toString()
{
StringBuffer s = new StringBuffer();
s.append("<Association: "+getKey()+"="+getValue()+">");
return s.toString();
}
/*
...
*/
@Override
public int compareTo(Association<K, V> o) {
return theKey.compareTo(o.getKey());
}
}