-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPair.java
72 lines (63 loc) · 1.44 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
/**
* CS2030S PE1 Question 2
* AY20/21 Semester 2
*
* @author A0238768J
*/
public class Pair<T> implements SourceList<T> {
private T first;
private SourceList<T> second;
public Pair(T first, SourceList<T> second) {
this.first = first;
this.second = second;
}
@Override
public T getFirst() {
return this.first;
}
@Override
public SourceList<T> getSecond() {
return this.second;
}
@Override
public String toString() {
return this.first + ", " + this.second;
}
// Write your code here
@Override
public int length() {
int count = 1;
if (this.second instanceof EmptyList<?>) {
return count;
} else {
count += this.second.length();
}
return count;
}
@Override
public boolean equals(Object o) {
if (o instanceof Pair<?>) {
@SuppressWarnings("unchecked")
Pair<?> op = (Pair<?>) o;
if (this.first == op.getFirst()) {
return this.second.equals(op.getSecond());
} else {
return false;
}
} else {
return false;
}
}
@Override
public SourceList<T> filter(BooleanCondition cond) {
if (cond.test(this.first)) {
return new Pair<T>(this.first, this.second.filter(cond));
} else {
return this.second.filter(cond);
}
}
@Override
public <U> SourceList<U> map(Transformer<? super T, ? extends U> trans) {
return new Pair<U>(trans.transform(this.first), this.second.map(trans));
}
}