From 9a201942925ddceb3df1d31d8b266a20eb9b9756 Mon Sep 17 00:00:00 2001 From: arrow2020 <316166287@qq.com> Date: Tue, 30 Dec 2025 03:02:24 +0800 Subject: [PATCH] * Refactor: string checks with idiomatic alternatives --- .../apollo/internals/RemoteConfigLongPollService.java | 2 +- .../framework/apollo/internals/RemoteConfigRepository.java | 2 +- .../ctrip/framework/apollo/util/http/DefaultHttpClient.java | 2 +- .../com/ctrip/framework/apollo/util/yaml/YamlParser.java | 6 +++--- .../com/ctrip/framework/apollo/core/MetaDomainConsts.java | 4 +--- .../ctrip/framework/apollo/core/signature/Signature.java | 2 +- .../framework/apollo/core/utils/ApolloThreadFactory.java | 2 +- .../ctrip/framework/apollo/core/utils/ResourceUtils.java | 2 +- .../com/ctrip/framework/apollo/core/utils/StringUtils.java | 2 +- .../com/ctrip/framework/apollo/openapi/dto/OpenPageDTO.java | 2 +- 10 files changed, 12 insertions(+), 14 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java index 4f52575b..1203b6de 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java @@ -346,7 +346,7 @@ private ServiceDTO resolveConfigService() { private List getConfigServices() { List services = m_serviceLocator.getConfigServices(); - if (services.size() == 0) { + if (services.isEmpty()) { throw new ApolloConfigException("No available config service"); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index 3f6f794c..cdcf3d0b 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -379,7 +379,7 @@ public void run() { private List getConfigServices() { List services = m_serviceLocator.getConfigServices(); - if (services.size() == 0) { + if (services.isEmpty()) { throw new ApolloConfigException("No available config service"); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/DefaultHttpClient.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/DefaultHttpClient.java index 5d5fb40e..722035f9 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/DefaultHttpClient.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/DefaultHttpClient.java @@ -97,7 +97,7 @@ private HttpResponse doGetWithSerializeFunction(HttpRequest httpRequest, conn.setRequestMethod("GET"); Map headers = httpRequest.getHeaders(); - if (headers != null && headers.size() > 0) { + if (headers != null && !headers.isEmpty()) { for (Map.Entry entry : headers.entrySet()) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java index b3e9e570..e9fad541 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java @@ -73,7 +73,7 @@ private Yaml createYaml() { private boolean process(MatchCallback callback, Yaml yaml, String content) { int count = 0; if (logger.isDebugEnabled()) { - logger.debug("Loading from YAML: " + content); + logger.debug("Loading from YAML: {}", content); } for (Object object : yaml.loadAll(content)) { if (object != null && process(asMap(object), callback)) { @@ -81,7 +81,7 @@ private boolean process(MatchCallback callback, Yaml yaml, String content) { } } if (logger.isDebugEnabled()) { - logger.debug("Loaded " + count + " document" + (count > 1 ? "s" : "") + " from YAML resource: " + content); + logger.debug("Loaded {} document{} from YAML resource: {}", count, count > 1 ? "s" : "", content); } return (count > 0); } @@ -118,7 +118,7 @@ private boolean process(Map map, MatchCallback callback) { properties.putAll(getFlattenedMap(map)); if (logger.isDebugEnabled()) { - logger.debug("Merging document (no matchers set): " + map); + logger.debug("Merging document (no matchers set): {}", map); } callback.process(properties, map); return true; diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java index aa905654..da4ab4a4 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java @@ -222,9 +222,7 @@ public void run() { updateMetaServerAddresses(metaServerAddresses); } } catch (Throwable ex) { - logger - .warn(String.format("Refreshing meta server address failed, will retry in %d seconds", - REFRESH_INTERVAL_IN_SECOND), ex); + logger.warn("Refreshing meta server address failed, will retry in {} seconds", REFRESH_INTERVAL_IN_SECOND, ex); } } }, REFRESH_INTERVAL_IN_SECOND, REFRESH_INTERVAL_IN_SECOND, TimeUnit.SECONDS); diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/signature/Signature.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/signature/Signature.java index 2c992423..9aad9078 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/signature/Signature.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/signature/Signature.java @@ -60,7 +60,7 @@ private static String url2PathWithQuery(String urlString) { String query = url.getQuery(); String pathWithQuery = path; - if (query != null && query.length() > 0) { + if (query != null && !query.isEmpty()) { pathWithQuery += "?" + query; } return pathWithQuery; diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ApolloThreadFactory.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ApolloThreadFactory.java index c4e67a79..dcbc0963 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ApolloThreadFactory.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ApolloThreadFactory.java @@ -60,7 +60,7 @@ public boolean satisfy(Thread thread) { return !thread.isAlive() || thread.isInterrupted() || thread.isDaemon(); } }); - if (alives.size() > 0) { + if (!alives.isEmpty()) { log.info("Alive apollo threads: {}", alives); try { TimeUnit.SECONDS.sleep(2); diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ResourceUtils.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ResourceUtils.java index 2c1dca0a..885da9c9 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ResourceUtils.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ResourceUtils.java @@ -65,7 +65,7 @@ public static Properties readConfigFile(String configPath, Properties defaults) } if (sb.length() > 0) { - logger.debug("Reading properties: \n" + sb); + logger.debug("Reading properties: \n{}", sb); } else { logger.warn("No available properties: {}", configPath); } diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/StringUtils.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/StringUtils.java index a223374b..09bbb780 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/StringUtils.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/StringUtils.java @@ -41,7 +41,7 @@ public class StringUtils { * @return true if the String is empty or null */ public static boolean isEmpty(String str) { - return str == null || str.length() == 0; + return str == null || str.isEmpty(); } diff --git a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/dto/OpenPageDTO.java b/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/dto/OpenPageDTO.java index 4b4678cb..a1755b8d 100644 --- a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/dto/OpenPageDTO.java +++ b/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/dto/OpenPageDTO.java @@ -53,7 +53,7 @@ public List getContent() { } public boolean hasContent() { - return content != null && content.size() > 0; + return content != null && !content.isEmpty(); } }