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 / CoerceToLong.java @ 2092

History | View | Annotate | Download (3.09 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 2 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.dataTypes.impl.coercion;
24

    
25
import java.text.DecimalFormat;
26
import java.text.NumberFormat;
27
import java.text.ParsePosition;
28
import java.util.Date;
29
import org.gvsig.tools.dataTypes.AbstractCoercion;
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.CoercionContext;
32
import org.gvsig.tools.dataTypes.CoercionContextLocale;
33
import org.gvsig.tools.dataTypes.DataTypeUtils;
34

    
35
@SuppressWarnings("UseSpecificCatch")
36
public class CoerceToLong extends AbstractCoercion {
37

    
38
  @Override
39
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
40
    if (value == null || value instanceof Long) {
41
      return value;
42
    }
43
    CoercionContextLocale theContext;
44
    if (context instanceof CoercionContextLocale) {
45
      theContext = (CoercionContextLocale) context;
46
    } else {
47
      theContext = DataTypeUtils.coerceContextDefaultLocale();
48
    }
49
    Number num;
50
    try {
51
      if (value instanceof Number) {
52
        num = (Number) value;
53

    
54
      } else if (value instanceof Boolean) {
55
        num = ((boolean) value ? 1l : 0l);
56

    
57
      } else if (value instanceof Date) {
58
        num = ((Date) value).getTime();
59

    
60
      } else {
61
        String s = value.toString();
62
        if (s == null) {
63
          return null;
64
        }
65
        s = s.trim().toLowerCase();
66
        if (s.length() == 0) {
67
          return null;
68
        }
69
        if (s.startsWith("0x")) {
70
          num = Long.valueOf(s.substring(2), 16);
71

    
72
        } else {
73
          if (s.startsWith("+")) {
74
            s = s.substring(1);
75
          }
76
          ParsePosition p = new ParsePosition(0);
77
          DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale());
78
          num = nf.parse(s, p);
79
          if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
80
            throw new CoercionException("Can't coerce '" + s + "' to long with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ").");
81
          }
82
        }
83
      }
84
      return num.longValue();
85
    } catch (Exception e) {
86
      throw new CoercionException("Can't coerce '" + value.toString() + "' to long.", e);
87
    }
88
  }
89
}