Add utilities and corresponding unit tests for manipulation of strings and display dates.
This commit is contained in:
parent
19a9c79d00
commit
caedb8861e
4 changed files with 169 additions and 5 deletions
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.utils;
|
package edu.cornell.mannlib.vitro.webapp.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
|
@ -19,10 +22,14 @@ public class StringUtils {
|
||||||
if (glue == null) {
|
if (glue == null) {
|
||||||
glue = ",";
|
glue = ",";
|
||||||
}
|
}
|
||||||
|
|
||||||
String joinedList = "";
|
String joinedList = "";
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (Object o : list) {
|
for (Object o : list) {
|
||||||
|
if (o == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
String s = o.toString();
|
String s = o.toString();
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
joinedList += glue;
|
joinedList += glue;
|
||||||
|
@ -38,10 +45,38 @@ public class StringUtils {
|
||||||
return join(list, false, ",");
|
return join(list, false, ",");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String join(List<?> list, String glue) {
|
||||||
|
return join(list, false, glue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String join(String glue, String ...strings) {
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
for (String s : strings) {
|
||||||
|
list.add(s);
|
||||||
|
}
|
||||||
|
return join(list, false, glue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String join(String[] stringArray, boolean quote, String glue) {
|
||||||
|
return join(Arrays.asList(stringArray), quote, glue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String join(String[] stringArray) {
|
||||||
|
return join(Arrays.asList(stringArray));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String join(String[] stringArray, String glue) {
|
||||||
|
return join(Arrays.asList(stringArray), false, glue);
|
||||||
|
}
|
||||||
|
|
||||||
public static String quotedList(List<?> list, String glue) {
|
public static String quotedList(List<?> list, String glue) {
|
||||||
return join(list, true, glue);
|
return join(list, true, glue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String quotedList(String[] stringArray, String glue) {
|
||||||
|
return quotedList(Arrays.asList(stringArray), glue);
|
||||||
|
}
|
||||||
|
|
||||||
// Because we can't use Java 1.6 String.isEmpty()
|
// Because we can't use Java 1.6 String.isEmpty()
|
||||||
public static boolean isEmpty(String s) {
|
public static boolean isEmpty(String s) {
|
||||||
return s == null || s.length() <= 0;
|
return s == null || s.length() <= 0;
|
||||||
|
@ -56,4 +91,9 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String setNullToEmptyString(String s) {
|
||||||
|
return s == null ? "" : s;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.utils.display;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
|
||||||
|
|
||||||
|
public class DateDisplayUtils {
|
||||||
|
|
||||||
|
public static String getDisplayDate(String date) {
|
||||||
|
String displayDate = null;
|
||||||
|
if (date == null) {
|
||||||
|
return displayDate;
|
||||||
|
}
|
||||||
|
List<String> dateParts = Arrays.asList(date.split("-"));
|
||||||
|
int datePartCount = dateParts.size();
|
||||||
|
switch (datePartCount) {
|
||||||
|
case 2:
|
||||||
|
displayDate = StringUtils.join("/", dateParts.get(1), dateParts.get(0));
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
displayDate = StringUtils.join("/", dateParts.get(1), dateParts.get(2), dateParts.get(0));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
displayDate = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
return displayDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDisplayDateRange(String startDate, String endDate) {
|
||||||
|
startDate = StringUtils.setNullToEmptyString(startDate);
|
||||||
|
endDate = StringUtils.setNullToEmptyString(endDate);
|
||||||
|
List<String> dates = Arrays.asList(startDate, endDate);
|
||||||
|
return StringUtils.join(dates, " - ");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDisplayDateRangeFromRawDates(String startDate, String endDate) {
|
||||||
|
return getDisplayDateRange(getDisplayDate(startDate), getDisplayDate(endDate));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.utils;
|
package edu.cornell.mannlib.vitro.webapp.utils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@ -10,11 +11,14 @@ import org.junit.Test;
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.display.DateDisplayUtils;
|
||||||
|
|
||||||
public class StringUtilsTest extends AbstractTestClass {
|
public class StringUtilsTest extends AbstractTestClass {
|
||||||
|
|
||||||
protected static List<String> stringList = new ArrayList<String>();
|
protected static List<String> stringList = new ArrayList<String>();
|
||||||
protected static List<Integer> intList = new ArrayList<Integer>();
|
protected static List<Integer> intList = new ArrayList<Integer>();
|
||||||
|
protected static List<String> stringListWithNulls = new ArrayList<String>();
|
||||||
|
protected static String[] stringArray = {"duck", "goose", "crow"};
|
||||||
static {
|
static {
|
||||||
stringList.add("apple");
|
stringList.add("apple");
|
||||||
stringList.add("banana");
|
stringList.add("banana");
|
||||||
|
@ -23,6 +27,11 @@ public class StringUtilsTest extends AbstractTestClass {
|
||||||
intList.add(1);
|
intList.add(1);
|
||||||
intList.add(2);
|
intList.add(2);
|
||||||
intList.add(3);
|
intList.add(3);
|
||||||
|
|
||||||
|
stringListWithNulls.add("rock");
|
||||||
|
stringListWithNulls.add("paper");
|
||||||
|
stringListWithNulls.add((String)null);
|
||||||
|
stringListWithNulls.add("scissors");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -57,12 +66,37 @@ public class StringUtilsTest extends AbstractTestClass {
|
||||||
@Test
|
@Test
|
||||||
public void testJoinArgs() {
|
public void testJoinArgs() {
|
||||||
|
|
||||||
|
// Three args
|
||||||
Assert.assertEquals("apple:banana:orange", StringUtils.join(stringList, false, ":"));
|
Assert.assertEquals("apple:banana:orange", StringUtils.join(stringList, false, ":"));
|
||||||
Assert.assertEquals("\"apple\"|\"banana\"|\"orange\"", StringUtils.join(stringList, true, "|"));
|
Assert.assertEquals("\"apple\"|\"banana\"|\"orange\"", StringUtils.join(stringList, true, "|"));
|
||||||
Assert.assertEquals("\"apple\",\"banana\",\"orange\"", StringUtils.join(stringList, true, null));
|
Assert.assertEquals("\"apple\",\"banana\",\"orange\"", StringUtils.join(stringList, true, null));
|
||||||
Assert.assertEquals("apple,banana,orange", StringUtils.join(stringList, false, null));
|
Assert.assertEquals("apple,banana,orange", StringUtils.join(stringList, false, null));
|
||||||
Assert.assertEquals("apple...banana...orange", StringUtils.join(stringList, false, "..."));
|
Assert.assertEquals("apple...banana...orange", StringUtils.join(stringList, false, "..."));
|
||||||
|
|
||||||
|
// Two args
|
||||||
|
Assert.assertEquals("apple - banana - orange", StringUtils.join(stringList, " - "));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJoinWithNulls() {
|
||||||
|
Assert.assertEquals("rock,paper,scissors", StringUtils.join(stringListWithNulls));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJoinWithVarargs() {
|
||||||
|
Assert.assertEquals("apple,banana,orange", StringUtils.join((String)null, "apple", "banana", "orange"));
|
||||||
|
Assert.assertEquals("he/she/it", StringUtils.join("/", "he", "she", "it"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJoinWithEmptyString() {
|
||||||
|
Assert.assertEquals(" - 1990", StringUtils.join(" - ", "", "1990"));
|
||||||
|
Assert.assertEquals("1990 - ", StringUtils.join(" - ", "1990", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArrayJoin() {
|
||||||
|
Assert.assertEquals("duck,goose,crow", StringUtils.join(stringArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,4 +115,11 @@ public class StringUtilsTest extends AbstractTestClass {
|
||||||
Assert.assertFalse(StringUtils.equalsOneOf(s1, "dog", "mouse", "horse"));
|
Assert.assertFalse(StringUtils.equalsOneOf(s1, "dog", "mouse", "horse"));
|
||||||
Assert.assertFalse(StringUtils.equalsOneOf(s1));
|
Assert.assertFalse(StringUtils.equalsOneOf(s1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetNullToEmptyString() {
|
||||||
|
Assert.assertEquals("", StringUtils.setNullToEmptyString((String)null));
|
||||||
|
Assert.assertEquals("cat", StringUtils.setNullToEmptyString("cat"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.utils.display;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DateDisplayUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisplayDate() {
|
||||||
|
String date = "2009-10";
|
||||||
|
Assert.assertEquals("10/2009", DateDisplayUtils.getDisplayDate(date));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetDisplayDateRange() {
|
||||||
|
String startRaw = "2010-10-11";
|
||||||
|
String endRaw = "2010-11-09";
|
||||||
|
Assert.assertEquals("10/11/2010 - 11/09/2010", DateDisplayUtils.getDisplayDateRangeFromRawDates(startRaw, endRaw));
|
||||||
|
|
||||||
|
String start1 = "1/2/2010";
|
||||||
|
String end1 = "3/4/2011";
|
||||||
|
Assert.assertEquals("1/2/2010 - 3/4/2011", DateDisplayUtils.getDisplayDateRange(start1, end1));
|
||||||
|
|
||||||
|
String empty = "";
|
||||||
|
Assert.assertEquals("1/2/2010 - ", DateDisplayUtils.getDisplayDateRange(start1, empty));
|
||||||
|
Assert.assertEquals(" - 3/4/2011", DateDisplayUtils.getDisplayDateRange(empty, end1));
|
||||||
|
|
||||||
|
Assert.assertEquals("1/2/2010 - ", DateDisplayUtils.getDisplayDateRange(start1, (String)null));
|
||||||
|
Assert.assertEquals(" - 3/4/2011", DateDisplayUtils.getDisplayDateRange((String)null, end1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue