Added join method to StringUtil

This commit is contained in:
Ziver Koc 2015-12-10 21:45:14 +01:00
parent 78b1171754
commit 515bf60309
4 changed files with 33 additions and 3 deletions

BIN
Zutil.jar

Binary file not shown.

View file

@ -27,6 +27,8 @@ package zutil;
import zutil.converters.Converter; import zutil.converters.Converter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/** /**
* This is a class whit utility methods. * This is a class whit utility methods.
@ -128,6 +130,23 @@ 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
* @return a String containing all entries in the list with the specified delimiter in between entries
*/
public static String join(Iterable<?> list, String delimiter){
StringBuilder str = new StringBuilder();
Iterator<?> it = list.iterator();
if(it.hasNext()) {
str.append(it.next());
while (it.hasNext()) {
str.append(delimiter).append(it.next());
}
}
return str.toString();
}
/** /**
* Trims the given char and whitespace at the beginning and the end * Trims the given char and whitespace at the beginning and the end
* *

View file

@ -1,6 +1,6 @@
package zutil.db; package zutil.db;
import com.sun.deploy.util.StringUtils; import zutil.StringUtil;
import zutil.db.handler.ListSQLResult; import zutil.db.handler.ListSQLResult;
import zutil.db.handler.SimpleSQLResult; import zutil.db.handler.SimpleSQLResult;
import zutil.log.LogUtil; import zutil.log.LogUtil;
@ -18,7 +18,7 @@ import java.util.logging.Logger;
/** /**
* This class will take a reference DB and alter * This class will take a reference DB and alter
* the source DB to have the same structure. * the source DB to have the same structure.
* NOTE: only works with sqllite * NOTE: only works with SQLite
* *
* Created by Ziver * Created by Ziver
*/ */
@ -162,7 +162,7 @@ public class DBUpgradeHandler {
// Restoring data // Restoring data
logger.fine("Forced Upgrade: Restoring data for table: "+table); logger.fine("Forced Upgrade: Restoring data for table: "+table);
String cols = StringUtils.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));

11
test/zutil/test/StringUtilTest.java Normal file → Executable file
View file

@ -26,6 +26,9 @@ package zutil.test;
import org.junit.Test; import org.junit.Test;
import zutil.StringUtil; import zutil.StringUtil;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class StringUtilTest { public class StringUtilTest {
@ -90,4 +93,12 @@ public class StringUtilTest {
"024 00 00 00 00 00 00 00 00 '........'", "024 00 00 00 00 00 00 00 00 '........'",
StringUtil.formatBytesToString(data3)); StringUtil.formatBytesToString(data3));
} }
@Test
public void joinTest(){
assertEquals("", StringUtil.join(Arrays.asList(), ","));
assertEquals("1,2,3,4,5", StringUtil.join(Arrays.asList(1,2,3,4,5), ","));
assertEquals("animal,monkey,dog", StringUtil.join(Arrays.asList("animal", "monkey", "dog"), ","));
assertEquals("12345", StringUtil.join(Arrays.asList(1,2,3,4,5), ""));
}
} }