Skip to content

Commit

Permalink
Make Giphy models implement equals/hashcode for in memory caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Sep 12, 2018
1 parent e7965ed commit ebb0e45
Showing 1 changed file with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import com.bumptech.glide.util.Util;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -140,11 +141,31 @@ public String toString() {

/**
* A POJO mirroring an individual GIF image returned from Giphy's api.
*
* <p>Implements equals and hashcode so that in memory caching will work when this object is used
* as a model for loading Glide's images.
*/
public static final class GifResult {
public String id;
GifUrlSet images;

@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 17;
result = 31 * result + (images != null ? images.hashCode() : 17);
return result;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof GifResult) {
GifResult other = (GifResult) obj;
return Util.bothNullOrEqual(id, other.id)
&& Util.bothNullOrEqual(images, other.images);
}
return false;
}

@Override
public String toString() {
return "GifResult{" + "id='" + id + '\'' + ", images=" + images
Expand All @@ -161,6 +182,25 @@ public static final class GifUrlSet {
GifImage fixed_width;
GifImage fixed_height;

@Override
public int hashCode() {
int result = original != null ? original.hashCode() : 17;
result = 31 * result + (fixed_width != null ? fixed_width.hashCode() : 17);
result = 31 * result + (fixed_height != null ? fixed_height.hashCode() : 17);
return result;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof GifUrlSet) {
GifUrlSet other = (GifUrlSet) obj;
return Util.bothNullOrEqual(original, other.original)
&& Util.bothNullOrEqual(fixed_width, other.fixed_width)
&& Util.bothNullOrEqual(fixed_height, other.fixed_height);
}
return false;
}

@Override
public String toString() {
return "GifUrlSet{" + "original=" + original + ", fixed_width="
Expand All @@ -179,6 +219,24 @@ public static final class GifImage {
int height;

@Override
public int hashCode() {
int result = url != null ? url.hashCode() : 17;
result = 31 * result + width;
result = 31 * result + height;
return result;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof GifImage) {
GifImage other = (GifImage) obj;
return other.width == width
&& other.height == height
&& Util.bothNullOrEqual(url, other.url);
}
return false;
}

public String toString() {
return "GifImage{" + "url='" + url + '\'' + ", width=" + width + ", height=" + height + '}';
}
Expand Down

0 comments on commit ebb0e45

Please sign in to comment.