|
| 1 | +package intermediate; |
| 2 | + |
| 3 | +import com.sandwich.koan.Koan; |
| 4 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 5 | +import static com.sandwich.util.Assert.assertEquals; |
| 6 | + |
| 7 | +public class AboutEquality { |
| 8 | + |
| 9 | + @Koan |
| 10 | + public void sameObject() { |
| 11 | + Object a = new Object(); |
| 12 | + Object b = a; |
| 13 | + assertEquals(a == b, __); |
| 14 | + } |
| 15 | + |
| 16 | + @Koan |
| 17 | + public void equalObject() { |
| 18 | + Integer a = new Integer(1); |
| 19 | + Integer b = new Integer(1); |
| 20 | + assertEquals(a.equals(b), __); |
| 21 | + assertEquals(b.equals(a), __); |
| 22 | + } |
| 23 | + |
| 24 | + @Koan |
| 25 | + public void noObjectShouldbeEqualToNull() { |
| 26 | + assertEquals(new Object().equals(null), __); |
| 27 | + } |
| 28 | + |
| 29 | + static class Car { |
| 30 | + private String name = ""; |
| 31 | + private int horsepower = 0; |
| 32 | + public Car(String s, int p) { |
| 33 | + name = s; horsepower = p; |
| 34 | + } |
| 35 | + @Override |
| 36 | + public boolean equals(Object other) { |
| 37 | + // Change this implementation to match the equals contract |
| 38 | + // Car objects with same horsepower and name values should be considered equal |
| 39 | + // http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object) |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public int hashCode() { |
| 45 | + // see koan ownHashCode |
| 46 | + return super.hashCode(); |
| 47 | + } |
| 48 | + } |
| 49 | + @Koan |
| 50 | + public void equalForOwnObjects() { |
| 51 | + Car car1 = new Car("Beetle", 50); |
| 52 | + Car car2 = new Car("Beetle", 50); |
| 53 | + // See line 37 for the task you have to solve |
| 54 | + assertEquals(car1.equals(car2), true); |
| 55 | + assertEquals(car2.equals(car1), true); |
| 56 | + } |
| 57 | + |
| 58 | + @Koan |
| 59 | + public void unequalForOwnObjects() { |
| 60 | + Car car1 = new Car("Beetle", 50); |
| 61 | + Car car2 = new Car("Prosche", 300); |
| 62 | + // See line 37 for the task you have to solve |
| 63 | + assertEquals(car1.equals(car2), false); |
| 64 | + } |
| 65 | + |
| 66 | + @Koan |
| 67 | + public void unequalForOwnObjectsWithDifferentType() { |
| 68 | + Car car1 = new Car("Beetle", 50); |
| 69 | + String s = "foo"; |
| 70 | + // See line 37 for the task you have to solve |
| 71 | + assertEquals(car1.equals(s), false); |
| 72 | + } |
| 73 | + |
| 74 | + @Koan |
| 75 | + public void equalNullForOwnObjects() { |
| 76 | + Car car1 = new Car("Beetle", 50); |
| 77 | + // See line 37 for the task you have to solve |
| 78 | + assertEquals(car1.equals(null), false); |
| 79 | + } |
| 80 | + |
| 81 | + @Koan |
| 82 | + public void ownHashCode() { |
| 83 | + // As a general rule: When you override equals you should override |
| 84 | + // hash code |
| 85 | + // Read the hash code contract to figure out why |
| 86 | + // http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode() |
| 87 | + |
| 88 | + // Implement a hash code version in line 47 so that the following assertions pass |
| 89 | + Car car1 = new Car("Beetle", 50); |
| 90 | + Car car2 = new Car("Beetle", 50); |
| 91 | + assertEquals(car1.equals(car2), true); |
| 92 | + assertEquals(car1.hashCode() == car2.hashCode(), true); |
| 93 | + } |
| 94 | + |
| 95 | + static class Chicken { |
| 96 | + String color = "green"; |
| 97 | + @Override |
| 98 | + public int hashCode() { |
| 99 | + return 4000; |
| 100 | + } |
| 101 | + @Override |
| 102 | + public boolean equals(Object other) { |
| 103 | + if(!(other instanceof Chicken)) |
| 104 | + return false; |
| 105 | + return ((Chicken)other).color.equals(color); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Koan |
| 110 | + public void ownHashCodeImplementationPartTwo() { |
| 111 | + Chicken chicken1 = new Chicken(); chicken1.color = "black"; |
| 112 | + Chicken chicken2 = new Chicken(); |
| 113 | + assertEquals(chicken1.equals(chicken2), __); |
| 114 | + assertEquals(chicken1.hashCode() == chicken2.hashCode(), __); |
| 115 | + // Does this still fit the hashCode contract? Why? |
| 116 | + // If it's valid why is this still not a good idea? |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments