Revision 2223 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
29 29
import java.util.Date;
30 30
import java.util.Locale;
31 31
import java.text.DateFormat;
32
import java.text.DecimalFormatSymbols;
32 33
import org.gvsig.tools.dataTypes.AbstractCoercion;
33 34
import org.gvsig.tools.dataTypes.Coercion;
34 35
import org.gvsig.tools.dataTypes.CoercionContext;
......
38 39

  
39 40
public class CoerceToString extends AbstractCoercion {
40 41

  
41
  @Override
42
  public Object coerce(Object value) throws CoercionException {
43
    if (value == null ) {
44
      return null;
42
    @Override
43
    public Object coerce(Object value) throws CoercionException {
44
        if (value == null) {
45
            return null;
46
        }
47
        if (value instanceof CharSequence) {
48
            return value.toString();
49
        }
50
        if (value instanceof Number) {
51
            return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
52
        }
53
        return coerce(value, DataTypeUtils.coerceContextDefaultLocale());
45 54
    }
46
    if( value instanceof CharSequence ) {
47
      return value.toString();
48
    }
49
    if( value instanceof Number ) {
50
      return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
51
    }
52
    return coerce(value, DataTypeUtils.coerceContextDefaultLocale());
53
  }
54
  
55
  @Override
56
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
57
    if (value == null ) {
58
      return null;
59
    }
60
    if( value instanceof CharSequence ) {
61
      return value.toString();
62
    }
63
    if( value instanceof CharSequence ) {
64
      return value.toString();
65
    }
66
    if( context == null ) {
67
      if( value instanceof Number ) {
68
        context = DataTypeUtils.coerceContextDefaultDecimal();
69
      } else {
70
        context = DataTypeUtils.coerceContextDefaultLocale();
71
      }
72
    }
73
		try {
74
      Locale locale;
75
      if (context instanceof CoercionContextLocale) {
76
        locale = ((CoercionContextLocale) context).locale();
77
      } else {
78
        locale = Locale.getDefault();
79
      }
80
      String s;
81
			if (value instanceof Time) {
82
				DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, locale);
83
				s = df.format(value);
84
        
85
			} else if (value instanceof Date) {
86
				DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
87
				s = df.format(value);
88
        
89
			} else if (value instanceof Float || value instanceof Double ) {
90
        double d = (double) value;
91
        NumberFormat nf = NumberFormat.getInstance(locale);
92
        if( d>1.8014398509481984E16 || d<-1.8014398509481984E16 ) {
93
          // Esto viene a ser la maxima precision de la mantisa, Math.pow(2, 54), 
94
          // asi que para valores mas grandes utilizamos la notacion cientifica.
95
          s = String.format("%E", d);
96
        } else {
97
          nf.setMaximumFractionDigits(16);
98
          nf.setGroupingUsed(false);
99
          s = nf.format(((Number) value).doubleValue());
55

  
56
    @Override
57
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
58
        if (value == null) {
59
            return null;
100 60
        }
61
        if (value instanceof CharSequence) {
62
            return value.toString();
63
        }
64
        if (value instanceof CharSequence) {
65
            return value.toString();
66
        }
67
        if (context == null) {
68
            if (value instanceof Number) {
69
                context = DataTypeUtils.coerceContextDefaultDecimal();
70
            } else {
71
                context = DataTypeUtils.coerceContextDefaultLocale();
72
            }
73
        }
74
        try {
75
            Locale locale;
76
            if (context instanceof CoercionContextLocale) {
77
                locale = ((CoercionContextLocale) context).locale();
78
            } else {
79
                locale = Locale.getDefault();
80
            }
81
            String s;
82
            if (value instanceof Time) {
83
                DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, locale);
84
                s = df.format(value);
101 85

  
102
			} else if (value instanceof BigDecimal ) {
103
        s = ((BigDecimal) value).toPlainString();
86
            } else if (value instanceof Date) {
87
                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
88
                s = df.format(value);
104 89

  
105
			} else if (value instanceof Locale ) {
106
        s = CoerceToLocale.toString((Locale)value);
107
        
108
  		} else {
109
				s = value.toString();
110
			}
111
			return s;
112
		} catch (Exception e) {
113
			throw new CoercionException(e);
114
		}
90
            } else if (value instanceof Float || value instanceof Double) {
91
                double d = (double) value;
92
                NumberFormat nf = NumberFormat.getInstance(locale);
93
                if (d > 1.8014398509481984E16 || d < -1.8014398509481984E16) {
94
                    // Esto viene a ser la maxima precision de la mantisa, Math.pow(2, 54), 
95
                    // asi que para valores mas grandes utilizamos la notacion cientifica.
96
                    s = String.format("%E", d);
97
                } else {
98
                    nf.setMaximumFractionDigits(16);
99
                    nf.setGroupingUsed(false);
100
                    s = nf.format(((Number) value).doubleValue());
101
                }
115 102

  
116
	}
103
            } else if (value instanceof BigDecimal) {
104
                DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
105
                char decimalSeparator = dfs.getDecimalSeparator();
106
                s = ((BigDecimal) value).toPlainString();
107
                if (decimalSeparator!='.'){
108
                    s = s.replace('.', decimalSeparator);
109
                }
110
            } else if (value instanceof Locale) {
111
                s = CoerceToLocale.toString((Locale) value);
117 112

  
118
  public static void main(String[] args) throws CoercionException {
119
    Coercion toString = new CoerceToString();
120
    
121
    String x = (String) toString.coerce(10.5);
122
    System.out.println(x);
123
    x = (String) toString.coerce((double)0.168873933773767);
124
    System.out.println(x);
125
    x = (String) toString.coerce((double)123456789012345678901234567890d);
126
    System.out.println(x);
127
    x = (String) toString.coerce((double)-123456789012345678901234567890d);
128
    System.out.println(x);
129
    
130
  }
113
            } else {
114
                s = value.toString();
115
            }
116
            return s;
117
        } catch (Exception e) {
118
            throw new CoercionException(e);
119
        }
120

  
121
    }
122

  
123
    public static void main(String[] args) throws CoercionException {
124
        Coercion toString = new CoerceToString();
125

  
126
        String x = (String) toString.coerce(10.5);
127
        System.out.println(x);
128
        x = (String) toString.coerce((double) 0.168873933773767);
129
        System.out.println(x);
130
        x = (String) toString.coerce((double) 123456789012345678901234567890d);
131
        System.out.println(x);
132
        x = (String) toString.coerce((double) -123456789012345678901234567890d);
133
        System.out.println(x);
134

  
135
    }
131 136
}

Also available in: Unified diff