From 31dfbbf097491065e26b5ee6716421b8f33da9d9 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 27 Aug 2023 21:30:47 +0800 Subject: [PATCH] Use guava Strings.isNullOrEmpty instead of other StringUtils (#28276) * Minor changes for code format * Minor changes for code format * Use guava Strings.isNullOrEmpty instead of other StringUtils --- .../jdbc/core/driver/spi/AbsolutePathURLProvider.java | 4 ++-- .../driver/jdbc/core/driver/spi/ApolloURLProvider.java | 4 ++-- .../driver/jdbc/core/driver/spi/ClasspathURLProvider.java | 4 ++-- .../common/metadata/generator/PipelineDDLGenerator.java | 6 +++--- .../mode/repository/cluster/nacos/NacosRepository.java | 7 +++---- .../mode/repository/cluster/nacos/NacosRepositoryTest.java | 6 +++--- .../standalone/jdbc/sql/JDBCRepositorySQLLoader.java | 5 ++--- .../env/container/atomic/util/DatabaseVersionParser.java | 4 ++-- .../strategy/impl/GitHubTestParameterLoadStrategy.java | 4 ++-- 9 files changed, 21 insertions(+), 23 deletions(-) diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathURLProvider.java index c8950f5482a51..3d95c8fda88ce 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathURLProvider.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathURLProvider.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.driver.jdbc.core.driver.spi; import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import lombok.SneakyThrows; -import org.apache.commons.lang3.StringUtils; import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider; import java.io.BufferedReader; @@ -39,7 +39,7 @@ public final class AbsolutePathURLProvider implements ShardingSphereURLProvider @Override public boolean accept(final String url) { - return StringUtils.isNotBlank(url) && url.contains(PATH_TYPE); + return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE); } @Override diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloURLProvider.java index fe4fb77800ebd..f267cf12f72a7 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloURLProvider.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloURLProvider.java @@ -21,7 +21,7 @@ import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.google.common.base.Preconditions; -import org.apache.commons.lang3.StringUtils; +import com.google.common.base.Strings; import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider; import java.nio.charset.StandardCharsets; @@ -35,7 +35,7 @@ public final class ApolloURLProvider implements ShardingSphereURLProvider { @Override public boolean accept(final String url) { - return StringUtils.isNotBlank(url) && url.contains(APOLLO_TYPE); + return !Strings.isNullOrEmpty(url) && url.contains(APOLLO_TYPE); } @Override diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathURLProvider.java index 181a3e3912e65..d34e395c37c62 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathURLProvider.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathURLProvider.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.driver.jdbc.core.driver.spi; import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import lombok.SneakyThrows; -import org.apache.commons.lang3.StringUtils; import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider; import java.io.BufferedReader; @@ -37,7 +37,7 @@ public final class ClasspathURLProvider implements ShardingSphereURLProvider { @Override public boolean accept(final String url) { - return StringUtils.isNotBlank(url) && url.contains(CLASSPATH_TYPE); + return !Strings.isNullOrEmpty(url) && url.contains(CLASSPATH_TYPE); } @Override diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/generator/PipelineDDLGenerator.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/generator/PipelineDDLGenerator.java index 86ca680a962d0..07e770e28ff08 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/generator/PipelineDDLGenerator.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/generator/PipelineDDLGenerator.java @@ -17,10 +17,9 @@ package org.apache.shardingsphere.data.pipeline.common.metadata.generator; +import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.CreateTableSQLGenerator; -import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.CommentStatementContext; @@ -29,6 +28,7 @@ import org.apache.shardingsphere.infra.binder.context.type.ConstraintAvailable; import org.apache.shardingsphere.infra.binder.context.type.IndexAvailable; import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; +import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtils; @@ -87,7 +87,7 @@ public String generateLogicDDL(final DatabaseType databaseType, final DataSource private Optional decorate(final DatabaseType databaseType, final DataSource dataSource, final String schemaName, final String targetTableName, final SQLParserEngine parserEngine, final String sql) throws SQLException { - if (StringUtils.isBlank(sql)) { + if (Strings.isNullOrEmpty(sql)) { return Optional.empty(); } String databaseName; diff --git a/mode/type/cluster/repository/provider/nacos/src/main/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepository.java b/mode/type/cluster/repository/provider/nacos/src/main/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepository.java index 0426110681b56..89d2ec4f0ccc4 100644 --- a/mode/type/cluster/repository/provider/nacos/src/main/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepository.java +++ b/mode/type/cluster/repository/provider/nacos/src/main/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepository.java @@ -23,7 +23,6 @@ import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.common.utils.CollectionUtils; -import com.alibaba.nacos.common.utils.StringUtils; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import lombok.SneakyThrows; @@ -97,7 +96,7 @@ private void initServiceMetaData() { serviceController = new ServiceController(); for (ServiceMetaData each : serviceController.getAllServices()) { Integer port = client.getAllInstances(each.getServiceName(), false).stream() - .filter(instance -> StringUtils.equals(instance.getIp(), ip)).map(Instance::getPort).max(Comparator.naturalOrder()).orElse(Integer.MIN_VALUE); + .filter(instance -> ip.equals(instance.getIp())).map(Instance::getPort).max(Comparator.naturalOrder()).orElse(Integer.MIN_VALUE); each.setIp(ip); each.setPort(new AtomicInteger(port)); } @@ -292,7 +291,7 @@ public void delete(final String key) { Collection instances = findExistedInstance(each.isEphemeral()).stream() .filter(instance -> { String fullPath = NacosMetaDataUtils.getKey(instance); - return fullPath.startsWith(key + PATH_SEPARATOR) || StringUtils.equals(fullPath, key); + return fullPath.startsWith(key + PATH_SEPARATOR) || key.equals(fullPath); }) .sorted(Comparator.comparing(NacosMetaDataUtils::getKey).reversed()).collect(Collectors.toList()); Collection keyValues = new LinkedList<>(); @@ -339,7 +338,7 @@ private boolean isAvailable(final Collection keyValues) throws NacosEx keyValues.removeIf(keyValue -> { Collection instances = instanceMap.get(keyValue.getKey()); String value = keyValue.getValue(); - return CollectionUtils.isNotEmpty(instances) ? instances.stream().anyMatch(instance -> StringUtils.equals(NacosMetaDataUtils.getValue(instance), value)) : null == value; + return CollectionUtils.isNotEmpty(instances) ? instances.stream().anyMatch(instance -> Objects.equals(NacosMetaDataUtils.getValue(instance), value)) : null == value; }); } return keyValues.isEmpty(); diff --git a/mode/type/cluster/repository/provider/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java b/mode/type/cluster/repository/provider/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java index 02e5f43abbde5..efccc41fb4e2f 100644 --- a/mode/type/cluster/repository/provider/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java +++ b/mode/type/cluster/repository/provider/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java @@ -24,10 +24,9 @@ import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.listener.NamingEvent; import com.alibaba.nacos.api.naming.pojo.Instance; -import com.alibaba.nacos.common.utils.StringUtils; import com.google.common.util.concurrent.SettableFuture; -import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterPersistRepositoryException; import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterPersistRepositoryException; import org.apache.shardingsphere.mode.repository.cluster.nacos.entity.ServiceController; import org.apache.shardingsphere.mode.repository.cluster.nacos.entity.ServiceMetaData; import org.apache.shardingsphere.mode.repository.cluster.nacos.props.NacosProperties; @@ -50,6 +49,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; @@ -361,7 +361,7 @@ private VoidAnswer2 getListenerAnswer(final Instance preI private VoidAnswer2 getRegisterInstanceAnswer() { return (serviceName, instance) -> { List instances = client.getAllInstances(serviceName, false); - instances.removeIf(each -> StringUtils.equals(each.getIp(), instance.getIp()) && each.getPort() == instance.getPort()); + instances.removeIf(each -> Objects.equals(each.getIp(), instance.getIp()) && each.getPort() == instance.getPort()); instances.add(instance); when(client.getAllInstances(serviceName, false)).thenReturn(instances); }; diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java index 6794a0d7ad50f..94b72e46f7933 100644 --- a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java +++ b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java @@ -70,7 +70,7 @@ public static JDBCRepositorySQL load(final String type) { while (resources.hasMoreElements()) { URL resource = resources.nextElement(); result = JAR_URL_PROTOCOLS.contains(resource.getProtocol()) ? loadFromJar(resource, type) : loadFromDirectory(resource, type); - if (null != result && Objects.equals(result.isDefault(), false)) { + if (null != result && !result.isDefault()) { break; } } @@ -85,8 +85,7 @@ private static JDBCRepositorySQL loadFromDirectory(final URL url, final String t @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException { if (file.toString().endsWith(FILE_EXTENSION)) { - JDBCRepositorySQL provider = (JDBCRepositorySQL) JAXBContext.newInstance(JDBCRepositorySQL.class).createUnmarshaller() - .unmarshal(Files.newInputStream(file.toFile().toPath())); + JDBCRepositorySQL provider = (JDBCRepositorySQL) JAXBContext.newInstance(JDBCRepositorySQL.class).createUnmarshaller().unmarshal(Files.newInputStream(file.toFile().toPath())); if (provider.isDefault()) { result[0] = provider; } diff --git a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/util/DatabaseVersionParser.java b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/util/DatabaseVersionParser.java index 98d088c47022b..6c1ce6dd9b039 100644 --- a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/util/DatabaseVersionParser.java +++ b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/util/DatabaseVersionParser.java @@ -17,9 +17,9 @@ package org.apache.shardingsphere.test.e2e.env.container.atomic.util; +import com.google.common.base.Strings; import lombok.AccessLevel; import lombok.NoArgsConstructor; -import org.apache.commons.lang.StringUtils; /** * Database version parser. @@ -34,6 +34,6 @@ public final class DatabaseVersionParser { * @return major version */ public static String parseMajorVersion(final String storageContainerImage) { - return StringUtils.isBlank(storageContainerImage) ? "" : storageContainerImage.substring(storageContainerImage.indexOf(':') + 1, storageContainerImage.indexOf('.')); + return Strings.isNullOrEmpty(storageContainerImage) ? "" : storageContainerImage.substring(storageContainerImage.indexOf(':') + 1, storageContainerImage.indexOf('.')); } } diff --git a/test/util/src/main/java/org/apache/shardingsphere/test/loader/strategy/impl/GitHubTestParameterLoadStrategy.java b/test/util/src/main/java/org/apache/shardingsphere/test/loader/strategy/impl/GitHubTestParameterLoadStrategy.java index 6d50e5377c2f0..0eb522635f8fa 100644 --- a/test/util/src/main/java/org/apache/shardingsphere/test/loader/strategy/impl/GitHubTestParameterLoadStrategy.java +++ b/test/util/src/main/java/org/apache/shardingsphere/test/loader/strategy/impl/GitHubTestParameterLoadStrategy.java @@ -17,10 +17,10 @@ package org.apache.shardingsphere.test.loader.strategy.impl; +import com.google.common.base.Strings; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.apache.shardingsphere.test.loader.strategy.TestParameterLoadStrategy; import org.apache.shardingsphere.test.loader.summary.FileSummary; @@ -82,7 +82,7 @@ private URI getGitHubApiUri(final URI sqlCaseURI) { private String loadContent(final URI casesURI) { try { URLConnection urlConnection = casesURI.toURL().openConnection(); - if (StringUtils.isNotBlank(GitHubEnvironment.getInstance().getGithubToken())) { + if (!Strings.isNullOrEmpty(GitHubEnvironment.getInstance().getGithubToken())) { urlConnection.setRequestProperty("Authorization", "Bearer " + GitHubEnvironment.getInstance().getGithubToken()); } try (BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {