Skip to content

Commit 00217d2

Browse files
committed
fix hashcode/equals in PageableList
1 parent 9cc8b17 commit 00217d2

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/main/java/com/gooddata/collections/PageableList.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,15 @@ public boolean equals(final Object o) {
236236
final PageableList<?> that = (PageableList<?>) o;
237237

238238
if (!items.equals(that.items)) return false;
239-
if (!getPaging().equals(that.getPaging())) return false;
240-
return getLinks().equals(that.getLinks());
239+
if (getPaging() != null ? !getPaging().equals(that.getPaging()) : that.getPaging() != null) return false;
240+
return getLinks() != null ? getLinks().equals(that.getLinks()) : that.getLinks() == null;
241241
}
242242

243243
@Override
244244
public int hashCode() {
245245
int result = items.hashCode();
246-
result = 31 * result + getPaging().hashCode();
247-
result = 31 * result + getLinks().hashCode();
246+
result = 31 * result + (getPaging() != null ? getPaging().hashCode() : 0);
247+
result = 31 * result + (getLinks() != null ? getLinks().hashCode() : 0);
248248
return result;
249249
}
250250
}

src/test/java/com/gooddata/collections/PageableListTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
import org.testng.annotations.Test;
99

10+
import java.util.Collections;
11+
1012
import static java.util.Arrays.asList;
1113
import static org.hamcrest.CoreMatchers.notNullValue;
1214
import static org.hamcrest.CoreMatchers.nullValue;
1315
import static org.hamcrest.MatcherAssert.assertThat;
1416
import static org.hamcrest.Matchers.empty;
1517
import static org.hamcrest.Matchers.hasSize;
1618
import static org.hamcrest.core.Is.is;
19+
import static org.hamcrest.core.IsNot.not;
1720

1821
public class PageableListTest {
1922

@@ -41,4 +44,18 @@ public void testCollectionWithPaging() {
4144
assertThat(collection.getNextPage(), notNullValue());
4245
assertThat(collection.getNextPage().getPageUri(null).toString(), is("next"));
4346
}
47+
48+
@Test
49+
public void testEquals() {
50+
assertThat(new PageableList<>(), is(new PageableList<>()));
51+
assertThat(new PageableList<>(Collections.singletonList(1), null), is(not(new PageableList<>())));
52+
}
53+
54+
55+
@Test
56+
public void testHashCode() {
57+
assertThat(new PageableList<>().hashCode(), is(new PageableList<>().hashCode()));
58+
assertThat(new PageableList<>(Collections.singletonList(1), null).hashCode(), is(not(new PageableList<>().hashCode())));
59+
}
60+
4461
}

0 commit comments

Comments
 (0)