File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change 18
18
19
19
import org .bson .util .annotations .Immutable ;
20
20
21
+ import java .util .ArrayList ;
21
22
import java .util .Collections ;
23
+ import java .util .LinkedHashSet ;
22
24
import java .util .List ;
23
25
24
26
import static org .bson .util .Assertions .isTrueArgument ;
@@ -53,14 +55,17 @@ private Builder() {
53
55
// CHECKSTYLE:OFF
54
56
55
57
/**
56
- * Sets the hosts for the cluster.
58
+ * Sets the hosts for the cluster. And duplicate server addresses are removed from the list.
57
59
*
58
60
* @param hosts the seed list of hosts
59
61
* @return this
60
62
*/
61
63
public Builder hosts (final List <ServerAddress > hosts ) {
62
64
notNull ("hosts" , hosts );
63
- this .hosts = Collections .unmodifiableList (hosts );
65
+ if (hosts .isEmpty ()) {
66
+ throw new IllegalArgumentException ("hosts list may not be empty" );
67
+ }
68
+ this .hosts = Collections .unmodifiableList (new ArrayList <ServerAddress >(new LinkedHashSet <ServerAddress >(hosts )));
64
69
return this ;
65
70
}
66
71
Original file line number Diff line number Diff line change @@ -84,4 +84,30 @@ class ClusterSettingsSpecification extends Specification {
84
84
then :
85
85
thrown(IllegalArgumentException )
86
86
}
87
+
88
+ def ' should throws if hosts list is null' () {
89
+ when :
90
+ ClusterSettings . builder(). hosts(null ). build();
91
+
92
+ then :
93
+ thrown(IllegalArgumentException )
94
+ }
95
+
96
+ def ' should throws if hosts list is empty' () {
97
+ when :
98
+ ClusterSettings . builder(). hosts([]). build();
99
+
100
+ then :
101
+ thrown(IllegalArgumentException )
102
+ }
103
+
104
+ def ' should remove duplicate hosts' () {
105
+ when :
106
+ def settings = ClusterSettings . builder(). hosts([new ServerAddress (' server1' ),
107
+ new ServerAddress (' server2' ),
108
+ new ServerAddress (' server1' )]). build();
109
+
110
+ then :
111
+ settings. getHosts() == [new ServerAddress (' server1' ), new ServerAddress (' server2' )]
112
+ }
87
113
}
You can’t perform that action at this time.
0 commit comments