-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pair.java
54 lines (51 loc) · 1.43 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jack Rosen
* @param <X>
* @param <Y>
*/
public class Pair <X extends Comparable <X>,Y extends Comparable <Y>> implements Comparable<Pair<X,Y>>{
public X first;
public Y second;
public Pair (X first, Y second){
this.first = first;
this.second = second;
}
/**
* Prints out the info
*/
public void printOut(){
System.out.println(first + ", " + second);
}
/**
* Compares two Pairs
* Compares the first ones and then the second if the first are the same
* @param other The Pair object to test with
* @return Integer based on the comparison
*/
@Override
public int compareTo(Pair<X, Y> other){
int c = this.first.compareTo(other.first);
return c != 0 ? c : this.second.compareTo(other.second);
}
/**
* Compare the two pairs
* @param other The Pair object to test with
* @return A boolean if they are equal
*/
public boolean equals(Pair<X,Y> other){
return first.equals(other.first) && second.equals(other.second);
}
/**
* Swaps the values to return the second value first
* @return A swapped pair
*/
public Pair<Y,X> swap() {
return new Pair<>(second, first);
}
}