Created
May 4, 2010 14:21
-
-
Save EwanDawson/389459 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Appointment implements Comparable { | |
LocalDateTime time | |
String description | |
String place | |
// Define a natural ordering of Appointments | |
// base on their times. | |
int compareTo(that) { this.date <=> that.date } | |
// Instances should be considered equal if they | |
// have equal state | |
@Override boolean equals(that) { | |
if (this.is(that)) return true | |
if (!(this instanceof that)) return false | |
this.time == that.time && | |
this.description == that.description && | |
this.place == than.place | |
} | |
/* ... */ | |
} | |
def appt = new Appointment( | |
time: new LocalDateTime(2010, 5, 4, 15, 0), | |
description: "Pain", | |
place: "Dental Clinic" | |
) | |
def duplicateAppt = new Appointment( | |
time: new LocalDateTime(2010, 5, 4, 15, 0), | |
description: "Pain", | |
place: "Dental Clinic" | |
) | |
assert appt == duplicateAppt | |
duplicateAppt.place = "Torture Chamber" | |
assert !appt.equals(duplicateAppt) | |
assert appt != duplicateAppt // Uh-oh... FAIL! | |
// This last assertion fails, since under the hood | |
// Groovy is executing something like | |
// assert !(appt.compareTo(duplicateAppt) == 0) | |
// even though it is clear from our class definition | |
// that the intention is for these objects to be | |
// considered un-equal. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment