Optimize ClientBuilderImpl
* Optimize ClientBuilderImpl:
1. unify argument check logic using Preconditions
2. simplify if else logic on checking serviceUrlProvider
@@ -50,20 +50,16 @@ public class ClientBuilderImpl implements ClientBuilder {
@Override
public PulsarClient build() throws PulsarClientException {
- if (StringUtils.isBlank(conf.getServiceUrl()) && conf.getServiceUrlProvider() == null) {
- throw new IllegalArgumentException(
+ checkArgument(StringUtils.isNotBlank(conf.getServiceUrl()) || conf.getServiceUrlProvider() != null,
"service URL or service URL provider needs to be specified on the ClientBuilder object.");
- }
- if (StringUtils.isNotBlank(conf.getServiceUrl()) && conf.getServiceUrlProvider() != null) {
- throw new IllegalArgumentException("Can only chose one way service URL or service URL provider.");
- }
+ checkArgument(StringUtils.isBlank(conf.getServiceUrl()) || conf.getServiceUrlProvider() == null,
+ "Can only chose one way service URL or service URL provider.");
+
if (conf.getServiceUrlProvider() != null) {
- if (StringUtils.isBlank(conf.getServiceUrlProvider().getServiceUrl())) {
- throw new IllegalArgumentException("Cannot get service url from service url provider.");
- } else {
+ checkArgument(StringUtils.isNotBlank(conf.getServiceUrlProvider().getServiceUrl()),
+ "Cannot get service url from service url provider.");
conf.setServiceUrl(conf.getServiceUrlProvider().getServiceUrl());
}
- }
PulsarClient client = new PulsarClientImpl(conf);
if (conf.getServiceUrlProvider() != null) {
conf.getServiceUrlProvider().initialize(client);
@@ -78,16 +74,13 @@ public class ClientBuilderImpl implements ClientBuilder {
@Override
public ClientBuilder loadConf(Map<String, Object> config) {
- conf = ConfigurationDataUtils.loadData(
- config, conf, ClientConfigurationData.class);
+ conf = ConfigurationDataUtils.loadData(config, conf, ClientConfigurationData.class);
return this;
}
@Override
public ClientBuilder serviceUrl(String serviceUrl) {
- if (StringUtils.isBlank(serviceUrl)) {
- throw new IllegalArgumentException("Param serviceUrl must not be blank.");
- }
+ checkArgument(StringUtils.isNotBlank(serviceUrl), "Param serviceUrl must not be blank.");
conf.setServiceUrl(serviceUrl);
if (!conf.isUseTls()) {
enableTls(serviceUrl.startsWith("pulsar+ssl") || serviceUrl.startsWith("https"));
@@ -97,18 +90,14 @@ public class ClientBuilderImpl implements ClientBuilder {
@Override
public ClientBuilder serviceUrlProvider(ServiceUrlProvider serviceUrlProvider) {
- if (serviceUrlProvider == null) {
- throw new IllegalArgumentException("Param serviceUrlProvider must not be null.");
- }
+ checkArgument(serviceUrlProvider != null, "Param serviceUrlProvider must not be null.");
conf.setServiceUrlProvider(serviceUrlProvider);
return this;
}
@Override
public ClientBuilder listenerName(String listenerName) {
- if (StringUtils.isBlank(listenerName)) {
- throw new IllegalArgumentException("Param listenerName must not be blank.");
- }
+ checkArgument(StringUtils.isNotBlank(listenerName), "Param listenerName must not be blank.");
conf.setListenerName(StringUtils.trim(listenerName));
return this;
}
@@ -323,8 +312,8 @@ public class ClientBuilderImpl implements ClientBuilder {
@Override
public ClientBuilder proxyServiceUrl(String proxyServiceUrl, ProxyProtocol proxyProtocol) {
- if (StringUtils.isNotBlank(proxyServiceUrl) && proxyProtocol == null) {
- throw new IllegalArgumentException("proxyProtocol must be present with proxyServiceUrl");
+ if (StringUtils.isNotBlank(proxyServiceUrl)) {
+ checkArgument(proxyProtocol != null, "proxyProtocol must be present with proxyServiceUrl");
}
conf.setProxyServiceUrl(proxyServiceUrl);
conf.setProxyProtocol(proxyProtocol);
