Skip to content
Browse files

Merge pull request #104 from sjoerdmulder/timestamp_fix

JAVA-735 Binary.java should implements Equals and HashCode
  • Loading branch information...
2 parents f97eb44 + 4ee2f82 commit 1c632bcbecba48b76599d782c1ed6d1b7e1f0456 @trishagee trishagee committed Feb 15, 2013
Showing with 70 additions and 6 deletions.
  1. +37 −6 src/main/org/bson/types/Binary.java
  2. +33 −0 src/test/org/bson/types/BinaryTest.java
View
43 src/main/org/bson/types/Binary.java
@@ -24,44 +24,75 @@
import org.bson.BSON;
import java.io.Serializable;
+import java.util.Arrays;
/**
- generic binary holder
+ * generic binary holder
*/
public class Binary implements Serializable {
private static final long serialVersionUID = 7902997490338209467L;
/**
* Creates a Binary object with the default binary type of 0
+ *
* @param data raw data
*/
- public Binary( byte[] data ){
+ public Binary(byte[] data) {
this(BSON.B_GENERAL, data);
}
/**
* Creates a Binary object
+ *
* @param type type of the field as encoded in BSON
* @param data raw data
*/
- public Binary( byte type , byte[] data ){
+ public Binary(byte type, byte[] data) {
_type = type;
_data = data;
}
- public byte getType(){
+ public byte getType() {
return _type;
}
- public byte[] getData(){
+ public byte[] getData() {
return _data;
}
- public int length(){
+ 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;
}
View
33 src/test/org/bson/types/BinaryTest.java
@@ -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());
+
+ }
+}

0 comments on commit 1c632bc

Please sign in to comment.
Something went wrong with that request. Please try again.