-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPair.java
82 lines (73 loc) · 2.2 KB
/
Pair.java
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
/**
* @author Khoa Tran
* @version 03/09/2013
* A data structure that represents a pair of values.
* @param <A> a generic type of the first element
* @param <B> a generic type of the second element
*/
public class Pair<A, B> {
/* Private data for a pair construct */
private A first;
private B second;
/**
* Constructor for the Pair class
* @param first - the first element of a generic type A
* @param second - the second element of a generic type B
*/
public Pair(A first, B second) {
this.first = first;
this.second = second;
}
/**
* Checks if the two pairs are equal. Overrides the default "equal" method of
* Java Object class. Just for completeness.
*/
public boolean equals(Object other) {
if (other instanceof Pair) {
@SuppressWarnings("unchecked")
Pair<A, B> otherPair = (Pair<A, B>) other;
return (( this.first == otherPair.first ||
(this.first != null && otherPair.first != null &&
this.first.equals(otherPair.first))) &&
( this.second == otherPair.second ||
( this.second != null && otherPair.second != null &&
this.second.equals(otherPair.second))) );
}
return false;
}
/**
* Displays the pair in a tuple () format
*/
public String toString()
{
return "(" + first + ", " + second + ")";
}
/**
* Gets the first element
* @return the first element
*/
public A getFirst() {
return first;
}
/**
* Sets the first element to a new value
* @param first - the new first element of the pair
*/
public void setFirst(A first) {
this.first = first;
}
/**
* Gets the second element
* @return the second element
*/
public B getSecond() {
return second;
}
/**
* Sets the second element to a new value
* @param second - the new second element of the pair
*/
public void setSecond(B second) {
this.second = second;
}
}