From 1c8425300cd797d8177c02121ce1c760ccc67067 Mon Sep 17 00:00:00 2001 From: lin-mt <58337359+lin-mt@users.noreply.github.com> Date: Tue, 23 Jun 2020 22:20:28 +0800 Subject: [PATCH] replace lang3 in api (#3126) * replace lang3 * style:remove this --- .../nacos/api/exception/NacosException.java | 255 +++++++++--------- .../nacos/api/naming/pojo/Instance.java | 208 +++++++------- .../naming/pojo/healthcheck/impl/Http.java | 69 ++--- .../naming/pojo/healthcheck/impl/Mysql.java | 64 ++--- .../nacos/api/naming/utils/NamingUtils.java | 24 +- .../alibaba/nacos/api/utils/StringUtils.java | 187 +++++++++++++ .../api/naming/pojo/ServiceInfoTest.java | 4 +- .../AbstractHealthCheckerTest.java | 2 +- .../healthcheck/HealthCheckerFactoryTest.java | 5 +- .../pojo/healthcheck/impl/MysqlTest.java | 5 +- 10 files changed, 513 insertions(+), 310 deletions(-) create mode 100644 api/src/main/java/com/alibaba/nacos/api/utils/StringUtils.java diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index d13e1157d..be9b6af2c 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -13,136 +13,145 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.api.exception; import com.alibaba.nacos.api.common.Constants; -import org.apache.commons.lang3.StringUtils; +import com.alibaba.nacos.api.utils.StringUtils; /** - * Nacos Exception + * Nacos Exception. * * @author Nacos */ public class NacosException extends Exception { - - /** - * serialVersionUID - */ - private static final long serialVersionUID = -3913902031489277776L; - - private int errCode; - - private String errMsg; - - private Throwable causeThrowable; - - public NacosException() { - } - - public NacosException(int errCode, String errMsg) { - super(errMsg); - this.errCode = errCode; - this.errMsg = errMsg; - } - - public NacosException(int errCode, Throwable throwable) { - super(throwable); - this.errCode = errCode; - setCauseThrowable(throwable); - } - - public NacosException(int errCode, String errMsg, Throwable throwable) { - super(errMsg, throwable); - this.errCode = errCode; - this.errMsg = errMsg; - setCauseThrowable(throwable); - } - - public int getErrCode() { - return errCode; - } - - public String getErrMsg() { - if (!StringUtils.isBlank(this.errMsg)) { - return errMsg; - } - if (this.causeThrowable != null) { - return causeThrowable.getMessage(); - } - return Constants.NULL; - } - - public void setErrCode(int errCode) { - this.errCode = errCode; - } - - public void setErrMsg(String errMsg) { - this.errMsg = errMsg; - } - - public void setCauseThrowable(Throwable throwable) { - this.causeThrowable = getCauseThrowable(throwable); - } - - private Throwable getCauseThrowable(Throwable t) { - if (t.getCause() == null) { - return t; - } - return getCauseThrowable(t.getCause()); - } - - @Override - public String toString() { - return "ErrCode:" + getErrCode() + ", ErrMsg:" + getErrMsg(); - } - - /** - * client error code - * -400 -503 throw exception to user - */ - /** - * invalid param(参数错误) - */ - public static final int CLIENT_INVALID_PARAM = -400; - /** - * over client threshold(超过server端的限流阈值) - */ - public static final int CLIENT_OVER_THRESHOLD = -503; - - /** - * server error code - * 400 403 throw exception to user - * 500 502 503 change ip and retry - */ - - /** - * invalid param(参数错误) - */ - public static final int INVALID_PARAM = 400; - /** - * no right(鉴权失败) - */ - public static final int NO_RIGHT = 403; - /** - * not found - */ - public static final int NOT_FOUND = 404; - /** - * conflict(写并发冲突) - */ - public static final int CONFLICT = 409; - /** - * server error(server异常,如超时) - */ - public static final int SERVER_ERROR = 500; - /** - * bad gateway(路由异常,如nginx后面的Server挂掉) - */ - public static final int BAD_GATEWAY = 502; - /** - * over threshold(超过server端的限流阈值) - */ - public static final int OVER_THRESHOLD = 503; - - public static final int RESOURCE_NOT_FOUND = -404; + + /** + * serialVersionUID. + */ + private static final long serialVersionUID = -3913902031489277776L; + + private int errCode; + + private String errMsg; + + private Throwable causeThrowable; + + public NacosException() { + } + + public NacosException(final int errCode, final String errMsg) { + super(errMsg); + this.errCode = errCode; + this.errMsg = errMsg; + } + + public NacosException(final int errCode, final Throwable throwable) { + super(throwable); + this.errCode = errCode; + this.setCauseThrowable(throwable); + } + + public NacosException(final int errCode, final String errMsg, final Throwable throwable) { + super(errMsg, throwable); + this.errCode = errCode; + this.errMsg = errMsg; + this.setCauseThrowable(throwable); + } + + public int getErrCode() { + return this.errCode; + } + + public String getErrMsg() { + if (!StringUtils.isBlank(this.errMsg)) { + return this.errMsg; + } + if (this.causeThrowable != null) { + return this.causeThrowable.getMessage(); + } + return Constants.NULL; + } + + public void setErrCode(final int errCode) { + this.errCode = errCode; + } + + public void setErrMsg(final String errMsg) { + this.errMsg = errMsg; + } + + public void setCauseThrowable(final Throwable throwable) { + this.causeThrowable = this.getCauseThrowable(throwable); + } + + private Throwable getCauseThrowable(final Throwable t) { + if (t.getCause() == null) { + return t; + } + return this.getCauseThrowable(t.getCause()); + } + + @Override + public String toString() { + return "ErrCode:" + getErrCode() + ", ErrMsg:" + getErrMsg(); + } + + /* + * client error code. + * -400 -503 throw exception to user. + */ + + /** + * invalid param(参数错误). + */ + public static final int CLIENT_INVALID_PARAM = -400; + + /** + * over client threshold(超过server端的限流阈值). + */ + public static final int CLIENT_OVER_THRESHOLD = -503; + + /* + * server error code. + * 400 403 throw exception to user + * 500 502 503 change ip and retry + */ + + /** + * invalid param(参数错误). + */ + public static final int INVALID_PARAM = 400; + + /** + * no right(鉴权失败). + */ + public static final int NO_RIGHT = 403; + + /** + * not found. + */ + public static final int NOT_FOUND = 404; + + /** + * conflict(写并发冲突). + */ + public static final int CONFLICT = 409; + + /** + * server error(server异常,如超时). + */ + public static final int SERVER_ERROR = 500; + + /** + * bad gateway(路由异常,如nginx后面的Server挂掉). + */ + public static final int BAD_GATEWAY = 502; + + /** + * over threshold(超过server端的限流阈值). + */ + public static final int OVER_THRESHOLD = 503; + + public static final int RESOURCE_NOT_FOUND = -404; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java index 3d2c120e1..c2f17a3c5 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java @@ -13,238 +13,240 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.api.naming.pojo; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.PreservedMetadataKeys; +import com.alibaba.nacos.api.utils.StringUtils; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import org.apache.commons.lang3.StringUtils; - import java.util.HashMap; import java.util.Map; import static com.alibaba.nacos.api.common.Constants.NUMBER_PATTERN; /** - * Instance + * Instance. * * @author nkorange */ @JsonInclude(Include.NON_NULL) public class Instance { - + /** * unique id of this instance. */ private String instanceId; - + /** - * instance ip + * instance ip. */ private String ip; - + /** - * instance port + * instance port. */ private int port; - + /** - * instance weight + * instance weight. */ private double weight = 1.0D; - + /** - * instance health status + * instance health status. */ private boolean healthy = true; - + /** - * If instance is enabled to accept request + * If instance is enabled to accept request. */ private boolean enabled = true; - + /** - * If instance is ephemeral + * If instance is ephemeral. * * @since 1.0.0 */ private boolean ephemeral = true; - + /** - * cluster information of instance + * cluster information of instance. */ private String clusterName; - + /** - * Service information of instance + * Service information of instance. */ private String serviceName; - + /** - * user extended attributes + * user extended attributes. */ private Map metadata = new HashMap(); - + public String getInstanceId() { - return instanceId; + return this.instanceId; } - - public void setInstanceId(String instanceId) { + + public void setInstanceId(final String instanceId) { this.instanceId = instanceId; } - + public String getIp() { - return ip; + return this.ip; } - - public void setIp(String ip) { + + public void setIp(final String ip) { this.ip = ip; } - + public int getPort() { - return port; + return this.port; } - - public void setPort(int port) { + + public void setPort(final int port) { this.port = port; } - + public double getWeight() { - return weight; + return this.weight; } - - public void setWeight(double weight) { + + public void setWeight(final double weight) { this.weight = weight; } - + public boolean isHealthy() { - return healthy; + return this.healthy; } - - public void setHealthy(boolean healthy) { + + public void setHealthy(final boolean healthy) { this.healthy = healthy; } - + public String getClusterName() { - return clusterName; + return this.clusterName; } - - public void setClusterName(String clusterName) { + + public void setClusterName(final String clusterName) { this.clusterName = clusterName; } - + public String getServiceName() { - return serviceName; + return this.serviceName; } - - public void setServiceName(String serviceName) { + + public void setServiceName(final String serviceName) { this.serviceName = serviceName; } - + public Map getMetadata() { - return metadata; + return this.metadata; } - - public void setMetadata(Map metadata) { + + public void setMetadata(final Map metadata) { this.metadata = metadata; } - - public void addMetadata(String key, String value) { - if (this.metadata == null) { - this.metadata = new HashMap(4); + + /** + * add meta data. + * + * @param key meta data key + * @param value meta data value + */ + public void addMetadata(final String key, final String value) { + if (metadata == null) { + metadata = new HashMap(4); } - this.metadata.put(key, value); + metadata.put(key, value); } - + public boolean isEnabled() { - return enabled; + return this.enabled; } - - public void setEnabled(boolean enabled) { + + public void setEnabled(final boolean enabled) { this.enabled = enabled; } - + public boolean isEphemeral() { - return ephemeral; + return this.ephemeral; } - - public void setEphemeral(boolean ephemeral) { + + public void setEphemeral(final boolean ephemeral) { this.ephemeral = ephemeral; } - + @Override public String toString() { - return "Instance{" + - "instanceId='" + instanceId + '\'' + - ", ip='" + ip + '\'' + - ", port=" + port + - ", weight=" + weight + - ", healthy=" + healthy + - ", enabled=" + enabled + - ", ephemeral=" + ephemeral + - ", clusterName='" + clusterName + '\'' + - ", serviceName='" + serviceName + '\'' + - ", metadata=" + metadata + - '}'; + return "Instance{" + "instanceId='" + instanceId + '\'' + ", ip='" + ip + '\'' + ", port=" + port + + ", weight=" + weight + ", healthy=" + healthy + ", enabled=" + enabled + ", ephemeral=" + + ephemeral + ", clusterName='" + clusterName + '\'' + ", serviceName='" + serviceName + + '\'' + ", metadata=" + metadata + '}'; } - + public String toInetAddr() { return ip + ":" + port; } - + @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (!(obj instanceof Instance)) { return false; } - - Instance host = (Instance) obj; - return strEquals(host.toString(), toString()); + + final Instance host = (Instance) obj; + return Instance.strEquals(host.toString(), toString()); } - + @Override public int hashCode() { return toString().hashCode(); } - - private static boolean strEquals(String str1, String str2) { + + private static boolean strEquals(final String str1, final String str2) { return str1 == null ? str2 == null : str1.equals(str2); } - + public long getInstanceHeartBeatInterval() { - return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_INTERVAL, Constants.DEFAULT_HEART_BEAT_INTERVAL); + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_INTERVAL, + Constants.DEFAULT_HEART_BEAT_INTERVAL); } - + public long getInstanceHeartBeatTimeOut() { - return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, Constants.DEFAULT_HEART_BEAT_TIMEOUT); + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, + Constants.DEFAULT_HEART_BEAT_TIMEOUT); } - + public long getIpDeleteTimeout() { - return getMetaDataByKeyWithDefault(PreservedMetadataKeys.IP_DELETE_TIMEOUT, Constants.DEFAULT_IP_DELETE_TIMEOUT); + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.IP_DELETE_TIMEOUT, + Constants.DEFAULT_IP_DELETE_TIMEOUT); } - + public String getInstanceIdGenerator() { - return getMetaDataByKeyWithDefault(PreservedMetadataKeys.INSTANCE_ID_GENERATOR, Constants.DEFAULT_INSTANCE_ID_GENERATOR); + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.INSTANCE_ID_GENERATOR, + Constants.DEFAULT_INSTANCE_ID_GENERATOR); } - - private long getMetaDataByKeyWithDefault( String key, long defaultValue) { + + private long getMetaDataByKeyWithDefault(final String key, final long defaultValue) { if (getMetadata() == null || getMetadata().isEmpty()) { return defaultValue; } - String value = getMetadata().get(key); + final String value = getMetadata().get(key); if (!StringUtils.isEmpty(value) && value.matches(NUMBER_PATTERN)) { return Long.parseLong(value); } return defaultValue; } - - private String getMetaDataByKeyWithDefault( String key, String defaultValue) { + + private String getMetaDataByKeyWithDefault(final String key, final String defaultValue) { if (getMetadata() == null || getMetadata().isEmpty()) { return defaultValue; } return getMetadata().get(key); } - + } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Http.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Http.java index 4b04ac6f9..8db6cc2f1 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Http.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Http.java @@ -18,9 +18,9 @@ package com.alibaba.nacos.api.naming.pojo.healthcheck.impl; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker; +import com.alibaba.nacos.api.utils.StringUtils; import com.fasterxml.jackson.annotation.JsonIgnore; import com.google.common.base.Objects; -import org.apache.commons.lang3.StringUtils; import java.util.Collections; import java.util.HashMap; @@ -32,50 +32,51 @@ import java.util.Map; * @author yangyi */ public class Http extends AbstractHealthChecker { + public static final String TYPE = "HTTP"; - + private String path = ""; - + private String headers = ""; - + private int expectedResponseCode = 200; - + public Http() { - super(TYPE); + super(Http.TYPE); } - + public int getExpectedResponseCode() { - return expectedResponseCode; + return this.expectedResponseCode; } - - public void setExpectedResponseCode(int expectedResponseCode) { + + public void setExpectedResponseCode(final int expectedResponseCode) { this.expectedResponseCode = expectedResponseCode; } - + public String getPath() { - return path; + return this.path; } - - public void setPath(String path) { + + public void setPath(final String path) { this.path = path; } - + public String getHeaders() { - return headers; + return this.headers; } - - public void setHeaders(String headers) { + + public void setHeaders(final String headers) { this.headers = headers; } - + @JsonIgnore public Map getCustomHeaders() { if (StringUtils.isBlank(headers)) { return Collections.emptyMap(); } - Map headerMap = new HashMap(16); - for (String s : headers.split(Constants.NAMING_HTTP_HEADER_SPILIER)) { - String[] splits = s.split(":"); + final Map headerMap = new HashMap(16); + for (final String s : headers.split(Constants.NAMING_HTTP_HEADER_SPILIER)) { + final String[] splits = s.split(":"); if (splits.length != 2) { continue; } @@ -83,24 +84,24 @@ public class Http extends AbstractHealthChecker { } return headerMap; } - + @Override public int hashCode() { return Objects.hashCode(path, headers, expectedResponseCode); } - + @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (!(obj instanceof Http)) { return false; } - - Http other = (Http) obj; - + + final Http other = (Http) obj; + if (!StringUtils.equals(type, other.getType())) { return false; } - + if (!StringUtils.equals(path, other.getPath())) { return false; } @@ -109,13 +110,13 @@ public class Http extends AbstractHealthChecker { } return expectedResponseCode == other.getExpectedResponseCode(); } - + @Override public Http clone() throws CloneNotSupportedException { - Http config = new Http(); - config.setPath(this.getPath()); - config.setHeaders(this.getHeaders()); - config.setExpectedResponseCode(this.getExpectedResponseCode()); + final Http config = new Http(); + config.setPath(getPath()); + config.setHeaders(getHeaders()); + config.setExpectedResponseCode(getExpectedResponseCode()); return config; } } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Mysql.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Mysql.java index a9ddb3817..3c76a050b 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Mysql.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/Mysql.java @@ -16,9 +16,8 @@ package com.alibaba.nacos.api.naming.pojo.healthcheck.impl; -import org.apache.commons.lang3.StringUtils; - import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker; +import com.alibaba.nacos.api.utils.StringUtils; import com.google.common.base.Objects; /** @@ -27,72 +26,73 @@ import com.google.common.base.Objects; * @author yangyi */ public class Mysql extends AbstractHealthChecker { + public static final String TYPE = "MYSQL"; - + private String user; - + private String pwd; - + private String cmd; - + public Mysql() { - super(TYPE); + super(Mysql.TYPE); } - + public String getCmd() { - return cmd; + return this.cmd; } - + public String getPwd() { - return pwd; + return this.pwd; } - + public String getUser() { - return user; + return this.user; } - - public void setUser(String user) { + + public void setUser(final String user) { this.user = user; } - - public void setCmd(String cmd) { + + public void setCmd(final String cmd) { this.cmd = cmd; } - - public void setPwd(String pwd) { + + public void setPwd(final String pwd) { this.pwd = pwd; } - + @Override public int hashCode() { return Objects.hashCode(user, pwd, cmd); } - + @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (!(obj instanceof Mysql)) { return false; } - - Mysql other = (Mysql) obj; - + + final Mysql other = (Mysql) obj; + if (!StringUtils.equals(user, other.getUser())) { return false; } - + if (!StringUtils.equals(pwd, other.getPwd())) { return false; } - + return StringUtils.equals(cmd, other.getCmd()); } - + @Override public Mysql clone() throws CloneNotSupportedException { - Mysql config = new Mysql(); - config.setUser(this.getUser()); - config.setPwd(this.getPwd()); - config.setCmd(this.getCmd()); + final Mysql config = new Mysql(); + config.setUser(getUser()); + config.setPwd(getPwd()); + config.setCmd(getCmd()); return config; } } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java index 932b460b0..911219ae3 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java @@ -13,26 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.api.naming.utils; import com.alibaba.nacos.api.common.Constants; -import org.apache.commons.lang3.StringUtils; +import com.alibaba.nacos.api.utils.StringUtils; /** + * NamingUtils. + * * @author nkorange * @since 1.0.0 */ public class NamingUtils { - - public static String getGroupedName(String serviceName, String groupName) { - StringBuilder resultGroupedName = new StringBuilder() - .append(groupName) - .append(Constants.SERVICE_INFO_SPLITER) - .append(serviceName); - return resultGroupedName.toString().intern(); + + public static String getGroupedName(final String serviceName, final String groupName) { + final String resultGroupedName = groupName + Constants.SERVICE_INFO_SPLITER + serviceName; + return resultGroupedName.intern(); } - - public static String getServiceName(String serviceNameWithGroup) { + + public static String getServiceName(final String serviceNameWithGroup) { if (StringUtils.isBlank(serviceNameWithGroup)) { return StringUtils.EMPTY; } @@ -41,8 +41,8 @@ public class NamingUtils { } return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[1]; } - - public static String getGroupName(String serviceNameWithGroup) { + + public static String getGroupName(final String serviceNameWithGroup) { if (StringUtils.isBlank(serviceNameWithGroup)) { return StringUtils.EMPTY; } diff --git a/api/src/main/java/com/alibaba/nacos/api/utils/StringUtils.java b/api/src/main/java/com/alibaba/nacos/api/utils/StringUtils.java new file mode 100644 index 000000000..97764d9bc --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/utils/StringUtils.java @@ -0,0 +1,187 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.api.utils; + +/** + * StringUtils. copy from apache common-lang3. + * + * @author lin-mt + */ +public class StringUtils { + + /** + * The empty String {@code ""}. + * + * @since 2.0 + */ + public static final String EMPTY = ""; + + /** + *

Checks if a CharSequence is empty ("") or null.

+ * + *
+     * StringUtils.isEmpty(null)      = true
+     * StringUtils.isEmpty("")        = true
+     * StringUtils.isEmpty(" ")       = false
+     * StringUtils.isEmpty("bob")     = false
+     * StringUtils.isEmpty("  bob  ") = false
+     * 
+ * + *

NOTE: This method changed in Lang version 2.0. + * It no longer trims the CharSequence. That functionality is available in isBlank().

+ * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is empty or null + * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence) + */ + public static boolean isEmpty(final CharSequence cs) { + return cs == null || cs.length() == 0; + } + + /** + *

Checks if a CharSequence is whitespace, empty ("") or null.

+ * + *
+     * StringUtils.isBlank(null)      = true
+     * StringUtils.isBlank("")        = true
+     * StringUtils.isBlank(" ")       = true
+     * StringUtils.isBlank("bob")     = false
+     * StringUtils.isBlank("  bob  ") = false
+     * 
+ * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is null, empty or whitespace + * @since 2.0 + * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence) + */ + public static boolean isBlank(final CharSequence cs) { + final int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + + // Trim + //----------------------------------------------------------------------- + + /** + *

Removes control characters (char <= 32) from both + * ends of this String, handling {@code null} by returning {@code null}.

+ * + *

The String is trimmed using {@link String#trim()}. + * Trim removes start and end characters <= 32.

+ * + *
+     * StringUtils.trim(null)          = null
+     * StringUtils.trim("")            = ""
+     * StringUtils.trim("     ")       = ""
+     * StringUtils.trim("abc")         = "abc"
+     * StringUtils.trim("    abc    ") = "abc"
+     * 
+ * + * @param str the String to be trimmed, may be null + * @return the trimmed string, {@code null} if null String input + */ + public static String trim(final String str) { + return str == null ? null : str.trim(); + } + + // Equals + //----------------------------------------------------------------------- + + /** + *

Compares two CharSequences, returning {@code true} if they represent + * equal sequences of characters.

+ * + *

{@code null}s are handled without exceptions. Two {@code null} + * references are considered to be equal. The comparison is case sensitive.

+ * + *
+     * StringUtils.equals(null, null)   = true
+     * StringUtils.equals(null, "abc")  = false
+     * StringUtils.equals("abc", null)  = false
+     * StringUtils.equals("abc", "abc") = true
+     * StringUtils.equals("abc", "ABC") = false
+     * 
+ * + * @param cs1 the first CharSequence, may be {@code null} + * @param cs2 the second CharSequence, may be {@code null} + * @return {@code true} if the CharSequences are equal (case-sensitive), or both {@code null} + * @see Object#equals(Object) + * @since 3.0 Changed signature from equals(String, String) to equals(CharSequence, CharSequence) + */ + public static boolean equals(final CharSequence cs1, final CharSequence cs2) { + if (cs1 == cs2) { + return true; + } + if (cs1 == null || cs2 == null) { + return false; + } + if (cs1 instanceof String && cs2 instanceof String) { + return cs1.equals(cs2); + } + return StringUtils.regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length())); + } + + /** + * Green implementation of regionMatches. + * + * @param cs the {@code CharSequence} to be processed + * @param ignoreCase whether or not to be case insensitive + * @param thisStart the index to start on the {@code cs} CharSequence + * @param substring the {@code CharSequence} to be looked for + * @param start the index to start on the {@code substring} CharSequence + * @param length character length of the region + * @return whether the region matched + */ + public static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart, + final CharSequence substring, final int start, final int length) { + if (cs instanceof String && substring instanceof String) { + return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length); + } + int index1 = thisStart; + int index2 = start; + int tmpLen = length; + + while (tmpLen-- > 0) { + final char c1 = cs.charAt(index1++); + final char c2 = substring.charAt(index2++); + + if (c1 == c2) { + continue; + } + + if (!ignoreCase) { + return false; + } + + // The same check as in String.regionMatches(): + if (Character.toUpperCase(c1) != Character.toUpperCase(c2) && Character.toLowerCase(c1) != Character + .toLowerCase(c2)) { + return false; + } + } + + return true; + } +} diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java index 8c5d725e3..ee893f029 100644 --- a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java @@ -23,7 +23,9 @@ import org.junit.Before; import org.junit.Test; import java.io.IOException; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class ServiceInfoTest { diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/AbstractHealthCheckerTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/AbstractHealthCheckerTest.java index 627fcb372..cf9097fe9 100644 --- a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/AbstractHealthCheckerTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/AbstractHealthCheckerTest.java @@ -30,7 +30,7 @@ import java.io.IOException; public class AbstractHealthCheckerTest { - private ObjectMapper objectMapper = new ObjectMapper(); + private final ObjectMapper objectMapper = new ObjectMapper(); @Before public void setUp() { diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/HealthCheckerFactoryTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/HealthCheckerFactoryTest.java index 551168e58..62756386c 100644 --- a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/HealthCheckerFactoryTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/HealthCheckerFactoryTest.java @@ -16,12 +16,13 @@ package com.alibaba.nacos.api.naming.pojo.healthcheck; -import static org.junit.Assert.*; - import org.junit.Test; import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Tcp; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + public class HealthCheckerFactoryTest { @Test diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/MysqlTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/MysqlTest.java index 84a592206..c1b0d0be7 100644 --- a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/MysqlTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/healthcheck/impl/MysqlTest.java @@ -16,8 +16,6 @@ package com.alibaba.nacos.api.naming.pojo.healthcheck.impl; -import static org.junit.Assert.*; - import java.io.IOException; import org.junit.Before; @@ -26,6 +24,9 @@ import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + public class MysqlTest { private ObjectMapper objectMapper;