-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cons.java
45 lines (36 loc) · 1.11 KB
/
Cons.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
public class Cons implements ImmutableList {
// ---BEGIN INSTANCE VARIABLES---
public final int head;
public final ImmutableList tail;
// ---END INSTANCE VARIABLES---
public Cons(final int head, final ImmutableList tail) {
this.head = head;
this.tail = tail;
} // Cons
public boolean equals(final Object other) {
if (other instanceof Cons) {
final Cons otherCons = (Cons)other;
return head == otherCons.head && tail.equals(otherCons.tail);
} else {
return false;
}
} // equals
public String toString() {
return "Cons(" + head + ", " + tail.toString() + ")";
} // toString
public int hashCode() {
return sum();
} // hashCode
public int sum() {
return head + tail.sum();
}
public int length() {
return tail.length() + 1;
}
public boolean contains(final int value) {
return head == value || tail.contains(value);
}
public ImmutableList append(final ImmutableList other) {
return new Cons(head, tail.append(other));
}
} // Cons