Add hashCode and equals to UniformGranularitySpec

* Also add hashCode != 0 to AllGranularity and NoneGranularity
This commit is contained in:
Charles Allen 2015-10-13 14:56:05 -07:00
parent c9d6994040
commit f432b8e3f9
4 changed files with 129 additions and 2 deletions

View File

@ -62,7 +62,7 @@ public final class AllGranularity extends BaseQueryGranularity
@Override @Override
public int hashCode() public int hashCode()
{ {
return 0; return 1;
} }
@Override @Override

View File

@ -53,7 +53,7 @@ public final class NoneGranularity extends BaseQueryGranularity
@Override @Override
public int hashCode() public int hashCode()
{ {
return 0; return -1;
} }
@Override @Override

View File

@ -100,4 +100,39 @@ public class UniformGranularitySpec implements GranularitySpec
{ {
return Optional.fromNullable(inputIntervals); return Optional.fromNullable(inputIntervals);
} }
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UniformGranularitySpec that = (UniformGranularitySpec) o;
if (segmentGranularity != that.segmentGranularity) {
return false;
}
if (!queryGranularity.equals(that.queryGranularity)) {
return false;
}
if (inputIntervals != null ? !inputIntervals.equals(that.inputIntervals) : that.inputIntervals != null) {
return false;
}
return !(wrappedSpec != null ? !wrappedSpec.equals(that.wrappedSpec) : that.wrappedSpec != null);
}
@Override
public int hashCode()
{
int result = segmentGranularity.hashCode();
result = 31 * result + queryGranularity.hashCode();
result = 31 * result + (inputIntervals != null ? inputIntervals.hashCode() : 0);
result = 31 * result + (wrappedSpec != null ? wrappedSpec.hashCode() : 0);
return result;
}
} }

View File

@ -22,6 +22,7 @@ import com.google.common.base.Optional;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.metamx.common.Granularity; import com.metamx.common.Granularity;
import io.druid.granularity.QueryGranularity;
import io.druid.jackson.DefaultObjectMapper; import io.druid.jackson.DefaultObjectMapper;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.Interval; import org.joda.time.Interval;
@ -121,4 +122,95 @@ public class UniformGranularityTest
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
} }
@Test
public void testEquals()
{
final GranularitySpec spec = new UniformGranularitySpec(
Granularity.DAY,
null,
Lists.newArrayList(
new Interval("2012-01-08T00Z/2012-01-11T00Z"),
new Interval("2012-01-07T00Z/2012-01-08T00Z"),
new Interval("2012-01-03T00Z/2012-01-04T00Z"),
new Interval("2012-01-01T00Z/2012-01-03T00Z")
)
);
equalsCheck(
spec, new UniformGranularitySpec(
Granularity.DAY,
null,
Lists.newArrayList(
new Interval("2012-01-08T00Z/2012-01-11T00Z"),
new Interval("2012-01-07T00Z/2012-01-08T00Z"),
new Interval("2012-01-03T00Z/2012-01-04T00Z"),
new Interval("2012-01-01T00Z/2012-01-03T00Z")
)
)
);
}
public void equalsCheck(GranularitySpec spec1, GranularitySpec spec2) {
Assert.assertEquals(spec1, spec2);
Assert.assertEquals(spec1.hashCode(), spec2.hashCode());
}
@Test
public void testNotEquals()
{
final GranularitySpec spec = new UniformGranularitySpec(
Granularity.DAY,
null,
Lists.newArrayList(
new Interval("2012-01-08T00Z/2012-01-11T00Z"),
new Interval("2012-01-07T00Z/2012-01-08T00Z"),
new Interval("2012-01-03T00Z/2012-01-04T00Z"),
new Interval("2012-01-01T00Z/2012-01-03T00Z")
)
);
notEqualsCheck(
spec, new UniformGranularitySpec(
Granularity.YEAR,
null,
Lists.newArrayList(
new Interval("2012-01-08T00Z/2012-01-11T00Z"),
new Interval("2012-01-07T00Z/2012-01-08T00Z"),
new Interval("2012-01-03T00Z/2012-01-04T00Z"),
new Interval("2012-01-01T00Z/2012-01-03T00Z")
)
)
);
notEqualsCheck(
spec, new UniformGranularitySpec(
Granularity.DAY,
null,
Lists.newArrayList(
new Interval("2012-01-08T00Z/2012-01-12T00Z"),
new Interval("2012-01-07T00Z/2012-01-08T00Z"),
new Interval("2012-01-03T00Z/2012-01-04T00Z"),
new Interval("2012-01-01T00Z/2012-01-03T00Z")
)
)
);
notEqualsCheck(
spec, new UniformGranularitySpec(
Granularity.DAY,
QueryGranularity.ALL,
Lists.newArrayList(
new Interval("2012-01-08T00Z/2012-01-11T00Z"),
new Interval("2012-01-07T00Z/2012-01-08T00Z"),
new Interval("2012-01-03T00Z/2012-01-04T00Z"),
new Interval("2012-01-01T00Z/2012-01-03T00Z")
)
)
);
}
private void notEqualsCheck(GranularitySpec spec1, GranularitySpec spec2) {
Assert.assertNotEquals(spec1, spec2);
Assert.assertNotEquals(spec1.hashCode(), spec2.hashCode());
}
} }