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 1818
1919import org .bson .util .annotations .Immutable ;
2020
21+ import java .net .UnknownHostException ;
2122import java .util .ArrayList ;
2223import java .util .Collections ;
2324import java .util .LinkedHashSet ;
2425import java .util .List ;
26+ import java .util .Set ;
2527
2628import static org .bson .util .Assertions .isTrueArgument ;
2729import static org .bson .util .Assertions .notNull ;
@@ -65,7 +67,15 @@ public Builder hosts(final List<ServerAddress> hosts) {
6567 if (hosts .isEmpty ()) {
6668 throw new IllegalArgumentException ("hosts list may not be empty" );
6769 }
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 ));
6979 return this ;
7080 }
7181
@@ -231,4 +241,4 @@ else if (builder.requiredClusterType != ClusterType.ReplicaSet) {
231241 requiredReplicaSetName = builder .requiredReplicaSetName ;
232242 requiredClusterType = builder .requiredClusterType ;
233243 }
234- }
244+ }
Original file line number Diff line number Diff line change @@ -110,4 +110,13 @@ class ClusterSettingsSpecification extends Specification {
110110 then :
111111 settings. getHosts() == [new ServerAddress (' server1' ), new ServerAddress (' server2' )]
112112 }
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+ }
113122}
You can’t perform that action at this time.
0 commit comments