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 @ 2223
History | View | Annotate | Download (5.05 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.text.NumberFormat; |
29 |
import java.util.Date; |
30 |
import java.util.Locale; |
31 |
import java.text.DateFormat; |
32 |
import java.text.DecimalFormatSymbols; |
33 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
34 |
import org.gvsig.tools.dataTypes.Coercion; |
35 |
import org.gvsig.tools.dataTypes.CoercionContext; |
36 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
37 |
import org.gvsig.tools.dataTypes.CoercionException; |
38 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
39 |
|
40 |
public class CoerceToString extends AbstractCoercion { |
41 |
|
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());
|
54 |
} |
55 |
|
56 |
@Override
|
57 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
58 |
if (value == null) { |
59 |
return null; |
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); |
85 |
|
86 |
} else if (value instanceof Date) { |
87 |
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale); |
88 |
s = df.format(value); |
89 |
|
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 |
} |
102 |
|
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);
|
112 |
|
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 |
} |
136 |
} |