forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutEquality.java
More file actions
122 lines (107 loc) · 3.23 KB
/
AboutEquality.java
File metadata and controls
122 lines (107 loc) · 3.23 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package intermediate;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutEquality {
// This suite of Koans expands on the concepts introduced in beginner.AboutEquality
@Koan
public void sameObject() {
Object a = new Object();
Object b = a;
assertEquals(a == b, __);
}
@Koan
public void equalObject() {
Integer a = new Integer(1);
Integer b = new Integer(1);
assertEquals(a.equals(b), __);
assertEquals(b.equals(a), __);
}
@Koan
public void noObjectShouldBeEqualToNull() {
assertEquals(new Object().equals(null), __);
}
static class Car {
@SuppressWarnings("unused")
private String name = "";
@SuppressWarnings("unused")
private int horsepower = 0;
public Car(String s, int p) {
name = s; horsepower = p;
}
@Override
public boolean equals(Object other) {
// Change this implementation to match the equals contract
// Car objects with same horsepower and name values should be considered equal
// http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)
return false;
}
@Override
public int hashCode() {
// see koan ownHashCode
return super.hashCode();
}
}
@Koan
public void equalForOwnObjects() {
Car car1 = new Car("Beetle", 50);
Car car2 = new Car("Beetle", 50);
// See line 37 for the task you have to solve
assertEquals(car1.equals(car2), true);
assertEquals(car2.equals(car1), true);
}
@Koan
public void unequalForOwnObjects() {
Car car1 = new Car("Beetle", 50);
Car car2 = new Car("Porsche", 300);
// See line 37 for the task you have to solve
assertEquals(car1.equals(car2), false);
}
@Koan
public void unequalForOwnObjectsWithDifferentType() {
Car car1 = new Car("Beetle", 50);
String s = "foo";
// See line 37 for the task you have to solve
assertEquals(car1.equals(s), false);
}
@Koan
public void equalNullForOwnObjects() {
Car car1 = new Car("Beetle", 50);
// See line 37 for the task you have to solve
assertEquals(car1.equals(null), false);
}
@Koan
public void ownHashCode() {
// As a general rule: When you override equals you should override
// hash code
// Read the hash code contract to figure out why
// http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()
// Implement a hash code version in line 47 so that the following assertions pass
Car car1 = new Car("Beetle", 50);
Car car2 = new Car("Beetle", 50);
assertEquals(car1.equals(car2), true);
assertEquals(car1.hashCode() == car2.hashCode(), true);
}
static class Chicken {
String color = "green";
@Override
public int hashCode() {
return 4000;
}
@Override
public boolean equals(Object other) {
if(!(other instanceof Chicken))
return false;
return ((Chicken)other).color.equals(color);
}
}
@Koan
public void ownHashCodeImplementationPartTwo() {
Chicken chicken1 = new Chicken(); chicken1.color = "black";
Chicken chicken2 = new Chicken();
assertEquals(chicken1.equals(chicken2), __);
assertEquals(chicken1.hashCode() == chicken2.hashCode(), __);
// Does this still fit the hashCode contract? Why?
// If it's valid why is this still not a good idea?
}
}