1919import com .mongodb .annotations .Immutable ;
2020import com .mongodb .lang .Nullable ;
2121
22- import jnr .unixsocket .UnixSocketAddress ;
23-
24- import java .io .File ;
2522import java .io .Serializable ;
2623import java .net .InetAddress ;
2724import java .net .InetSocketAddress ;
28- import java .net .SocketAddress ;
2925import java .net .UnknownHostException ;
3026
3127/**
@@ -37,7 +33,6 @@ public class ServerAddress implements Serializable {
3733
3834 private final String host ;
3935 private final int port ;
40- private final SocketAddress address ;
4136
4237 /**
4338 * Creates a ServerAddress with default host and port
@@ -61,7 +56,7 @@ public ServerAddress(@Nullable final String host) {
6156 * @param inetAddress host address
6257 */
6358 public ServerAddress (final InetAddress inetAddress ) {
64- this (inetAddress .getHostName (), defaultPort (), new InetSocketAddress ( inetAddress , defaultPort ()) );
59+ this (inetAddress .getHostName (), defaultPort ());
6560 }
6661
6762 /**
@@ -71,7 +66,7 @@ public ServerAddress(final InetAddress inetAddress) {
7166 * @param port mongod port
7267 */
7368 public ServerAddress (final InetAddress inetAddress , final int port ) {
74- this (inetAddress .getHostName (), port , new InetSocketAddress ( inetAddress , port ) );
69+ this (inetAddress .getHostName (), port );
7570 }
7671
7772 /**
@@ -80,38 +75,7 @@ public ServerAddress(final InetAddress inetAddress, final int port) {
8075 * @param inetSocketAddress inet socket address containing hostname and port
8176 */
8277 public ServerAddress (final InetSocketAddress inetSocketAddress ) {
83- this (inetSocketAddress .getHostName (), inetSocketAddress .getPort (), inetSocketAddress );
84- }
85-
86- /**
87- * Creates a ServerAddress
88- *
89- * @param serverAddress an instance to be shallow-copied
90- */
91- public ServerAddress (final ServerAddress serverAddress ) {
92- this (serverAddress .host , serverAddress .port , serverAddress .address );
93- }
94-
95- /**
96- * Creates a ServerAddress
97- *
98- * @param path the file used for the Unix domain socket
99- */
100- public ServerAddress (final File path ) {
101- this (path .toString (), 0 , new UnixSocketAddress (path ));
102- }
103-
104- /**
105- * Creates a ServerAddress - intended for internal usage
106- *
107- * @param host hostname
108- * @param port mongod port
109- * @param address an instance of socket address or `null`
110- */
111- protected ServerAddress (final String host , final int port , final SocketAddress address ) {
112- this .host = host ;
113- this .port = port ;
114- this .address = address ;
78+ this (inetSocketAddress .getAddress (), inetSocketAddress .getPort ());
11579 }
11680
11781 /**
@@ -121,29 +85,31 @@ protected ServerAddress(final String host, final int port, final SocketAddress a
12185 * @param port mongod port
12286 */
12387 public ServerAddress (@ Nullable final String host , final int port ) {
124- String hostToUse = host == null ? defaultHost () : host ;
88+ String hostToUse = host ;
89+ if (hostToUse == null ) {
90+ hostToUse = defaultHost ();
91+ }
12592 hostToUse = hostToUse .trim ();
12693 if (hostToUse .length () == 0 ) {
12794 hostToUse = defaultHost ();
12895 }
129-
13096 int portToUse = port ;
13197
13298 if (hostToUse .startsWith ("[" )) {
133- int idx = hostToUse .indexOf ("]" );
99+ int idx = host .indexOf ("]" );
134100 if (idx == -1 ) {
135101 throw new IllegalArgumentException ("an IPV6 address must be encosed with '[' and ']'"
136102 + " according to RFC 2732." );
137103 }
138104
139- int portIdx = hostToUse .indexOf ("]:" );
105+ int portIdx = host .indexOf ("]:" );
140106 if (portIdx != -1 ) {
141107 if (port != defaultPort ()) {
142108 throw new IllegalArgumentException ("can't specify port in construct and via host" );
143109 }
144- portToUse = Integer .parseInt (hostToUse .substring (portIdx + 2 ));
110+ portToUse = Integer .parseInt (host .substring (portIdx + 2 ));
145111 }
146- hostToUse = hostToUse .substring (1 , idx );
112+ hostToUse = host .substring (1 , idx );
147113 } else {
148114 int idx = hostToUse .indexOf (":" );
149115 int lastIdx = hostToUse .lastIndexOf (":" );
@@ -161,7 +127,6 @@ public ServerAddress(@Nullable final String host, final int port) {
161127 }
162128 this .host = hostToUse .toLowerCase ();
163129 this .port = portToUse ;
164- this .address = null ;
165130 }
166131
167132 @ Override
@@ -216,10 +181,7 @@ public int getPort() {
216181 *
217182 * @return socket address
218183 */
219- public SocketAddress getSocketAddress () {
220- if (address != null ) {
221- return address ;
222- }
184+ public InetSocketAddress getSocketAddress () {
223185 try {
224186 return new InetSocketAddress (InetAddress .getByName (host ), port );
225187 } catch (UnknownHostException e ) {
0 commit comments