1818 */
1919package com .treasuredata .client ;
2020
21- import com .google .common .base .Optional ;
2221import com .google .common .collect .ImmutableMultimap ;
2322import com .google .common .collect .Multimap ;
2423
2524import java .util .Properties ;
25+ import java .util .Optional ;
26+ import java .util .stream .Stream ;
2627
2728import static com .treasuredata .client .TDClientConfig .ENV_TD_CLIENT_APIKEY ;
2829import static com .treasuredata .client .TDClientConfig .Type .APIKEY ;
5051 */
5152public abstract class AbstractTDClientBuilder <ClientImpl , BuilderImpl extends AbstractTDClientBuilder <ClientImpl , BuilderImpl >>
5253{
53- protected Optional <String > endpoint = Optional .absent ();
54- protected Optional <Integer > port = Optional .absent ();
54+ protected Optional <String > endpoint = Optional .empty ();
55+ protected Optional <Integer > port = Optional .empty ();
5556 protected boolean useSSL = true ;
56- protected Optional <String > apiKey = Optional .absent ();
57- protected Optional <String > user = Optional .absent ();
58- protected Optional <String > password = Optional .absent ();
59- protected Optional <ProxyConfig > proxy = Optional .absent ();
57+ protected Optional <String > apiKey = Optional .empty ();
58+ protected Optional <String > user = Optional .empty ();
59+ protected Optional <String > password = Optional .empty ();
60+ protected Optional <ProxyConfig > proxy = Optional .empty ();
6061 protected BackOffStrategy retryStrategy = BackOffStrategy .FullJitter ;
6162 protected int retryLimit = 7 ;
6263 protected int retryInitialIntervalMillis = 500 ;
@@ -74,7 +75,7 @@ private static Optional<String> getConfigProperty(Properties p, TDClientConfig.T
7475
7576 private static Optional <String > getConfigProperty (Properties p , String key )
7677 {
77- return Optional .fromNullable (p .getProperty (key ));
78+ return Optional .ofNullable (p .getProperty (key ));
7879 }
7980
8081 private static Optional <Integer > getConfigPropertyInt (Properties p , TDClientConfig .Type key )
@@ -94,7 +95,7 @@ private static Optional<Integer> getConfigPropertyInt(Properties p, String key)
9495 }
9596 }
9697 else {
97- return Optional .absent ();
98+ return Optional .empty ();
9899 }
99100 }
100101
@@ -115,7 +116,7 @@ private static Optional<Boolean> getConfigPropertyBoolean(Properties p, String k
115116 }
116117 }
117118 else {
118- return Optional .absent ();
119+ return Optional .empty ();
119120 }
120121 }
121122
@@ -131,7 +132,7 @@ private static Optional<Double> getConfigPropertyDouble(Properties p, TDClientCo
131132 }
132133 }
133134 else {
134- return Optional .absent ();
135+ return Optional .empty ();
135136 }
136137 }
137138
@@ -168,24 +169,25 @@ protected AbstractTDClientBuilder(boolean loadTDConf)
168169 */
169170 public BuilderImpl setProperties (Properties p )
170171 {
171- this .endpoint = getConfigProperty (p , API_ENDPOINT )
172- .or (getConfigProperty (p , "endpoint" ))
173- .or (endpoint );
174- this .port = getConfigPropertyInt (p , API_PORT )
175- .or (getConfigPropertyInt (p , "port" ))
176- .or (port );
177- this .useSSL = getConfigPropertyBoolean (p , USESSL )
178- .or (getConfigPropertyBoolean (p , "usessl" ))
179- .or (useSSL );
180- this .apiKey = getConfigProperty (p , APIKEY )
181- .or (getConfigProperty (p , "apikey" ))
182- .or (apiKey );
183- this .user = getConfigProperty (p , USER )
184- .or (getConfigProperty (p , "user" ))
185- .or (user );
186- this .password = getConfigProperty (p , PASSOWRD )
187- .or (getConfigProperty (p , "password" ))
188- .or (password );
172+ this .endpoint = Stream .of (getConfigProperty (p , API_ENDPOINT ), getConfigProperty (p , "endpoint" ), endpoint )
173+ .flatMap ((opt ) -> opt .map (Stream ::of ).orElseGet (Stream ::empty ))
174+ .findFirst ();
175+ this .port = Stream .of (getConfigPropertyInt (p , API_PORT ), getConfigPropertyInt (p , "port" ), port )
176+ .flatMap ((opt ) -> opt .map (Stream ::of ).orElseGet (Stream ::empty ))
177+ .findFirst ();
178+ this .useSSL = Stream .of (getConfigPropertyBoolean (p , USESSL ), getConfigPropertyBoolean (p , "usessl" ))
179+ .flatMap ((opt ) -> opt .map (Stream ::of ).orElseGet (Stream ::empty ))
180+ .findFirst ()
181+ .orElse (useSSL );
182+ this .apiKey = Stream .of (getConfigProperty (p , APIKEY ), getConfigProperty (p , "apikey" ), apiKey )
183+ .flatMap ((opt ) -> opt .map (Stream ::of ).orElseGet (Stream ::empty ))
184+ .findFirst ();
185+ this .user = Stream .of (getConfigProperty (p , USER ), getConfigProperty (p , "user" ), user )
186+ .flatMap ((opt ) -> opt .map (Stream ::of ).orElseGet (Stream ::empty ))
187+ .findFirst ();
188+ this .password = Stream .of (getConfigProperty (p , PASSOWRD ), getConfigProperty (p , "password" ), password )
189+ .flatMap ((opt ) -> opt .map (Stream ::of ).orElseGet (Stream ::empty ))
190+ .findFirst ();
189191
190192 // proxy
191193 boolean hasProxy = false ;
@@ -222,16 +224,16 @@ public BuilderImpl setProperties(Properties p)
222224 hasProxy = true ;
223225 proxyConfig .setPassword (proxyPassword .get ());
224226 }
225- this .proxy = Optional .fromNullable (hasProxy ? proxyConfig .createProxyConfig () : null );
227+ this .proxy = Optional .ofNullable (hasProxy ? proxyConfig .createProxyConfig () : null );
226228
227229 // http client parameter
228- this .retryLimit = getConfigPropertyInt (p , RETRY_LIMIT ).or (retryLimit );
229- this .retryInitialIntervalMillis = getConfigPropertyInt (p , RETRY_INITIAL_INTERVAL_MILLIS ).or (retryInitialIntervalMillis );
230- this .retryMaxIntervalMillis = getConfigPropertyInt (p , RETRY_MAX_INTERVAL_MILLIS ).or (retryMaxIntervalMillis );
231- this .retryMultiplier = getConfigPropertyDouble (p , RETRY_MULTIPLIER ).or (retryMultiplier );
232- this .connectTimeoutMillis = getConfigPropertyInt (p , CONNECT_TIMEOUT_MILLIS ).or (connectTimeoutMillis );
233- this .readTimeoutMillis = getConfigPropertyInt (p , READ_TIMEOUT_MILLIS ).or (readTimeoutMillis );
234- this .connectionPoolSize = getConfigPropertyInt (p , CONNECTION_POOL_SIZE ).or (connectionPoolSize );
230+ this .retryLimit = getConfigPropertyInt (p , RETRY_LIMIT ).orElse (retryLimit );
231+ this .retryInitialIntervalMillis = getConfigPropertyInt (p , RETRY_INITIAL_INTERVAL_MILLIS ).orElse (retryInitialIntervalMillis );
232+ this .retryMaxIntervalMillis = getConfigPropertyInt (p , RETRY_MAX_INTERVAL_MILLIS ).orElse (retryMaxIntervalMillis );
233+ this .retryMultiplier = getConfigPropertyDouble (p , RETRY_MULTIPLIER ).orElse (retryMultiplier );
234+ this .connectTimeoutMillis = getConfigPropertyInt (p , CONNECT_TIMEOUT_MILLIS ).orElse (connectTimeoutMillis );
235+ this .readTimeoutMillis = getConfigPropertyInt (p , READ_TIMEOUT_MILLIS ).orElse (readTimeoutMillis );
236+ this .connectionPoolSize = getConfigPropertyInt (p , CONNECTION_POOL_SIZE ).orElse (connectionPoolSize );
235237
236238 return self ();
237239 }
0 commit comments