23
23
import com .datastax .oss .driver .api .core .loadbalancing .LocalDcAwareLoadBalancingPolicy ;
24
24
import com .datastax .oss .driver .api .core .session .Session ;
25
25
import com .datastax .oss .driver .api .core .uuid .Uuids ;
26
+ import com .datastax .oss .driver .shaded .guava .common .base .Joiner ;
27
+ import com .datastax .oss .driver .shaded .guava .common .collect .Maps ;
26
28
import com .datastax .oss .protocol .internal .request .Startup ;
27
29
import com .datastax .oss .protocol .internal .util .collection .NullAllowingImmutableMap ;
28
30
import edu .umd .cs .findbugs .annotations .Nullable ;
29
31
import java .util .Map ;
32
+ import java .util .Optional ;
30
33
import java .util .UUID ;
34
+ import java .util .stream .Collectors ;
31
35
import net .jcip .annotations .Immutable ;
32
36
33
37
@ Immutable
@@ -123,10 +127,7 @@ public Map<String, String> build() {
123
127
builder .put (APPLICATION_VERSION_KEY , applicationVersion );
124
128
}
125
129
// do not cache local DC as it can change within LBP implementation
126
- String applicationLocalDcs = localDcs ();
127
- if (applicationLocalDcs != null ) {
128
- builder .put (DRIVER_LOCAL_DC , applicationLocalDcs );
129
- }
130
+ localDcs ().ifPresent (s -> builder .put (DRIVER_LOCAL_DC , s ));
130
131
131
132
return builder .build ();
132
133
}
@@ -151,28 +152,27 @@ protected String getDriverVersion() {
151
152
return Session .OSS_DRIVER_COORDINATES .getVersion ().toString ();
152
153
}
153
154
154
- private String localDcs () {
155
- StringBuilder result = new StringBuilder ();
156
- boolean first = true ;
157
- for (Map .Entry <String , LoadBalancingPolicy > entry :
158
- context .getLoadBalancingPolicies ().entrySet ()) {
159
- String dc = getLocalDc (entry .getValue ());
160
- if (dc != null ) {
161
- if (!first ) {
162
- result .append (", " );
163
- } else {
164
- first = false ;
165
- }
166
- result .append (entry .getKey ()).append (": " ).append (dc );
167
- }
155
+ private Optional <String > localDcs () {
156
+ Joiner joiner = Joiner .on (": " );
157
+ Map <String , Optional <String >> lbpToDc =
158
+ Maps .transformValues (context .getLoadBalancingPolicies (), this ::getLocalDc );
159
+ if (lbpToDc .isEmpty ()) {
160
+ return Optional .empty ();
168
161
}
169
- return first ? null : result .toString ();
162
+ return Optional .of (
163
+ lbpToDc .entrySet ().stream ()
164
+ .filter (e -> e .getValue ().isPresent ())
165
+ .map (entry -> joiner .join (entry .getKey (), entry .getValue ().get ()))
166
+ .collect (Collectors .joining (", " )));
170
167
}
171
168
172
- private String getLocalDc (LoadBalancingPolicy loadBalancingPolicy ) {
169
+ private Optional < String > getLocalDc (LoadBalancingPolicy loadBalancingPolicy ) {
173
170
if (loadBalancingPolicy instanceof LocalDcAwareLoadBalancingPolicy ) {
174
- return ((LocalDcAwareLoadBalancingPolicy ) loadBalancingPolicy ).getLocalDatacenter ();
171
+ String dc = ((LocalDcAwareLoadBalancingPolicy ) loadBalancingPolicy ).getLocalDatacenter ();
172
+ if (dc != null ) {
173
+ return Optional .of (dc );
174
+ }
175
175
}
176
- return null ;
176
+ return Optional . empty () ;
177
177
}
178
178
}
0 commit comments