Refactor default toString with date support into separate utility
This commit is contained in:
parent
a42925f3c8
commit
51c1bc4a2f
@ -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 (args.length == 2) {
|
||||||
if (o1 instanceof Calendar || o1 instanceof Date) {
|
if (o1 instanceof Calendar || o1 instanceof Date) {
|
||||||
DateFormat formatter = null;
|
Object o2 = args[1];
|
||||||
if (args.length == 2) {
|
if (o2 instanceof String) {
|
||||||
Object o2 = args[1];
|
DateFormat formatter = new SimpleDateFormat((String) o2);
|
||||||
if (o2 != null && o2 instanceof String) {
|
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
|
||||||
formatter = new SimpleDateFormat((String) o2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (formatter == null) {
|
}
|
||||||
formatter = DateFormat.getDateInstance();
|
} else if (args.length == 1) {
|
||||||
}
|
if (o1 instanceof String) {
|
||||||
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
|
return (String) o1;
|
||||||
} else {
|
} else if (o1 != null) {
|
||||||
return (o1 instanceof String) ? (String) o1 : o1.toString();
|
return StringUtils.toString(o1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return new EvalError("ToString accepts an object an optional second argument containing a date format string");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
24
main/src/com/google/refine/util/StringUtils.java
Normal file
24
main/src/com/google/refine/util/StringUtils.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user