Skip to content

Commit 0d3b964

Browse files
committed
JAVA-744: Added MongoClientOptions equals and hashCode methods
1 parent a65a4de commit 0d3b964

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/main/com/mongodb/MongoClientOptions.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,55 @@ public boolean isAlwaysUseMBeans() {
511511
return alwaysUseMBeans;
512512
}
513513

514+
@Override
515+
public boolean equals(final Object o) {
516+
if (this == o) return true;
517+
if (o == null || getClass() != o.getClass()) return false;
518+
519+
final MongoClientOptions that = (MongoClientOptions) o;
520+
521+
if (alwaysUseMBeans != that.alwaysUseMBeans) return false;
522+
if (autoConnectRetry != that.autoConnectRetry) return false;
523+
if (connectTimeout != that.connectTimeout) return false;
524+
if (connectionsPerHost != that.connectionsPerHost) return false;
525+
if (cursorFinalizerEnabled != that.cursorFinalizerEnabled) return false;
526+
if (maxAutoConnectRetryTime != that.maxAutoConnectRetryTime) return false;
527+
if (maxWaitTime != that.maxWaitTime) return false;
528+
if (socketKeepAlive != that.socketKeepAlive) return false;
529+
if (socketTimeout != that.socketTimeout) return false;
530+
if (threadsAllowedToBlockForConnectionMultiplier != that.threadsAllowedToBlockForConnectionMultiplier)
531+
return false;
532+
if (!dbDecoderFactory.equals(that.dbDecoderFactory)) return false;
533+
if (!dbEncoderFactory.equals(that.dbEncoderFactory)) return false;
534+
if (description != null ? !description.equals(that.description) : that.description != null) return false;
535+
if (!readPreference.equals(that.readPreference)) return false;
536+
// Compare SocketFactory Class, since some equivalent SocketFactory instances are not equal to each other
537+
if (!socketFactory.getClass().equals(that.socketFactory.getClass())) return false;
538+
if (!writeConcern.equals(that.writeConcern)) return false;
539+
540+
return true;
541+
}
514542

543+
@Override
544+
public int hashCode() {
545+
int result = description != null ? description.hashCode() : 0;
546+
result = 31 * result + connectionsPerHost;
547+
result = 31 * result + threadsAllowedToBlockForConnectionMultiplier;
548+
result = 31 * result + maxWaitTime;
549+
result = 31 * result + connectTimeout;
550+
result = 31 * result + socketTimeout;
551+
result = 31 * result + (socketKeepAlive ? 1 : 0);
552+
result = 31 * result + (autoConnectRetry ? 1 : 0);
553+
result = 31 * result + (int) (maxAutoConnectRetryTime ^ (maxAutoConnectRetryTime >>> 32));
554+
result = 31 * result + readPreference.hashCode();
555+
result = 31 * result + dbDecoderFactory.hashCode();
556+
result = 31 * result + dbEncoderFactory.hashCode();
557+
result = 31 * result + writeConcern.hashCode();
558+
result = 31 * result + socketFactory.hashCode();
559+
result = 31 * result + (cursorFinalizerEnabled ? 1 : 0);
560+
result = 31 * result + (alwaysUseMBeans ? 1 : 0);
561+
return result;
562+
}
515563

516564
private MongoClientOptions(final Builder builder) {
517565
description = builder.description;

src/test/com/mongodb/MongoClientOptionsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,13 @@ public DBDecoder create() {
160160
public void testStaticBuilderCreate() {
161161
Assert.assertNotNull(MongoClientOptions.builder());
162162
}
163+
164+
@Test
165+
public void testEqualsAndHashCode() {
166+
Assert.assertEquals(MongoClientOptions.builder().build(), MongoClientOptions.builder().build());
167+
Assert.assertEquals(MongoClientOptions.builder().build().hashCode(), MongoClientOptions.builder().build().hashCode());
168+
169+
Assert.assertEquals(MongoClientOptions.builder().socketFactory(SSLSocketFactory.getDefault()).build(),
170+
MongoClientOptions.builder().socketFactory(SSLSocketFactory.getDefault()).build());
171+
}
163172
}

0 commit comments

Comments
 (0)