Skip to content

Commit f5ea80a

Browse files
christophstroblmp911de
authored andcommitted
Implement equals, hashCode and toString for CollectionOptions.
Closes: #4210 Original pull request: #4277
1 parent a414e21 commit f5ea80a

File tree

2 files changed

+195
-2
lines changed

2 files changed

+195
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java

+121-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.data.util.Optionals;
2727
import org.springframework.lang.Nullable;
2828
import org.springframework.util.Assert;
29+
import org.springframework.util.ObjectUtils;
2930

3031
import com.mongodb.client.model.ValidationAction;
3132
import com.mongodb.client.model.ValidationLevel;
@@ -62,8 +63,7 @@ public CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Null
6263
}
6364

6465
private CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped,
65-
@Nullable Collation collation, ValidationOptions validationOptions,
66-
@Nullable TimeSeriesOptions timeSeriesOptions) {
66+
@Nullable Collation collation, ValidationOptions validationOptions, @Nullable TimeSeriesOptions timeSeriesOptions) {
6767

6868
this.maxDocuments = maxDocuments;
6969
this.size = size;
@@ -346,6 +346,56 @@ public Optional<TimeSeriesOptions> getTimeSeriesOptions() {
346346
return Optional.ofNullable(timeSeriesOptions);
347347
}
348348

349+
@Override
350+
public String toString() {
351+
return "CollectionOptions{" + "maxDocuments=" + maxDocuments + ", size=" + size + ", capped=" + capped
352+
+ ", collation=" + collation + ", validationOptions=" + validationOptions + ", timeSeriesOptions="
353+
+ timeSeriesOptions + ", disableValidation="
354+
+ disableValidation() + ", strictValidation=" + strictValidation() + ", moderateValidation="
355+
+ moderateValidation() + ", warnOnValidationError=" + warnOnValidationError() + ", failOnValidationError="
356+
+ failOnValidationError() + '}';
357+
}
358+
359+
@Override
360+
public boolean equals(@Nullable Object o) {
361+
if (this == o) {
362+
return true;
363+
}
364+
if (o == null || getClass() != o.getClass()) {
365+
return false;
366+
}
367+
368+
CollectionOptions that = (CollectionOptions) o;
369+
370+
if (!ObjectUtils.nullSafeEquals(maxDocuments, that.maxDocuments)) {
371+
return false;
372+
}
373+
if (!ObjectUtils.nullSafeEquals(size, that.size)) {
374+
return false;
375+
}
376+
if (!ObjectUtils.nullSafeEquals(capped, that.capped)) {
377+
return false;
378+
}
379+
if (!ObjectUtils.nullSafeEquals(collation, that.collation)) {
380+
return false;
381+
}
382+
if (!ObjectUtils.nullSafeEquals(validationOptions, that.validationOptions)) {
383+
return false;
384+
}
385+
return ObjectUtils.nullSafeEquals(timeSeriesOptions, that.timeSeriesOptions);
386+
}
387+
388+
@Override
389+
public int hashCode() {
390+
int result = ObjectUtils.nullSafeHashCode(maxDocuments);
391+
result = 31 * result + ObjectUtils.nullSafeHashCode(size);
392+
result = 31 * result + ObjectUtils.nullSafeHashCode(capped);
393+
result = 31 * result + ObjectUtils.nullSafeHashCode(collation);
394+
result = 31 * result + ObjectUtils.nullSafeHashCode(validationOptions);
395+
result = 31 * result + ObjectUtils.nullSafeHashCode(timeSeriesOptions);
396+
return result;
397+
}
398+
349399
/**
350400
* Encapsulation of ValidationOptions options.
351401
*
@@ -440,6 +490,40 @@ public Optional<ValidationAction> getValidationAction() {
440490
boolean isEmpty() {
441491
return !Optionals.isAnyPresent(getValidator(), getValidationAction(), getValidationLevel());
442492
}
493+
494+
@Override
495+
public String toString() {
496+
497+
return "ValidationOptions{" + "validator=" + validator + ", validationLevel=" + validationLevel
498+
+ ", validationAction=" + validationAction + '}';
499+
}
500+
501+
@Override
502+
public boolean equals(@Nullable Object o) {
503+
if (this == o) {
504+
return true;
505+
}
506+
if (o == null || getClass() != o.getClass()) {
507+
return false;
508+
}
509+
510+
ValidationOptions that = (ValidationOptions) o;
511+
512+
if (!ObjectUtils.nullSafeEquals(validator, that.validator)) {
513+
return false;
514+
}
515+
if (validationLevel != that.validationLevel)
516+
return false;
517+
return validationAction == that.validationAction;
518+
}
519+
520+
@Override
521+
public int hashCode() {
522+
int result = ObjectUtils.nullSafeHashCode(validator);
523+
result = 31 * result + ObjectUtils.nullSafeHashCode(validationLevel);
524+
result = 31 * result + ObjectUtils.nullSafeHashCode(validationAction);
525+
return result;
526+
}
443527
}
444528

445529
/**
@@ -525,5 +609,40 @@ public String getMetaField() {
525609
public GranularityDefinition getGranularity() {
526610
return granularity;
527611
}
612+
613+
@Override
614+
public String toString() {
615+
616+
return "TimeSeriesOptions{" + "timeField='" + timeField + '\'' + ", metaField='" + metaField + '\''
617+
+ ", granularity=" + granularity + '}';
618+
}
619+
620+
@Override
621+
public boolean equals(@Nullable Object o) {
622+
if (this == o) {
623+
return true;
624+
}
625+
if (o == null || getClass() != o.getClass()) {
626+
return false;
627+
}
628+
629+
TimeSeriesOptions that = (TimeSeriesOptions) o;
630+
631+
if (!ObjectUtils.nullSafeEquals(timeField, that.timeField)) {
632+
return false;
633+
}
634+
if (!ObjectUtils.nullSafeEquals(metaField, that.metaField)) {
635+
return false;
636+
}
637+
return ObjectUtils.nullSafeEquals(granularity, that.granularity);
638+
}
639+
640+
@Override
641+
public int hashCode() {
642+
int result = ObjectUtils.nullSafeHashCode(timeField);
643+
result = 31 * result + ObjectUtils.nullSafeHashCode(metaField);
644+
result = 31 * result + ObjectUtils.nullSafeHashCode(granularity);
645+
return result;
646+
}
528647
}
529648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.mongodb.core.CollectionOptions.*;
20+
21+
import org.bson.Document;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.data.mongodb.core.query.Collation;
24+
import org.springframework.data.mongodb.core.validation.Validator;
25+
26+
/**
27+
* @author Christoph Strobl
28+
*/
29+
class CollectionOptionsUnitTests {
30+
31+
@Test // GH-4210
32+
void emptyEquals() {
33+
assertThat(empty()).isEqualTo(empty());
34+
}
35+
36+
@Test // GH-4210
37+
void collectionProperties() {
38+
assertThat(empty().maxDocuments(10).size(1).disableValidation())
39+
.isEqualTo(empty().maxDocuments(10).size(1).disableValidation());
40+
}
41+
42+
@Test // GH-4210
43+
void cappedEquals() {
44+
assertThat(empty().capped()).isNotEqualTo(empty()).isEqualTo(empty().capped());
45+
}
46+
47+
@Test // GH-4210
48+
void collationEquals() {
49+
50+
assertThat(empty().collation(Collation.of("en_US"))) //
51+
.isEqualTo(empty().collation(Collation.of("en_US"))) //
52+
.isNotEqualTo(empty()) //
53+
.isNotEqualTo(empty().collation(Collation.of("de_AT")));
54+
}
55+
56+
@Test // GH-4210
57+
void timeSeriesEquals() {
58+
59+
assertThat(empty().timeSeries(TimeSeriesOptions.timeSeries("tf"))) //
60+
.isEqualTo(empty().timeSeries(TimeSeriesOptions.timeSeries("tf"))) //
61+
.isNotEqualTo(empty()) //
62+
.isNotEqualTo(empty().timeSeries(TimeSeriesOptions.timeSeries("other")));
63+
}
64+
65+
@Test // GH-4210
66+
void validatorEquals() {
67+
68+
assertThat(empty().validator(Validator.document(new Document("one", "two")))) //
69+
.isEqualTo(empty().validator(Validator.document(new Document("one", "two")))) //
70+
.isNotEqualTo(empty()) //
71+
.isNotEqualTo(empty().validator(Validator.document(new Document("three", "four"))))
72+
.isNotEqualTo(empty().validator(Validator.document(new Document("one", "two"))).moderateValidation());
73+
}
74+
}

0 commit comments

Comments
 (0)