Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToString.java @ 2281

History | View | Annotate | Download (5.35 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dataTypes.impl.coercion;
25

    
26
import java.math.BigDecimal;
27
import java.sql.Time;
28
import java.sql.Timestamp;
29
import java.text.NumberFormat;
30
import java.util.Date;
31
import java.util.Locale;
32
import java.text.DateFormat;
33
import java.text.DecimalFormatSymbols;
34
import java.time.format.DateTimeFormatter;
35
import java.time.format.FormatStyle;
36
import org.gvsig.tools.dataTypes.AbstractCoercion;
37
import org.gvsig.tools.dataTypes.Coercion;
38
import org.gvsig.tools.dataTypes.CoercionContext;
39
import org.gvsig.tools.dataTypes.CoercionContextLocale;
40
import org.gvsig.tools.dataTypes.CoercionException;
41
import org.gvsig.tools.dataTypes.DataTypeUtils;
42

    
43
public class CoerceToString extends AbstractCoercion {
44

    
45
    @Override
46
    public Object coerce(Object value) throws CoercionException {
47
        if (value == null) {
48
            return null;
49
        }
50
        if (value instanceof CharSequence) {
51
            return value.toString();
52
        }
53
        if (value instanceof Number) {
54
            return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
55
        }
56
        return coerce(value, DataTypeUtils.coerceContextDefaultLocale());
57
    }
58

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

    
89
            } else if (value instanceof Timestamp) {
90
                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM, locale);
91
                s = df.format(value);
92

    
93
            } else if (value instanceof Date) {
94
                DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, locale);
95
                s = df.format(value);
96

    
97
            } else if (value instanceof Float || value instanceof Double) {
98
                double d = ((Number)value).doubleValue();
99
                NumberFormat nf = NumberFormat.getInstance(locale);
100
                if (d > 1.8014398509481984E16 || d < -1.8014398509481984E16) {
101
                    // Esto viene a ser la maxima precision de la mantisa, Math.pow(2, 54), 
102
                    // asi que para valores mas grandes utilizamos la notacion cientifica.
103
                    s = String.format("%E", d);
104
                } else {
105
                    nf.setMaximumFractionDigits(16);
106
                    nf.setGroupingUsed(false);
107
                    s = nf.format(((Number) value).doubleValue());
108
                }
109

    
110
            } else if (value instanceof BigDecimal) {
111
                DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
112
                char decimalSeparator = dfs.getDecimalSeparator();
113
                s = ((BigDecimal) value).toPlainString();
114
                if (decimalSeparator!='.'){
115
                    s = s.replace('.', decimalSeparator);
116
                }
117
            } else if (value instanceof Locale) {
118
                s = CoerceToLocale.toString((Locale) value);
119

    
120
            } else {
121
                s = value.toString();
122
            }
123
            return s;
124
        } catch (Exception e) {
125
            throw new CoercionException(e);
126
        }
127

    
128
    }
129

    
130
    public static void main(String[] args) throws CoercionException {
131
        Coercion toString = new CoerceToString();
132

    
133
        String x = (String) toString.coerce(10.5);
134
        System.out.println(x);
135
        x = (String) toString.coerce((double) 0.168873933773767);
136
        System.out.println(x);
137
        x = (String) toString.coerce((double) 123456789012345678901234567890d);
138
        System.out.println(x);
139
        x = (String) toString.coerce((double) -123456789012345678901234567890d);
140
        System.out.println(x);
141

    
142
    }
143
}