From 66cd8f579a142d543f6a5366e80d2167859ceba4 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 7 Mar 2015 14:16:52 -0600 Subject: [PATCH] Added equals implementations on model classes. --- build.gradle | 1 + .../nlsongs/model/Performance.groovy | 18 +++++++++++++++ .../jdbernard/nlsongs/model/Service.groovy | 22 ++++++++++++++++++- .../com/jdbernard/nlsongs/model/Song.groovy | 11 ++++++++++ .../com/jdbernard/nlsongs/model/Token.groovy | 16 ++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2acbfbc..6fadd63 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ dependencies { compile 'com.zaxxer:HikariCP-java6:2.3.2' compile 'javax:javaee-api:7.0' compile 'javax.ws.rs:javax.ws.rs-api:2.0.1' + compile 'joda-time:joda-time:2.7' compile 'org.codehaus.groovy:groovy-all:2.3.6' compile 'org.slf4j:slf4j-api:1.7.10' runtime 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.2' diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy index d1f548b..deef1a9 100644 --- a/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Performance.groovy @@ -10,4 +10,22 @@ public class Performance implements Serializable { String drummer String guitarist String leader + + @Override public boolean equals(Object thatObj) { + if (thatObj == null) return false + if (!(thatObj instanceof Performance)) return false + + Performance that = (Performance) thatObj + + return (this.serviceId == that.serviceId && + this.songId == that.songId && + this.pianist == that.pianist && + this.organist == that.organist && + this.bassist == that.bassist && + this.drummer == that.drummer && + this.guitarist == that.guitarist && + this.leader == that.leader) } + + @Override String toString() { + return "($serviceId, $songId): $leader - $pianist" } } diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy index 5929482..6c3e8bd 100644 --- a/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Service.groovy @@ -1,8 +1,28 @@ package com.jdbernard.nlsongs.model +import org.joda.time.LocalDate + public class Service implements Serializable { int id - Date date + private LocalDate date ServiceType serviceType + + public boolean equals(Object thatObj) { + if (thatObj == null) return false + if (!(thatObj instanceof Service)) return false + + Service that = (Service) thatObj + + return (this.id == that.id && + this.date == (that.@date) && + this.serviceType == that.serviceType) } + + public void setDate(Date date) { this.date = LocalDate.fromDateFields(date) } + + public void setDate(LocalDate date) { this.date = date } + + public Date getDate() { return this.date.toDate() } + + public String toString() { return "$id: $date - $serviceType" } } diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy index 112a7c9..6640b79 100644 --- a/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Song.groovy @@ -5,4 +5,15 @@ public class Song implements Serializable { int id String name List artists + + @Override public boolean equals(Object thatObj) { + if (thatObj == null) return false + if (!(thatObj instanceof Song)) return false + + Song that = (Song) thatObj + return (this.id == that.id && + this.name == that.name && + this.artists == that.artists) } + + @Override public String toString() { return "$id: $name ($artists)" } } diff --git a/src/main/groovy/com/jdbernard/nlsongs/model/Token.groovy b/src/main/groovy/com/jdbernard/nlsongs/model/Token.groovy index 02fb335..ba421cf 100644 --- a/src/main/groovy/com/jdbernard/nlsongs/model/Token.groovy +++ b/src/main/groovy/com/jdbernard/nlsongs/model/Token.groovy @@ -2,10 +2,26 @@ package com.jdbernard.nlsongs.model public class Token implements Serializable { + public static final long EXPIRY_WINDOW = 1000 * 60 * 60 * 24; + String token User user Date expires + public Token(Map namedArgs) { + if (!namedArgs.user) throw new IllegalArgumentException( + "Cannot create Token object: missing user property.") + + if (namedArgs.expire) this.expires = namedArgs.expires + else this.expires = new Date((new Date()).time + EXPIRY_WINDOW) + + if (namedArgs.token) this.token = namedArgs.token + else this.token = UUID.randomUUID().toString() } + + public Token(User user) { this([user: user]) } + + public void refresh() { this.expires = new Date((new Date()).time + EXPIRY_WINDOW) } + @Override public boolean equals(Object thatObj) { if (thatObj == null) return false