Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hashCode and equals to UniformGranularitySpec #1824

Merged
merged 1 commit into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean equals(Object o)
@Override
public int hashCode()
{
return 0;
return 1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean equals(Object o)
@Override
public int hashCode()
{
return 0;
return -1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,39 @@ public Optional<List<Interval>> getIntervals()
{
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.metamx.common.Granularity;
import io.druid.granularity.QueryGranularity;
import io.druid.jackson.DefaultObjectMapper;
import org.joda.time.DateTime;
import org.joda.time.Interval;
Expand Down Expand Up @@ -121,4 +122,95 @@ public void testJson()
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());
}
}