File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-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 .net .UnknownHostException ;
21
22
import java .util .ArrayList ;
22
23
import java .util .Collections ;
23
24
import java .util .LinkedHashSet ;
24
25
import java .util .List ;
26
+ import java .util .Set ;
25
27
26
28
import static org .bson .util .Assertions .isTrueArgument ;
27
29
import static org .bson .util .Assertions .notNull ;
@@ -65,7 +67,15 @@ public Builder hosts(final List<ServerAddress> hosts) {
65
67
if (hosts .isEmpty ()) {
66
68
throw new IllegalArgumentException ("hosts list may not be empty" );
67
69
}
68
- this .hosts = Collections .unmodifiableList (new ArrayList <ServerAddress >(new LinkedHashSet <ServerAddress >(hosts )));
70
+ Set <ServerAddress > hostsSet = new LinkedHashSet <ServerAddress >(hosts .size ());
71
+ for (ServerAddress host : hosts ) {
72
+ try {
73
+ hostsSet .add (new ServerAddress (host .getHost (), host .getPort ()));
74
+ } catch (UnknownHostException e ) {
75
+ throw new MongoInternalException ("This can't happen" , e );
76
+ }
77
+ }
78
+ this .hosts = Collections .unmodifiableList (new ArrayList <ServerAddress >(hostsSet ));
69
79
return this ;
70
80
}
71
81
@@ -231,4 +241,4 @@ else if (builder.requiredClusterType != ClusterType.ReplicaSet) {
231
241
requiredReplicaSetName = builder .requiredReplicaSetName ;
232
242
requiredClusterType = builder .requiredClusterType ;
233
243
}
234
- }
244
+ }
Original file line number Diff line number Diff line change @@ -110,4 +110,13 @@ class ClusterSettingsSpecification extends Specification {
110
110
then :
111
111
settings. getHosts() == [new ServerAddress (' server1' ), new ServerAddress (' server2' )]
112
112
}
113
+
114
+ def ' should replace ServerAddress subclass instances with ServerAddress' () {
115
+ when :
116
+ def settings = ClusterSettings . builder(). hosts([new DBAddress (' server1/mydb' ),
117
+ new DBAddress (' server2/mydb' )]). build();
118
+
119
+ then :
120
+ settings. getHosts() == [new ServerAddress (' server1' ), new ServerAddress (' server2' )]
121
+ }
113
122
}
You can’t perform that action at this time.
0 commit comments