Changed join method parameters to add a generic new one
This commit is contained in:
parent
4f26a1ae87
commit
cefd99f6c4
3 changed files with 19 additions and 7 deletions
|
|
@ -25,6 +25,7 @@
|
||||||
package zutil;
|
package zutil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -128,12 +129,21 @@ public class StringUtil {
|
||||||
return str.reverse().toString();
|
return str.reverse().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param list a list of object that toString() will be called on
|
|
||||||
* @param delimiter a String delimiter that will be added between every entry in the list
|
* @param delimiter a String delimiter that will be added between every entry in the list
|
||||||
|
* @param array a array of object that toString() will be called on
|
||||||
* @return a String containing all entries in the list with the specified delimiter in between entries
|
* @return a String containing all entries in the list with the specified delimiter in between entries
|
||||||
*/
|
*/
|
||||||
public static String join(Iterable<?> list, String delimiter){
|
public static <T> String join(String delimiter, T... array){
|
||||||
|
return join(delimiter, Arrays.asList(array));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param delimiter a String delimiter that will be added between every entry in the list
|
||||||
|
* @param list a list of object that toString() will be called on
|
||||||
|
* @return a String containing all entries in the list with the specified delimiter in between entries
|
||||||
|
*/
|
||||||
|
public static String join(String delimiter, Iterable<?> list){
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
Iterator<?> it = list.iterator();
|
Iterator<?> it = list.iterator();
|
||||||
if(it.hasNext()) {
|
if(it.hasNext()) {
|
||||||
|
|
|
||||||
|
|
@ -206,7 +206,7 @@ public class DBUpgradeHandler {
|
||||||
|
|
||||||
// Restoring data
|
// Restoring data
|
||||||
logger.fine(String.format("Forced Upgrade: Restoring data for table: '%s'", table));
|
logger.fine(String.format("Forced Upgrade: Restoring data for table: '%s'", table));
|
||||||
String cols = StringUtil.join(refStruct, ",");
|
String cols = StringUtil.join(",", refStruct);
|
||||||
target.exec(String.format(
|
target.exec(String.format(
|
||||||
"INSERT INTO %s (%s) SELECT %s FROM %s",
|
"INSERT INTO %s (%s) SELECT %s FROM %s",
|
||||||
table, cols, cols, backupTable));
|
table, cols, cols, backupTable));
|
||||||
|
|
|
||||||
|
|
@ -72,9 +72,11 @@ public class StringUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void joinTest(){
|
public void joinTest(){
|
||||||
assertEquals("", StringUtil.join(Arrays.asList(), ","));
|
assertEquals("", StringUtil.join(",", Arrays.asList()));
|
||||||
assertEquals("1,2,3,4,5", StringUtil.join(Arrays.asList(1,2,3,4,5), ","));
|
assertEquals("1,2,3,4,5", StringUtil.join(",", 1,2,3,4,5));
|
||||||
assertEquals("animal,monkey,dog", StringUtil.join(Arrays.asList("animal", "monkey", "dog"), ","));
|
assertEquals("1,2,3,4,5", StringUtil.join(",", Arrays.asList(1,2,3,4,5)));
|
||||||
assertEquals("12345", StringUtil.join(Arrays.asList(1,2,3,4,5), ""));
|
assertEquals("animal,monkey,dog", StringUtil.join(",", "animal", "monkey", "dog"));
|
||||||
|
assertEquals("animal,monkey,dog", StringUtil.join(",", Arrays.asList("animal", "monkey", "dog")));
|
||||||
|
assertEquals("12345", StringUtil.join("", 1,2,3,4,5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue