Skip to content

Commit

Permalink
新建将list 转成 list<String> 的转换方法 fix #708
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed May 15, 2024
1 parent b62d529 commit 1ea3b17
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
83 changes: 83 additions & 0 deletions feilong-core/src/main/java/com/feilong/core/bean/ConvertUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,89 @@ public static <T> T[] toArray(String[] toBeConvertedValue,Class<T> targetType){

//---------------------------------------------------------------

/**
* 将 toBeConvertedValue 转成 string list.
*
* <p>
* toBeConvertedValue 支持字符串,数组,集合
* </p>
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <pre class="code">
*
* <span style="color:green">//逗号分隔的字符串</span>
* ConvertUtil.toStringList("1,2,3") = toList("1", "2", "3");
* ConvertUtil.toStringList("{1,2,3}") = toList("1", "2", "3");
* ConvertUtil.toStringList("{ 1 ,2,3}") = toList("1", "2", "3");
*
* <span style="color:green">//逗号分隔的字符串 自动去空格</span>
* ConvertUtil.toStringList("1,2, 3") = toList("1", "2", "3");
* ConvertUtil.toStringList("1,2 , 3") = toList("1", "2", "3");
*
* <span style="color:green">//Integer集合</span>
* ConvertUtil.toStringList(toList(1, 2, 3)) = toList("1", "2", "3");
*
* <span style="color:green">//String 数组</span>
* ConvertUtil.toStringList(new String[] { "1", "2", "3" }) = toList("1", "2", "3");
* ConvertUtil.toStringList(toList(1, 2, 3)) = toList("1", "2", "3");
*
* <span style="color:green">//String 集合</span>
* ConvertUtil.toStringList(toList("1", "2", "3")) = toList("1", "2", "3");
* <span style="color:red">//String集合 注意这里不会自动去空格</span>
* ConvertUtil.toStringList(toList("1", "2", " 3")) =toList("1", "2", " 3");
*
* </pre>
*
* </blockquote>
*
* <h3>重构:</h3>
*
* <blockquote>
* <p>
* 对于以下代码:
* </p>
*
* <pre class="code">
*
* String recommendString = staticConfig.getAnchorRecommend();
* if (isNullOrEmpty(recommendString)){
* return new SuccessResult(Lists.newArrayList());
* }
*
* //---------------------------------------------------------------
* String[] recommendStr = recommendString.split(",");
* List{@code <String>} lString = Arrays.asList(recommendStr);
*
* </pre>
*
* <b>可以重构成:</b>
*
* <pre class="code">
* ConvertUtil.toStringList(staticConfig.getAnchorRecommend());
* </pre>
*
* </blockquote>
*
* @apiNote
* <h3>说明:</h3>
* <blockquote>
* <ol>
* <li>此方法返回的list可以进行add等操作</li>
* </ol>
* </blockquote>
*
* @param toBeConvertedValue
* the to be converted value
* @return 如果 <code>toBeConvertedValue</code> 是null,返回 null<br>
* @since 4.1.0
*/
public static List<String> toStringList(Object toBeConvertedValue){
return null == toBeConvertedValue ? null : toList(toStrings(toBeConvertedValue));
}

/**
* 将 <code>toBeConvertedValue</code> 转成{@link String}数组.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
ToLongListTest.class,
ToLongListParameterizedTest.class,

ToStringListTest.class,
ToStringListParameterizedTest.class,

ToMapArrayTest.class,
ToMapCollectionTest.class,
ToMapKeyValueTest.class,
Expand All @@ -78,6 +81,7 @@
ToStringsTest.class,

ToSetVarargsTest.class,

//
})
public class ConvertUtilSuiteTests{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2008 feilong
*
* 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.feilong.core.bean.convertutil;

import static com.feilong.core.bean.ConvertUtil.toArray;
import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.bean.ConvertUtil.toStringList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;

import java.util.List;

import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

import com.feilong.test.Abstract1ParamAndResultParameterizedTest;

public class ToStringListParameterizedTest extends Abstract1ParamAndResultParameterizedTest<Object, List<String>>{

/**
* Data.
*
* @return the iterable
*/
@Parameters(name = "index:{index}: ConvertUtil.toStringList({0})={1}")
public static Iterable<Object[]> data(){
Object[][] objects = build();
return toList(objects);
}

/**
* @return
* @since 1.10.3
*/
private static Object[][] build(){
return new Object[][] { //
{ "1,2,3", toList("1", "2", "3") },
{ "{1,2,3}", toList("1", "2", "3") },
{ "{ 1 ,2,3}", toList("1", "2", "3") },
{ "1,2, 3", toList("1", "2", "3") },
{ "1,2 , 3", toList("1", "2", "3") },
{ new String[] { "1", "2", "3" }, toList("1", "2", "3") },
{ toList("1", "2", "3"), toList("1", "2", "3") },
{ toList(1, 2, 3), toList("1", "2", "3") },
{ toList(1f, 2f, 3f), toList("1.0", "2.0", "3.0") },
{ toList(1L, 2f, 3f), toList("1", "2.0", "3.0") },
{ toList(1L, 2f, 3), toList("1", "2.0", "3") },
{ toList(1d, 2d, 3d), toList("1.0", "2.0", "3.0") },
{ toList("1", "2", " 3"), toList("1", "2", " 3") },

{ toArray(true, false, false), toList("true", "false", "false") },
// { new String[] { "1", null, "2", "3" }, toList(1L, null, 2L, 3L) },

};
}

@Test
public void testToLongs(){
List<String> longList = toStringList(input1);

for (int i = 0; i < expectedValue.size(); ++i){
assertThat(longList, allOf(hasItem(expectedValue.get(i))));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2008 feilong
*
* 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.feilong.core.bean.convertutil;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.feilong.core.bean.ConvertUtil;

public class ToStringListTest{

@Test
public void testToLongListNull(){
assertEquals(null, ConvertUtil.toStringList(null));
}

}

0 comments on commit 1ea3b17

Please sign in to comment.