-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKVPair.java
More file actions
91 lines (78 loc) · 1.84 KB
/
KVPair.java
File metadata and controls
91 lines (78 loc) · 1.84 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
/**
* KVPair class definition: Pretty specific for Project 2
*
* @author CS3114 Instructor and TAs
* @version 9/22/2014
*/
public class KVPair
implements Comparable<KVPair>
{
private Handle theKey;
private Handle theVal;
/**
* Constructor
*
* @param k
* the key (first Handle)
* @param v
* the value (second Handle)
*/
public KVPair(Handle k, Handle v)
{
theKey = k;
theVal = v;
}
/**
* The magic that lets us compare two KVPairs. KVPairs are all that this
* knows how to compare against First compare the key field. If they are
* identical, then break the tie with the value field.
*
* @return the ususal for a comparable (+, 0, -)
* @param it
* the KVPair to compare "this" against
*/
public int compareTo(KVPair it)
{
int compKey = theKey.compareTo(it.key());
return compKey == 0 ? theVal.compareTo(it.value()) : compKey;
}
/**
* Compare a KVPair to a Handle, by comparing theKey to the Handle's
* position. Note that this relies on Handle having a compareTo method.
*
* @return the ususal for a comparable (+, 0, -)
* @param it
* the Handle to compare "this" against
*/
public int compareTo(Handle it)
{
return theKey.compareTo(it);
}
/**
* Getter for "key" Handle
*
* @return the key
*/
public Handle key()
{
return theKey;
}
/**
* Getter for "value" Handle
*
* @return the value
*/
public Handle value()
{
return theVal;
}
/**
* Standard toString method
*
* @return the printable string
*/
public String toString()
{
return theKey.toString() + " " + theVal.toString();
}
}