Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit e3450ac

Browse files
author
Hideki Itakura
authored
Fixed #1474 - 2.0: Implement Blob.equals()/hashCode() (#1477)
* Fixed #1474 - 2.0: Implement Blob.equals()/hashCode() - Implement Blob.equals()/hashCode() - Fixed Document.equals() and hashCode() with Database. * update * Follows iOS implementation for Blob.equals() and hashCode() methods.
1 parent 24fb6fe commit e3450ac

File tree

4 files changed

+126
-2
lines changed

4 files changed

+126
-2
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.couchbase.lite;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class BlobTest extends BaseTest {
9+
final static String kBlobTestBlob1 = "i'm blob";
10+
final static String kBlobTestBlob2 = "i'm blob2";
11+
12+
@Test
13+
public void testEquals() throws CouchbaseLiteException {
14+
15+
byte[] content1a = kBlobTestBlob1.getBytes();
16+
byte[] content1b = kBlobTestBlob1.getBytes();
17+
byte[] content2a = kBlobTestBlob2.getBytes();
18+
19+
// store blob
20+
Blob data1a = new Blob("text/plain", content1a);
21+
Blob data1b = new Blob("text/plain", content1b);
22+
Blob data1c = new Blob("text/plain", content1a); // not store in db
23+
Blob data2a = new Blob("text/plain", content2a);
24+
25+
assertTrue(data1a.equals(data1b));
26+
assertTrue(data1b.equals(data1a));
27+
assertFalse(data1a.equals(data2a));
28+
assertFalse(data1b.equals(data2a));
29+
assertFalse(data2a.equals(data1a));
30+
assertFalse(data2a.equals(data1b));
31+
32+
MutableDocument mDoc = new MutableDocument();
33+
mDoc.setBlob("blob1a", data1a);
34+
mDoc.setBlob("blob1b", data1b);
35+
mDoc.setBlob("blob2a", data2a);
36+
Document doc = save(mDoc);
37+
38+
Blob blob1a = doc.getBlob("blob1a");
39+
Blob blob1b = doc.getBlob("blob1b");
40+
Blob blob2a = doc.getBlob("blob2a");
41+
42+
assertTrue(blob1a.equals(blob1b));
43+
assertTrue(blob1b.equals(blob1a));
44+
assertFalse(blob1a.equals(blob2a));
45+
assertFalse(blob1b.equals(blob2a));
46+
assertFalse(blob2a.equals(blob1a));
47+
assertFalse(blob2a.equals(blob1b));
48+
49+
assertTrue(blob1a.equals(data1c));
50+
assertTrue(data1c.equals(blob1a));
51+
}
52+
@Test
53+
public void testHashCode() throws CouchbaseLiteException {
54+
byte[] content1a = kBlobTestBlob1.getBytes();
55+
byte[] content1b = kBlobTestBlob1.getBytes();
56+
byte[] content2a = kBlobTestBlob2.getBytes();
57+
58+
// store blob
59+
Blob data1a = new Blob("text/plain", content1a);
60+
Blob data1b = new Blob("text/plain", content1b);
61+
Blob data1c = new Blob("text/plain", content1a); // not store in db
62+
Blob data2a = new Blob("text/plain", content2a);
63+
64+
assertTrue(data1a.hashCode() == data1b.hashCode());
65+
assertTrue(data1b.hashCode() == data1a.hashCode());
66+
assertFalse(data1a.hashCode() == data2a.hashCode());
67+
assertFalse(data1b.hashCode() == data2a.hashCode());
68+
assertFalse(data2a.hashCode() == data1a.hashCode());
69+
assertFalse(data2a.hashCode() == data1b.hashCode());
70+
71+
MutableDocument mDoc = new MutableDocument();
72+
mDoc.setBlob("blob1a", data1a);
73+
mDoc.setBlob("blob1b", data1b);
74+
mDoc.setBlob("blob2a", data2a);
75+
Document doc = save(mDoc);
76+
77+
Blob blob1a = doc.getBlob("blob1a");
78+
Blob blob1b = doc.getBlob("blob1b");
79+
Blob blob2a = doc.getBlob("blob2a");
80+
81+
assertTrue(blob1a.hashCode() == blob1b.hashCode());
82+
assertTrue(blob1b.hashCode() == blob1a.hashCode());
83+
assertFalse(blob1a.hashCode() == blob2a.hashCode());
84+
assertFalse(blob1b.hashCode() == blob2a.hashCode());
85+
assertFalse(blob2a.hashCode() == blob1a.hashCode());
86+
assertFalse(blob2a.hashCode() == blob1b.hashCode());
87+
88+
assertTrue(blob1a.hashCode() == data1c.hashCode());
89+
assertTrue(data1c.hashCode() == blob1a.hashCode());
90+
}
91+
}

shared/src/main/java/com/couchbase/lite/Blob.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ public Map<String, Object> getProperties() {
289289
}
290290
}
291291

292+
//---------------------------------------------
293+
// Override
294+
//---------------------------------------------
295+
292296
/**
293297
* Returns a string representation of the object.
294298
*
@@ -300,6 +304,23 @@ public String toString() {
300304
contentType, (length() + 512) / 1024);
301305
}
302306

307+
@Override
308+
public boolean equals(Object o) {
309+
if (this == o) return true;
310+
if (!(o instanceof Blob)) return false;
311+
312+
Blob m = (Blob) o;
313+
if (digest != null && m.digest != null)
314+
return digest.equals(m.digest);
315+
else
316+
return Arrays.equals(getContent(), m.getContent());
317+
}
318+
319+
@Override
320+
public int hashCode() {
321+
return Arrays.hashCode(getContent());
322+
}
323+
303324
//---------------------------------------------
304325
// Package level access
305326
//---------------------------------------------

shared/src/main/java/com/couchbase/lite/Database.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,18 @@ protected void finalize() throws Throwable {
667667
//---------------------------------------------
668668
// Package level access
669669
//---------------------------------------------
670+
boolean equalsWithPath(Database other) {
671+
if (other == null) return false;
672+
File path = getPath();
673+
File otherPath = other.getPath();
674+
if (path == null && otherPath == null)
675+
return true;
676+
else if ((path == null && otherPath != null) || (path != null && otherPath == null))
677+
return false;
678+
else
679+
return path.equals(otherPath);
680+
}
681+
670682
C4BlobStore getBlobStore() throws CouchbaseLiteRuntimeException {
671683
mustBeOpen();
672684
try {

shared/src/main/java/com/couchbase/lite/Document.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public boolean equals(Object o) {
317317
Document doc = (Document) o;
318318

319319
// Step 1: Check Database
320-
if (_database != null ? !_database.equals(doc._database) : doc._database != null)
320+
if (_database != null ? !_database.equalsWithPath(doc._database) : doc._database != null)
321321
return false;
322322

323323
// Step 2: Check document ID
@@ -336,7 +336,7 @@ public boolean equals(Object o) {
336336
@Override
337337
public int hashCode() {
338338
// NOTE _id and _dict never null
339-
int result = _database != null ? _database.hashCode() : 0;
339+
int result = _database != null && _database.getPath() != null ? _database.getPath().hashCode() : 0;
340340
result = 31 * result + _id.hashCode();
341341
result = 31 * result + _dict.hashCode();
342342
return result;

0 commit comments

Comments
 (0)