Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

JAVA-735 Binary.java should implements Equals and HashCode

  • Loading branch information...
commit 648c21a4d52b8d673f6a9ef5c79122fe8076010b 1 parent 5839dc7
Sjoerd Mulder sjoerdmulder authored
21 src/main/org/bson/types/Binary.java
View
@@ -24,6 +24,7 @@
import org.bson.BSON;
import java.io.Serializable;
+import java.util.Arrays;
/**
generic binary holder
@@ -62,6 +63,26 @@ public int length(){
return _data.length;
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Binary)) return false;
+
+ Binary binary = (Binary) o;
+
+ if (_type != binary._type) return false;
+ if (!Arrays.equals(_data, binary._data)) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = (int) _type;
+ result = 31 * result + (_data != null ? Arrays.hashCode(_data) : 0);
+ return result;
+ }
+
final byte _type;
final byte[] _data;
}
33 src/test/org/bson/types/BinaryTest.java
View
@@ -0,0 +1,33 @@
+package org.bson.types;
+
+import org.testng.annotations.Test;
+
+import static com.mongodb.util.MyAsserts.assertEquals;
+import static com.mongodb.util.MyAsserts.assertFalse;
+import static com.mongodb.util.MyAsserts.assertTrue;
+
+public class BinaryTest {
+
+ Binary first = new Binary((byte) 0, new byte[]{0, 1, 2});
+ Binary second = new Binary((byte) 1, new byte[]{0, 1, 2});
+ Binary third = new Binary((byte) 0, new byte[]{0, 1, 2, 3});
+ Binary fourth = new Binary((byte) 0, new byte[]{0, 1, 2});
+
+ @Test
+ public void testEquals() throws Exception {
+ assertFalse(first.equals(second));
+ assertFalse(first.equals(third));
+ assertEquals(first, fourth);
+ assertFalse(first.equals("abc"));
+ assertFalse(first.equals(null));
+ }
+
+ @Test
+ public void testHashCode() throws Exception {
+ assertTrue(first.hashCode() == fourth.hashCode());
+ assertFalse(first.hashCode() == second.hashCode());
+ assertFalse(first.hashCode() == third.hashCode());
+ assertFalse(second.hashCode() == third.hashCode());
+
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.