Revision 1999 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToString.java

View differences:

CoerceToString.java
23 23
 */
24 24
package org.gvsig.tools.dataTypes.impl.coercion;
25 25

  
26
import java.sql.Time;
26 27
import java.text.NumberFormat;
27 28
import java.util.Date;
28 29
import java.util.Locale;
......
32 33

  
33 34
public class CoerceToString implements CoercionWithLocale {
34 35

  
35
    @Override
36
    public Object coerce(Object value) throws CoercionException {
37
        if (value == null) {
38
            return null;
39
        }
40
        try {
41
            if (value instanceof Date) {
42
                Locale locale = Locale.getDefault();
43
                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
44
                value = df.format(value);
45
            } else if (!(value instanceof String)) {
46
                value = value.toString();
47
            }
48
            return value;
49
        } catch (Exception e) {
50
            throw new CoercionException(e);
51
        }
36
	@Override
37
	public Object coerce(Object value) throws CoercionException {
38
		if (value == null) {
39
			return null;
40
		}
41
		try {
42
			if (value instanceof Time) {
43
				Locale locale = Locale.getDefault();
44
				DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, locale);
45
				value = df.format(value);
46
			} else if (value instanceof Date) {
47
				Locale locale = Locale.getDefault();
48
				DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
49
				value = df.format(value);
50
			} else if (!(value instanceof String)) {
51
				Locale locale = Locale.getDefault();
52
				if (value instanceof Float || value instanceof Double) {
53
					NumberFormat nf = NumberFormat.getInstance(locale);
54
					value = nf.format(((Number) value).doubleValue());
55
				} else {
56
					value = value.toString();
57
				}
58
			}
59
			return value;
60
		} catch (Exception e) {
61
			throw new CoercionException(e);
62
		}
52 63

  
53
    }
64
	}
54 65

  
55
    @Override
56
    public Object coerce(Object value, Locale locale) throws CoercionException {
57
        if (value == null) {
58
            return null;
59
        }
60
        try {
61
            if (value instanceof Date) {
62
                if (locale == null) {
63
                    locale = Locale.getDefault();
64
                }
65
                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
66
                value = df.format(value);
67
            } else if (!(value instanceof String)) {
68
                if (value instanceof Float || value instanceof Double) {
69
                    NumberFormat nf = NumberFormat.getInstance(locale);
70
                    value = nf.format(((Number) value).doubleValue());
71
                } else {
72
                    value = value.toString();
73
                }
74
            }
75
            return value;
76
        } catch (Exception e) {
77
            throw new CoercionException(e);
78
        }
79
    }
66
	@Override
67
	public Object coerce(Object value, Locale locale) throws CoercionException {
68
		if (value == null) {
69
			return null;
70
		}
71
		try {
72
			if (value instanceof Date) {
73
				if (locale == null) {
74
					locale = Locale.getDefault();
75
				}
76
				DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
77
				value = df.format(value);
78
			} else if (!(value instanceof String)) {
79
				if (value instanceof Float || value instanceof Double) {
80
					NumberFormat nf = NumberFormat.getInstance(locale);
81
					value = nf.format(((Number) value).doubleValue());
82
				} else {
83
					value = value.toString();
84
				}
85
			}
86
			return value;
87
		} catch (Exception e) {
88
			throw new CoercionException(e);
89
		}
90
	}
80 91

  
81 92
}

Also available in: Unified diff