Refactor default toString with date support into separate utility

This commit is contained in:
Tom Morris 2013-06-23 12:02:13 -04:00
parent a42925f3c8
commit 51c1bc4a2f
4 changed files with 43 additions and 22 deletions

View File

@ -42,33 +42,33 @@ import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
import com.google.refine.util.StringUtils;
public class ToString implements Function { public class ToString implements Function {
@Override @Override
public String call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if (args.length >= 1) { if (args.length >= 1) {
Object o1 = args[0]; Object o1 = args[0];
if (o1 != null) {
if (o1 instanceof Calendar || o1 instanceof Date) {
DateFormat formatter = null;
if (args.length == 2) { if (args.length == 2) {
if (o1 instanceof Calendar || o1 instanceof Date) {
Object o2 = args[1]; Object o2 = args[1];
if (o2 != null && o2 instanceof String) { if (o2 instanceof String) {
formatter = new SimpleDateFormat((String) o2); DateFormat formatter = new SimpleDateFormat((String) o2);
}
}
if (formatter == null) {
formatter = DateFormat.getDateInstance();
}
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime()); return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
} else { }
return (o1 instanceof String) ? (String) o1 : o1.toString(); }
} else if (args.length == 1) {
if (o1 instanceof String) {
return (String) o1;
} else if (o1 != null) {
return StringUtils.toString(o1);
} }
} }
} }
return null; return new EvalError("ToString accepts an object an optional second argument containing a date format string");
} }

View File

@ -42,9 +42,9 @@ import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.ToString;
import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
import com.google.refine.util.StringUtils;
public class Escape implements Function { public class Escape implements Function {
@ -60,8 +60,7 @@ public class Escape implements Function {
s = ""; s = "";
} else { } else {
// Use our own ToString so that formatting is consistent // Use our own ToString so that formatting is consistent
ToString toString = new ToString(); s = StringUtils.toString(o1);
s = toString.call(bindings,new Object[] {o1});
} }
if (o2 instanceof String) { if (o2 instanceof String) {
String mode = ((String) o2).toLowerCase(); String mode = ((String) o2).toLowerCase();

View File

@ -0,0 +1,24 @@
package com.google.refine.util;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class StringUtils {
/**
* String formatting method that knows how to format dates (using the defaul locale's date formatter)
* @param o object to be converted to a string
* @return string representing object
*/
public static String toString(Object o) {
if (o instanceof Calendar || o instanceof Date) {
DateFormat formatter = DateFormat.getDateInstance();
return formatter.format(o instanceof Date ? ((Date) o) : ((Calendar) o).getTime());
} else {
return o.toString();
}
}
}

View File

@ -111,10 +111,8 @@ public class ToFromConversionTests extends RefineTest {
@Test @Test
public void testToString() { public void testToString() {
// Assert.assertTrue(invoke("toString") instanceof EvalError); Assert.assertTrue(invoke("toString") instanceof EvalError);
Assert.assertNull(invoke("toString")); Assert.assertTrue(invoke("toString", (Object) null) instanceof EvalError);
// Assert.assertTrue(invoke("toString", (Object) null) instanceof EvalError);
Assert.assertNull(invoke("toString", (Object) null));
Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0"); Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0");
// Calendar // Calendar
// Date // Date