From 44bdeedad2d0e28717f5012d375d12af76305bb5 Mon Sep 17 00:00:00 2001 From: gorden5566 Date: Wed, 4 Aug 2021 14:02:47 +0800 Subject: [PATCH] [ISSUE 3203] Replace the class 'StringBuffer' by 'StringBuilder' (#3204) --- .../acl/plain/RemoteAddressStrategyFactory.java | 2 +- .../java/org/apache/rocketmq/common/UtilAll.java | 4 ++-- .../apache/rocketmq/common/message/Message.java | 11 +++++++---- .../rocketmq/common/protocol/NamespaceUtil.java | 2 +- .../org/apache/rocketmq/common/UtilAllTest.java | 13 +++++++++++++ .../rocketmq/common/utils/IOTinyUtilsTest.java | 4 ++-- .../filter/expression/UnaryExpression.java | 16 +++++++++++++++- .../rocketmq/filter/parser/ParseException.java | 7 ++++--- .../rocketmq/filter/parser/TokenMgrError.java | 3 ++- .../org/apache/rocketmq/filter/ParserTest.java | 2 +- .../rocketmq/remoting/common/RemotingHelper.java | 2 +- .../rocketmq/store/stats/BrokerStatsManager.java | 4 ++-- .../message/QueryMsgByUniqueKeySubCommand.java | 5 +++-- 13 files changed, 54 insertions(+), 21 deletions(-) diff --git a/acl/src/main/java/org/apache/rocketmq/acl/plain/RemoteAddressStrategyFactory.java b/acl/src/main/java/org/apache/rocketmq/acl/plain/RemoteAddressStrategyFactory.java index 6ec90ee3..1c10fae4 100644 --- a/acl/src/main/java/org/apache/rocketmq/acl/plain/RemoteAddressStrategyFactory.java +++ b/acl/src/main/java/org/apache/rocketmq/acl/plain/RemoteAddressStrategyFactory.java @@ -167,7 +167,7 @@ public class RemoteAddressStrategyFactory { String[] strArray = StringUtils.split(remoteAddr, "."); if (analysis(strArray, 1) || analysis(strArray, 2) || analysis(strArray, 3)) { AclUtils.verify(remoteAddr, index - 1); - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int j = 0; j < index; j++) { sb.append(strArray[j].trim()).append("."); } diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java index aa692081..776c991c 100644 --- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java @@ -565,11 +565,11 @@ public class UtilAll { if (list == null || list.size() == 0) { return null; } - StringBuffer str = new StringBuffer(); + StringBuilder str = new StringBuilder(); for (int i = 0; i < list.size(); i++) { str.append(list.get(i)); if (i == list.size() - 1) { - continue; + break; } str.append(splitor); } diff --git a/common/src/main/java/org/apache/rocketmq/common/message/Message.java b/common/src/main/java/org/apache/rocketmq/common/message/Message.java index c9a133b4..48e1f45a 100644 --- a/common/src/main/java/org/apache/rocketmq/common/message/Message.java +++ b/common/src/main/java/org/apache/rocketmq/common/message/Message.java @@ -43,11 +43,13 @@ public class Message implements Serializable { this.flag = flag; this.body = body; - if (tags != null && tags.length() > 0) + if (tags != null && tags.length() > 0) { this.setTags(tags); + } - if (keys != null && keys.length() > 0) + if (keys != null && keys.length() > 0) { this.setKeys(keys); + } this.setWaitStoreMsgOK(waitStoreMsgOK); } @@ -127,7 +129,7 @@ public class Message implements Serializable { } public void setKeys(Collection keys) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (String k : keys) { sb.append(k); sb.append(MessageConst.KEY_SEPARATOR); @@ -151,8 +153,9 @@ public class Message implements Serializable { public boolean isWaitStoreMsgOK() { String result = this.getProperty(MessageConst.PROPERTY_WAIT_STORE_MSG_OK); - if (null == result) + if (null == result) { return true; + } return Boolean.parseBoolean(result); } diff --git a/common/src/main/java/org/apache/rocketmq/common/protocol/NamespaceUtil.java b/common/src/main/java/org/apache/rocketmq/common/protocol/NamespaceUtil.java index 704eb90b..60fadaab 100644 --- a/common/src/main/java/org/apache/rocketmq/common/protocol/NamespaceUtil.java +++ b/common/src/main/java/org/apache/rocketmq/common/protocol/NamespaceUtil.java @@ -120,7 +120,7 @@ public class NamespaceUtil { return null; } - return new StringBuffer() + return new StringBuilder() .append(MixAll.RETRY_GROUP_TOPIC_PREFIX) .append(wrapNamespace(namespace, consumerGroup)) .toString(); diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java index b854099a..e1942b40 100644 --- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java @@ -19,11 +19,15 @@ package org.apache.rocketmq.common; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.Properties; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.within; +import static org.junit.Assert.assertEquals; public class UtilAllTest { @@ -109,6 +113,15 @@ public class UtilAllTest { assertThat(UtilAll.ipToIPv6Str(nonInternal.getAddress()).toUpperCase()).isEqualTo("2408:4004:0180:8100:3FAA:1DDE:2B3F:898A"); } + @Test + public void testList2String() { + List list = Arrays.asList("groupA=DENY", "groupB=PUB|SUB", "groupC=SUB"); + String comma = ","; + assertEquals("groupA=DENY,groupB=PUB|SUB,groupC=SUB", UtilAll.list2String(list, comma)); + assertEquals(null, UtilAll.list2String(null, comma)); + assertEquals(null, UtilAll.list2String(Collections.emptyList(), comma)); + } + static class DemoConfig { private int demoWidth = 0; private int demoLength = 0; diff --git a/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsTest.java b/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsTest.java index e03e0b96..6a63eecc 100644 --- a/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsTest.java @@ -82,7 +82,7 @@ public class IOTinyUtilsTest { @Test public void testReadLines() throws Exception { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.append("testReadLines").append("\n"); } @@ -95,7 +95,7 @@ public class IOTinyUtilsTest { @Test public void testToBufferedReader() throws Exception { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.append("testToBufferedReader").append("\n"); } diff --git a/filter/src/main/java/org/apache/rocketmq/filter/expression/UnaryExpression.java b/filter/src/main/java/org/apache/rocketmq/filter/expression/UnaryExpression.java index 24845fc0..7f18ddd5 100644 --- a/filter/src/main/java/org/apache/rocketmq/filter/expression/UnaryExpression.java +++ b/filter/src/main/java/org/apache/rocketmq/filter/expression/UnaryExpression.java @@ -53,6 +53,7 @@ public abstract class UnaryExpression implements Expression { public static Expression createNegate(Expression left) { return new UnaryExpression(left, UnaryType.NEGATE) { + @Override public Object evaluate(EvaluationContext context) throws Exception { Object rvalue = right.evaluate(context); if (rvalue == null) { @@ -64,6 +65,7 @@ public abstract class UnaryExpression implements Expression { return null; } + @Override public String getExpressionSymbol() { return "-"; } @@ -85,6 +87,7 @@ public abstract class UnaryExpression implements Expression { final Collection inList = t; return new UnaryInExpression(right, UnaryType.IN, inList, not) { + @Override public Object evaluate(EvaluationContext context) throws Exception { Object rvalue = right.evaluate(context); @@ -103,8 +106,9 @@ public abstract class UnaryExpression implements Expression { } + @Override public String toString() { - StringBuffer answer = new StringBuffer(); + StringBuilder answer = new StringBuilder(); answer.append(right); answer.append(" "); answer.append(getExpressionSymbol()); @@ -124,6 +128,7 @@ public abstract class UnaryExpression implements Expression { return answer.toString(); } + @Override public String getExpressionSymbol() { if (not) { return "NOT IN"; @@ -139,6 +144,7 @@ public abstract class UnaryExpression implements Expression { super(left, unaryType); } + @Override public boolean matches(EvaluationContext context) throws Exception { Object object = evaluate(context); return object != null && object == Boolean.TRUE; @@ -147,6 +153,7 @@ public abstract class UnaryExpression implements Expression { public static BooleanExpression createNOT(BooleanExpression left) { return new BooleanUnaryExpression(left, UnaryType.NOT) { + @Override public Object evaluate(EvaluationContext context) throws Exception { Boolean lvalue = (Boolean) right.evaluate(context); if (lvalue == null) { @@ -155,6 +162,7 @@ public abstract class UnaryExpression implements Expression { return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE; } + @Override public String getExpressionSymbol() { return "NOT"; } @@ -163,6 +171,7 @@ public abstract class UnaryExpression implements Expression { public static BooleanExpression createBooleanCast(Expression left) { return new BooleanUnaryExpression(left, UnaryType.BOOLEANCAST) { + @Override public Object evaluate(EvaluationContext context) throws Exception { Object rvalue = right.evaluate(context); if (rvalue == null) { @@ -174,10 +183,12 @@ public abstract class UnaryExpression implements Expression { return ((Boolean) rvalue).booleanValue() ? Boolean.TRUE : Boolean.FALSE; } + @Override public String toString() { return right.toString(); } + @Override public String getExpressionSymbol() { return ""; } @@ -233,6 +244,7 @@ public abstract class UnaryExpression implements Expression { /** * @see Object#toString() */ + @Override public String toString() { return "(" + getExpressionSymbol() + " " + right.toString() + ")"; } @@ -240,6 +252,7 @@ public abstract class UnaryExpression implements Expression { /** * @see Object#hashCode() */ + @Override public int hashCode() { return toString().hashCode(); } @@ -247,6 +260,7 @@ public abstract class UnaryExpression implements Expression { /** * @see Object#equals(Object) */ + @Override public boolean equals(Object o) { if (o == null || !this.getClass().equals(o.getClass())) { diff --git a/filter/src/main/java/org/apache/rocketmq/filter/parser/ParseException.java b/filter/src/main/java/org/apache/rocketmq/filter/parser/ParseException.java index 06014cbc..0a327bea 100644 --- a/filter/src/main/java/org/apache/rocketmq/filter/parser/ParseException.java +++ b/filter/src/main/java/org/apache/rocketmq/filter/parser/ParseException.java @@ -106,7 +106,7 @@ public class ParseException extends Exception { int[][] expectedTokenSequences, String[] tokenImage) { String eol = System.getProperty("line.separator", "\n"); - StringBuffer expected = new StringBuffer(); + StringBuilder expected = new StringBuilder(); int maxSize = 0; for (int i = 0; i < expectedTokenSequences.length; i++) { if (maxSize < expectedTokenSequences[i].length) { @@ -123,8 +123,9 @@ public class ParseException extends Exception { String retval = "Encountered \""; Token tok = currentToken.next; for (int i = 0; i < maxSize; i++) { - if (i != 0) + if (i != 0) { retval += " "; + } if (tok.kind == 0) { retval += tokenImage[0]; break; @@ -157,7 +158,7 @@ public class ParseException extends Exception { * string literal. */ static String add_escapes(String str) { - StringBuffer retval = new StringBuffer(); + StringBuilder retval = new StringBuilder(); char ch; for (int i = 0; i < str.length(); i++) { switch (str.charAt(i)) { diff --git a/filter/src/main/java/org/apache/rocketmq/filter/parser/TokenMgrError.java b/filter/src/main/java/org/apache/rocketmq/filter/parser/TokenMgrError.java index e8132df5..0aeb27cf 100644 --- a/filter/src/main/java/org/apache/rocketmq/filter/parser/TokenMgrError.java +++ b/filter/src/main/java/org/apache/rocketmq/filter/parser/TokenMgrError.java @@ -66,7 +66,7 @@ public class TokenMgrError extends Error { * equivalents in the given string */ protected static final String addEscapes(String str) { - StringBuffer retval = new StringBuffer(); + StringBuilder retval = new StringBuilder(); char ch; for (int i = 0; i < str.length(); i++) { switch (str.charAt(i)) { @@ -141,6 +141,7 @@ public class TokenMgrError extends Error { *

* from this method for such cases in the release version of your parser. */ + @Override public String getMessage() { return super.getMessage(); } diff --git a/filter/src/test/java/org/apache/rocketmq/filter/ParserTest.java b/filter/src/test/java/org/apache/rocketmq/filter/ParserTest.java index 115632fa..7dc2ab25 100644 --- a/filter/src/test/java/org/apache/rocketmq/filter/ParserTest.java +++ b/filter/src/test/java/org/apache/rocketmq/filter/ParserTest.java @@ -84,7 +84,7 @@ public class ParserTest { @Test public void testParse_floatOverFlow() { try { - StringBuffer sb = new StringBuffer(210000); + StringBuilder sb = new StringBuilder(210000); sb.append("1"); for (int i = 0; i < 2048; i ++) { sb.append("111111111111111111111111111111111111111111111111111"); diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java b/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java index f244bf4c..7dacea9c 100644 --- a/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java +++ b/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java @@ -36,7 +36,7 @@ public class RemotingHelper { private static final InternalLogger log = InternalLoggerFactory.getLogger(ROCKETMQ_REMOTING); public static String exceptionSimpleDesc(final Throwable e) { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); if (e != null) { sb.append(e.toString()); diff --git a/store/src/main/java/org/apache/rocketmq/store/stats/BrokerStatsManager.java b/store/src/main/java/org/apache/rocketmq/store/stats/BrokerStatsManager.java index e1518447..7bb6a8b4 100644 --- a/store/src/main/java/org/apache/rocketmq/store/stats/BrokerStatsManager.java +++ b/store/src/main/java/org/apache/rocketmq/store/stats/BrokerStatsManager.java @@ -159,7 +159,7 @@ public class BrokerStatsManager { } public String buildStatsKey(String topic, String group) { - StringBuffer strBuilder = new StringBuffer(); + StringBuilder strBuilder = new StringBuilder(); strBuilder.append(topic); strBuilder.append("@"); strBuilder.append(group); @@ -217,7 +217,7 @@ public class BrokerStatsManager { } public String buildCommercialStatsKey(String owner, String topic, String group, String type) { - StringBuffer strBuilder = new StringBuffer(); + StringBuilder strBuilder = new StringBuilder(); strBuilder.append(owner); strBuilder.append("@"); strBuilder.append(topic); diff --git a/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java b/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java index 8c7bec61..17abdf21 100644 --- a/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java +++ b/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByUniqueKeySubCommand.java @@ -114,7 +114,7 @@ public class QueryMsgByUniqueKeySubCommand implements SubCommand { private static String createBodyFile(MessageExt msg, int index) throws IOException { DataOutputStream dos = null; try { - StringBuffer bodyTmpFilePath = new StringBuffer("/tmp/rocketmq/msgbodys"); + StringBuilder bodyTmpFilePath = new StringBuilder("/tmp/rocketmq/msgbodys"); File file = new File(bodyTmpFilePath.toString()); if (!file.exists()) { file.mkdirs(); @@ -127,8 +127,9 @@ public class QueryMsgByUniqueKeySubCommand implements SubCommand { dos.write(msg.getBody()); return bodyTmpFilePath.toString(); } finally { - if (dos != null) + if (dos != null) { dos.close(); + } } } -- GitLab