20
20
import com .mongodb .ConnectionString ;
21
21
import com .mongodb .annotations .Immutable ;
22
22
23
+ import java .util .Objects ;
23
24
import java .util .concurrent .TimeUnit ;
24
25
25
26
import static com .mongodb .assertions .Assertions .notNull ;
27
+ import static java .lang .Math .toIntExact ;
26
28
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
27
29
28
30
/**
32
34
*/
33
35
@ Immutable
34
36
public final class SocketSettings {
35
- private final long connectTimeoutMS ;
36
- private final long readTimeoutMS ;
37
+ private final int connectTimeoutMS ;
38
+ private final int readTimeoutMS ;
37
39
private final int receiveBufferSize ;
38
40
private final int sendBufferSize ;
39
41
private final ProxySettings proxySettings ;
@@ -62,8 +64,8 @@ public static Builder builder(final SocketSettings socketSettings) {
62
64
* A builder for an instance of {@code SocketSettings}.
63
65
*/
64
66
public static final class Builder {
65
- private long connectTimeoutMS = 10000 ;
66
- private long readTimeoutMS ;
67
+ private int connectTimeoutMS = 10000 ;
68
+ private int readTimeoutMS ;
67
69
private int receiveBufferSize ;
68
70
private int sendBufferSize ;
69
71
private ProxySettings .Builder proxySettingsBuilder = ProxySettings .builder ();
@@ -93,25 +95,27 @@ public Builder applySettings(final SocketSettings socketSettings) {
93
95
/**
94
96
* Sets the socket connect timeout.
95
97
*
96
- * @param connectTimeout the connect timeout
98
+ * @param connectTimeout the connect timeout.
99
+ * The timeout converted to milliseconds must not be greater than {@link Integer#MAX_VALUE}.
97
100
* @param timeUnit the time unit
98
101
* @return this
99
102
*/
100
- public Builder connectTimeout (final int connectTimeout , final TimeUnit timeUnit ) {
101
- this .connectTimeoutMS = MILLISECONDS . convert (connectTimeout , timeUnit );
103
+ public Builder connectTimeout (final long connectTimeout , final TimeUnit timeUnit ) {
104
+ this .connectTimeoutMS = timeoutArgumentToMillis (connectTimeout , timeUnit );
102
105
return this ;
103
106
}
104
107
105
108
/**
106
109
* Sets the socket read timeout.
107
110
*
108
- * @param readTimeout the read timeout
111
+ * @param readTimeout the read timeout.
112
+ * The timeout converted to milliseconds must not be greater than {@link Integer#MAX_VALUE}.
109
113
* @param timeUnit the time unit
110
114
* @return this
111
115
* @see #getReadTimeout(TimeUnit)
112
116
*/
113
- public Builder readTimeout (final int readTimeout , final TimeUnit timeUnit ) {
114
- this .readTimeoutMS = MILLISECONDS . convert (readTimeout , timeUnit );
117
+ public Builder readTimeout (final long readTimeout , final TimeUnit timeUnit ) {
118
+ this .readTimeoutMS = timeoutArgumentToMillis (readTimeout , timeUnit );
115
119
return this ;
116
120
}
117
121
@@ -197,7 +201,7 @@ public int getConnectTimeout(final TimeUnit timeUnit) {
197
201
*
198
202
* @param timeUnit the time unit to get the timeout in
199
203
* @return the read timeout in the requested time unit, or 0 if there is no timeout
200
- * @see Builder#readTimeout(int , TimeUnit)
204
+ * @see Builder#readTimeout(long , TimeUnit)
201
205
*/
202
206
public int getReadTimeout (final TimeUnit timeUnit ) {
203
207
return (int ) timeUnit .convert (readTimeoutMS , MILLISECONDS );
@@ -260,12 +264,7 @@ public boolean equals(final Object o) {
260
264
261
265
@ Override
262
266
public int hashCode () {
263
- int result = (int ) (connectTimeoutMS ^ (connectTimeoutMS >>> 32 ));
264
- result = 31 * result + (int ) (readTimeoutMS ^ (readTimeoutMS >>> 32 ));
265
- result = 31 * result + receiveBufferSize ;
266
- result = 31 * result + sendBufferSize ;
267
- result = 31 * result + proxySettings .hashCode ();
268
- return result ;
267
+ return Objects .hash (connectTimeoutMS , readTimeoutMS , receiveBufferSize , sendBufferSize , proxySettings );
269
268
}
270
269
271
270
@ Override
@@ -285,4 +284,13 @@ private SocketSettings(final Builder builder) {
285
284
sendBufferSize = builder .sendBufferSize ;
286
285
proxySettings = builder .proxySettingsBuilder .build ();
287
286
}
287
+
288
+ private static int timeoutArgumentToMillis (final long timeout , final TimeUnit timeUnit ) throws IllegalArgumentException {
289
+ try {
290
+ return toIntExact (MILLISECONDS .convert (timeout , timeUnit ));
291
+ } catch (ArithmeticException e ) {
292
+ throw new IllegalArgumentException (
293
+ "The timeout converted to milliseconds must not be greater than `Integer.MAX_VALUE`" , e );
294
+ }
295
+ }
288
296
}
0 commit comments