Skip to content

Commit

Permalink
[ISSUE 3203] Replace the class 'StringBuffer' by 'StringBuilder'
Browse files Browse the repository at this point in the history
  • Loading branch information
gorden5566 committed Jul 31, 2021
1 parent 9e8fea9 commit 366ee98
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 21 deletions.
Expand Up @@ -167,7 +167,7 @@ public RangeRemoteAddressStrategy(String remoteAddr) {
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(".");
}
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/org/apache/rocketmq/common/UtilAll.java
Expand Up @@ -565,11 +565,11 @@ public static String list2String(List<String> list, String splitor) {
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);
}
Expand Down
Expand Up @@ -43,11 +43,13 @@ public Message(String topic, String tags, String keys, int flag, byte[] body, bo
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);
}
Expand Down Expand Up @@ -127,7 +129,7 @@ public String getKeys() {
}

public void setKeys(Collection<String> keys) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (String k : keys) {
sb.append(k);
sb.append(MessageConst.KEY_SEPARATOR);
Expand All @@ -151,8 +153,9 @@ public void setDelayTimeLevel(int level) {

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);
}
Expand Down
Expand Up @@ -120,7 +120,7 @@ public static String wrapNamespaceAndRetry(String namespace, String consumerGrou
return null;
}

return new StringBuffer()
return new StringBuilder()
.append(MixAll.RETRY_GROUP_TOPIC_PREFIX)
.append(wrapNamespace(namespace, consumerGroup))
.toString();
Expand Down
13 changes: 13 additions & 0 deletions common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
Expand Up @@ -19,11 +19,15 @@

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 {

Expand Down Expand Up @@ -109,6 +113,15 @@ public void testIPv6Check() throws UnknownHostException {
assertThat(UtilAll.ipToIPv6Str(nonInternal.getAddress()).toUpperCase()).isEqualTo("2408:4004:0180:8100:3FAA:1DDE:2B3F:898A");
}

@Test
public void testList2String() {
List<String> 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;
Expand Down
Expand Up @@ -82,7 +82,7 @@ public void testCopy() throws Exception {

@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");
}
Expand All @@ -95,7 +95,7 @@ public void testReadLines() throws Exception {

@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");
}
Expand Down
Expand Up @@ -53,6 +53,7 @@ public UnaryExpression(Expression left, UnaryType unaryType) {

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) {
Expand All @@ -64,6 +65,7 @@ public Object evaluate(EvaluationContext context) throws Exception {
return null;
}

@Override
public String getExpressionSymbol() {
return "-";
}
Expand All @@ -85,6 +87,7 @@ public static BooleanExpression createInExpression(PropertyExpression right, Lis
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);
Expand All @@ -103,8 +106,9 @@ public Object evaluate(EvaluationContext context) throws Exception {

}

@Override
public String toString() {
StringBuffer answer = new StringBuffer();
StringBuilder answer = new StringBuilder();
answer.append(right);
answer.append(" ");
answer.append(getExpressionSymbol());
Expand All @@ -124,6 +128,7 @@ public String toString() {
return answer.toString();
}

@Override
public String getExpressionSymbol() {
if (not) {
return "NOT IN";
Expand All @@ -139,6 +144,7 @@ public BooleanUnaryExpression(Expression left, UnaryType unaryType) {
super(left, unaryType);
}

@Override
public boolean matches(EvaluationContext context) throws Exception {
Object object = evaluate(context);
return object != null && object == Boolean.TRUE;
Expand All @@ -147,6 +153,7 @@ public boolean matches(EvaluationContext context) throws Exception {

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) {
Expand All @@ -155,6 +162,7 @@ public Object evaluate(EvaluationContext context) throws Exception {
return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}

@Override
public String getExpressionSymbol() {
return "NOT";
}
Expand All @@ -163,6 +171,7 @@ public String getExpressionSymbol() {

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) {
Expand All @@ -174,10 +183,12 @@ public Object evaluate(EvaluationContext context) throws Exception {
return ((Boolean) rvalue).booleanValue() ? Boolean.TRUE : Boolean.FALSE;
}

@Override
public String toString() {
return right.toString();
}

@Override
public String getExpressionSymbol() {
return "";
}
Expand Down Expand Up @@ -233,20 +244,23 @@ public void setUnaryType(UnaryType unaryType) {
/**
* @see Object#toString()
*/
@Override
public String toString() {
return "(" + getExpressionSymbol() + " " + right.toString() + ")";
}

/**
* @see Object#hashCode()
*/
@Override
public int hashCode() {
return toString().hashCode();
}

/**
* @see Object#equals(Object)
*/
@Override
public boolean equals(Object o) {

if (o == null || !this.getClass().equals(o.getClass())) {
Expand Down
Expand Up @@ -106,7 +106,7 @@ private static String initialise(Token currentToken,
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) {
Expand All @@ -123,8 +123,9 @@ private static String initialise(Token currentToken,
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;
Expand Down Expand Up @@ -157,7 +158,7 @@ private static String initialise(Token currentToken,
* 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)) {
Expand Down
Expand Up @@ -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)) {
Expand Down Expand Up @@ -141,6 +141,7 @@ protected static String LexicalError(boolean eofSeen, int lexState, int errorLin
* <p/>
* from this method for such cases in the release version of your parser.
*/
@Override
public String getMessage() {
return super.getMessage();
}
Expand Down
Expand Up @@ -84,7 +84,7 @@ public void testParse_decimalOverFlow() {
@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");
Expand Down
Expand Up @@ -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());

Expand Down
Expand Up @@ -159,7 +159,7 @@ public void incGroupGetNums(final String group, final String topic, final int in
}

public String buildStatsKey(String topic, String group) {
StringBuffer strBuilder = new StringBuffer();
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(topic);
strBuilder.append("@");
strBuilder.append(group);
Expand Down Expand Up @@ -217,7 +217,7 @@ public void incCommercialValue(final String key, final String owner, final Strin
}

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);
Expand Down
Expand Up @@ -114,7 +114,7 @@ private static void showMessage(final DefaultMQAdminExt admin, MessageExt msg, i
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();
Expand All @@ -127,8 +127,9 @@ private static String createBodyFile(MessageExt msg, int index) throws IOExcepti
dos.write(msg.getBody());
return bodyTmpFilePath.toString();
} finally {
if (dos != null)
if (dos != null) {
dos.close();
}
}
}

Expand Down

0 comments on commit 366ee98

Please sign in to comment.