Skip to content

Commit 9a7dbdb

Browse files
committed
Merge pull request #8 from zalando/issue7
Fix issue 7: equals & hashCode impl for PgArray
2 parents 60a5f08 + 432b3ef commit 9a7dbdb

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/main/java/de/zalando/typemapper/postgres/PgArray.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.zalando.typemapper.postgres;
22

3+
import com.google.common.base.Objects;
4+
35
import java.sql.Connection;
46
import java.sql.ResultSet;
57
import java.sql.SQLException;
@@ -175,4 +177,19 @@ public void free() throws SQLException {
175177
throw new SQLFeatureNotSupportedException("Feature not supported");
176178
}
177179

180+
@Override
181+
public int hashCode() {
182+
return Objects.hashCode(elementTypeName, serializer.collection);
183+
}
184+
185+
@Override
186+
public boolean equals(Object obj) {
187+
if (obj instanceof PgArray) {
188+
PgArray other = (PgArray) obj;
189+
return Objects.equal(elementTypeName, other.elementTypeName) &&
190+
Objects.equal(serializer.collection, other.serializer.collection);
191+
} else {
192+
return false;
193+
}
194+
}
178195
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.zalando.typemapper.postgres;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotEquals;
9+
10+
public class PgArrayTest {
11+
12+
@Test
13+
public void testEquals() {
14+
PgArray<Short> array1 = PgArray.ARRAY(Arrays.asList((short) 1, (short) 2, (short) 3));
15+
PgArray<Short> array2 = PgArray.ARRAY(Arrays.asList((short) 1, (short) 2, (short) 3));
16+
assertEquals(array1, array2);
17+
18+
PgArray<Integer> array3 = PgArray.ARRAY(Arrays.asList(1, 2, 3));
19+
assertNotEquals(array1, array3);
20+
}
21+
22+
@Test
23+
public void testHashCode() {
24+
PgArray<Short> array1 = PgArray.ARRAY(Arrays.asList((short) 1, (short) 2, (short) 3));
25+
PgArray<Short> array2 = PgArray.ARRAY(Arrays.asList((short) 1, (short) 2, (short) 3));
26+
assertEquals(array1.hashCode(), array2.hashCode());
27+
28+
PgArray<Integer> array3 = PgArray.ARRAY(Arrays.asList(1, 2, 3));
29+
assertNotEquals(array1.hashCode(), array3.hashCode());
30+
}
31+
}

0 commit comments

Comments
 (0)