Skip to content

Commit

Permalink
[ISSUE alibaba#6198] BUG nacos-common>utils>StringUtils.join method N…
Browse files Browse the repository at this point in the history
…ullPointerException alibaba#6198 (alibaba#6213)

* [ISSUE alibaba#6198] fix bug

* format code

* Modify Comment.Remove toString()
  • Loading branch information
ZZQ001010 authored Jun 30, 2021
1 parent 5bb048c commit 4cd6c58
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ public static String substringBetween(String str, String open, String close) {
}

/**
* Join object with input separator.
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>
*
* @param collection collection of objects need to join
* @param separator separator
* @return joined string
* @param collection the Collection of values to join together, may be null
* @param separator the separator string to use
* @return the joined String, {@code null} if null array input
*/
public static String join(Collection collection, String separator) {
if (collection == null) {
Expand All @@ -139,12 +140,15 @@ public static String join(Collection collection, String separator) {
StringBuilder stringBuilder = new StringBuilder();
Object[] objects = collection.toArray();

for (int i = 0; i < collection.size() - 1; i++) {
stringBuilder.append(objects[i].toString()).append(separator);
}

if (collection.size() > 0) {
stringBuilder.append(objects[collection.size() - 1]);
for (int i = 0; i < collection.size(); i++) {
if (objects[i] != null) {
stringBuilder.append(objects[i]);
if (i != collection.size() - 1 && separator != null) {
if (separator != null) {
stringBuilder.append(separator);
}
}
}
}

return stringBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.common.utils;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

/**
* String utils.
* @author zzq
*/
public class StringUtilsTest {

@Test
public void testJoin() {
ArrayList<Object> objects = new ArrayList<>();
objects.add(null);
Assert.assertNull(StringUtils.join(null, "a"));
Assert.assertEquals(StringUtils.EMPTY, StringUtils.join(Arrays.asList(), "a"));
Assert.assertEquals(StringUtils.EMPTY, StringUtils.join(objects, "a"));
Assert.assertEquals("a;b;c", StringUtils.join(Arrays.asList("a", "b", "c"), ";"));
Assert.assertEquals("abc", StringUtils.join(Arrays.asList("a", "b", "c"), null));
}

}

0 comments on commit 4cd6c58

Please sign in to comment.