Skip to content

Instantly share code, notes, and snippets.

@lgawin
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgawin/482a07df53720d832c2a to your computer and use it in GitHub Desktop.
Save lgawin/482a07df53720d832c2a to your computer and use it in GitHub Desktop.
Example usage of guava's Equivalence, when in some cases, we just want to compare host (authority) parts of URLs
package pl.lgawin.guavasandbox.equivalence;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Objects.equal;
import com.google.common.base.Objects;
import com.google.common.net.MediaType;
public class RemoteObject {
private final String url;
private final MediaType mediaType;
public RemoteObject(String url, MediaType mediaType) {
this.url = url;
this.mediaType = mediaType;
}
public String getUrl() {
return url;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RemoteObject)) {
return false;
}
RemoteObject that = (RemoteObject) o;
return equal(this.url, that.url)
&& equal(this.mediaType, that.mediaType);
}
@Override
public int hashCode() {
return Objects.hashCode(url, mediaType);
}
@Override
public String toString() {
return toStringHelper(this).add("url", url).add("mediaType", mediaType).toString();
}
}
package pl.lgawin.guavasandbox.equivalence;
import static com.google.common.base.Objects.equal;
import static com.google.common.net.MediaType.HTML_UTF_8;
import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.google.common.net.MediaType.XML_UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.base.Equivalence;
import com.google.common.base.Objects;
import org.junit.Before;
import org.junit.Test;
import java.net.URI;
public class RemoteObjectTest {
private static final Equivalence<URI> URI_BY_AUTHORITY_EQUIVALENCE = new Equivalence<URI>() {
@Override
protected boolean doEquivalent(URI a, URI b) {
return equal(a.getAuthority(), b.getAuthority());
}
@Override
protected int doHash(URI url) {
return Objects.hashCode(url.getAuthority());
}
};
private Equivalence<RemoteObject> byAuthorityEquivalence;
@Before
public void setUp() throws Exception {
byAuthorityEquivalence = new Equivalence<RemoteObject>() {
@Override
protected boolean doEquivalent(RemoteObject a, RemoteObject b) {
return URI_BY_AUTHORITY_EQUIVALENCE.equivalent(URI.create(a.getUrl()), URI.create(b.getUrl()));
}
@Override
protected int doHash(RemoteObject remoteObject) {
return URI_BY_AUTHORITY_EQUIVALENCE.wrap(URI.create(remoteObject.getUrl())).hashCode();
}
};
}
@Test
public void buildInEqualsReturnsTrueWhenObjectUrlsAndMediaTypesAreTheSame() throws Exception {
RemoteObject object1 = new RemoteObject("http://example.com/resource1.html", HTML_UTF_8);
RemoteObject object2 = new RemoteObject("http://example.com/resource1.html", HTML_UTF_8);
assertThat(object1).isEqualTo(object2);
}
@Test
public void buildInEqualsReturnsFalseWhenObjectUrlsAreDifferent() throws Exception {
RemoteObject object1 = new RemoteObject("http://example.com/resource1.html", HTML_UTF_8);
RemoteObject object2 = new RemoteObject("http://example.com/resource2.html", HTML_UTF_8);
assertThat(object1).isNotEqualTo(object2);
}
@Test
public void buildInEqualsReturnsFalseWhenObjectUrlsMediaTypesDifferent() throws Exception {
RemoteObject object1 = new RemoteObject("http://example.com/resource1", XML_UTF_8);
RemoteObject object2 = new RemoteObject("http://example.com/resource1", JSON_UTF_8);
assertThat(object1).isNotEqualTo(object2);
}
@Test
public void customEquivalenceComparingAuthoritiesOnlyReturnsTrueIfHostIsTheSame() throws Exception {
RemoteObject object1 = new RemoteObject("http://example.com/resource1", XML_UTF_8);
RemoteObject object2 = new RemoteObject("http://example.com/resource2", JSON_UTF_8);
assertThat(byAuthorityEquivalence.wrap(object1))
.isEqualTo(byAuthorityEquivalence.wrap(object2));
}
@Test
public void customEquivalenceComparingAuthoritiesOnlyReturnsFalseForDifferentHosts() throws Exception {
RemoteObject object1 = new RemoteObject("http://example.com/resource1", JSON_UTF_8);
RemoteObject object2 = new RemoteObject("http://example2.com/resource1", JSON_UTF_8);
assertThat(byAuthorityEquivalence.wrap(object1))
.isNotEqualTo(byAuthorityEquivalence.wrap(object2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment