Permalink
Browse files

Merge pull request #76 from basho/riak-hadoop

Add equals to IndexQuery subclasses
  • Loading branch information...
2 parents 409ae59 + 48f8c4e commit 9cf5d627b1c9f6a51f26286aec0080106a0742fa @russelldb russelldb committed Nov 17, 2011
@@ -50,4 +50,46 @@ public String getIndex() {
public String getBucket() {
return this.bucket;
}
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((bucket == null) ? 0 : bucket.hashCode());
+ result = prime * result + ((index == null) ? 0 : index.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof AbstractIndexQuery)) {
+ return false;
+ }
+ AbstractIndexQuery other = (AbstractIndexQuery) obj;
+ if (bucket == null) {
+ if (other.bucket != null) {
+ return false;
+ }
+ } else if (!bucket.equals(other.bucket)) {
+ return false;
+ }
+ if (index == null) {
+ if (other.index != null) {
+ return false;
+ }
+ } else if (!index.equals(other.index)) {
+ return false;
+ }
+ return true;
+ }
}
@@ -57,4 +57,46 @@ public T from() {
public T to() {
return to;
}
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override public int hashCode() {
+ final int prime = 31;
+ int result = super.hashCode();
+ result = prime * result + ((from == null) ? 0 : from.hashCode());
+ result = prime * result + ((to == null) ? 0 : to.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!super.equals(obj)) {
+ return false;
+ }
+ if (!(obj instanceof AbstractRangeQuery)) {
+ return false;
+ }
+ @SuppressWarnings("rawtypes") AbstractRangeQuery other = (AbstractRangeQuery) obj;
+ if (from == null) {
+ if (other.from != null) {
+ return false;
+ }
+ } else if (!from.equals(other.from)) {
+ return false;
+ }
+ if (to == null) {
+ if (other.to != null) {
+ return false;
+ }
+ } else if (!to.equals(other.to)) {
+ return false;
+ }
+ return true;
+ }
}
@@ -42,4 +42,38 @@ protected AbstractValueQuery(String indexName, String bucket, T value) {
public T getValue() {
return value;
}
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override public int hashCode() {
+ final int prime = 31;
+ int result = super.hashCode();
+ result = prime * result + ((value == null) ? 0 : value.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!super.equals(obj)) {
+ return false;
+ }
+ if (!(obj instanceof AbstractValueQuery)) {
+ return false;
+ }
+ @SuppressWarnings("rawtypes") AbstractValueQuery other = (AbstractValueQuery) obj;
+ if (value == null) {
+ if (other.value != null) {
+ return false;
+ }
+ } else if (!value.equals(other.value)) {
+ return false;
+ }
+ return true;
+ }
}
@@ -0,0 +1,94 @@
+/*
+ * This file is provided to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.basho.riak.client.raw.query.indexes;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.basho.riak.client.query.indexes.BinIndex;
+import com.basho.riak.client.query.indexes.IntIndex;
+
+/**
+ * Really, really, need property based testing to be exhaustive
+ *
+ * @author russell
+ */
+public class IndexQueryEqualsHashCodeTest {
+
+ private static final BinIndex BI1 = BinIndex.named("bi1");
+ private static final BinIndex BI2 = BinIndex.named("bi2");
+ private static final IntIndex II1 = IntIndex.named("ii1");
+ private static final IntIndex II2 = IntIndex.named("ii2");
+
+ private static final String BUCKET_1 = "B1";
+ private static final String BUCKET_2 = "B2";
+
+ private static final String BIN_RANGE_FROM = "a";
+ private static final String BIN_RANGE_TO = "z";
+
+ private static final int INT_RANGE_FROM = 0;
+ private static final int INT_RANGE_TO = 100;
+
+ @Test public void binRangeQueries() {
+ BinRangeQuery brq1 = new BinRangeQuery(BI1, BUCKET_1, BIN_RANGE_FROM, BIN_RANGE_TO);
+ BinRangeQuery brq2 = new BinRangeQuery(BI1, BUCKET_1, BIN_RANGE_FROM, BIN_RANGE_TO);
+ BinRangeQuery brq3 = new BinRangeQuery(BI2, BUCKET_1, BIN_RANGE_FROM, BIN_RANGE_TO);
+ Object notABRQ = new Object();
+ queryTest(brq1, brq2, brq3, notABRQ);
+ }
+
+ @Test public void binValueQueries() {
+ BinValueQuery bvq1 = new BinValueQuery(BI1, BUCKET_1, BIN_RANGE_FROM);
+ BinValueQuery bvq2 = new BinValueQuery(BI1, BUCKET_1, BIN_RANGE_FROM);
+ BinValueQuery bvq3 = new BinValueQuery(BI1, BUCKET_2, BIN_RANGE_FROM);
+ Object notABVQ = new Object();
+ queryTest(bvq1, bvq2, bvq3, notABVQ);
+ }
+
+ @Test public void intRangeQueries() {
+ IntRangeQuery irq1 = new IntRangeQuery(II1, BUCKET_1, INT_RANGE_FROM, INT_RANGE_TO);
+ IntRangeQuery irq2 = new IntRangeQuery(II1, BUCKET_1, INT_RANGE_FROM, INT_RANGE_TO);
+ IntRangeQuery irq3 = new IntRangeQuery(II1, BUCKET_1, INT_RANGE_TO, INT_RANGE_TO);
+ Object notIRQ = new Object();
+ queryTest(irq1, irq2, irq3, notIRQ);
+ }
+
+ @Test public void intValueQueries() {
+ IntValueQuery ivq1 = new IntValueQuery(II1, BUCKET_1, INT_RANGE_FROM);
+ IntValueQuery ivq2 = new IntValueQuery(II1, BUCKET_1, INT_RANGE_FROM);
+ IntValueQuery ivq3 = new IntValueQuery(II2, BUCKET_2, INT_RANGE_FROM);
+ Object notIVQ = new Object();
+ queryTest(ivq1, ivq2, ivq3, notIVQ);
+ }
+
+ private <T> void queryTest(T query, T query2, T different, Object wrongType) {
+ assertTrue("Query should equal itself", query.equals(query));
+ assertTrue("Queries with same properties should be equal", query.equals(query2));
+ assertTrue("Equals should be reflexive", query2.equals(query));
+ assertEquals("Queries with same properties should have equal hascodes", query.hashCode(), query2.hashCode());
+
+ assertFalse("Queries with different properties should not be equal", query.equals(different));
+ assertFalse("Queries with different properties should not be equal", different.equals(query));
+ assertThat("Queries with different properties should not have equal hashcodes", query.hashCode(),
+ not(equalTo(different.hashCode())));
+ assertFalse("Queries should not equal non-queries", query.equals(wrongType));
+ }
+
+}

0 comments on commit 9cf5d62

Please sign in to comment.