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 / CoerceToInt.java @ 2080

History | View | Annotate | Download (2.47 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.util.Date;
26
import org.gvsig.tools.dataTypes.AbstractCoercion;
27
import org.gvsig.tools.dataTypes.CoercionException;
28
import org.gvsig.tools.dataTypes.CoercionContext;
29

    
30
@SuppressWarnings("UseSpecificCatch")
31
public class CoerceToInt extends AbstractCoercion {
32

    
33
  @Override
34
  public Object coerce(Object value) throws CoercionException {
35
    if (value == null || value instanceof Integer) {
36
      return value;
37
    }
38
    return coerce(value, null);
39
  }
40

    
41
  @Override
42
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
43
    if (value == null || value instanceof Integer) {
44
      return value;
45
    }
46
    Integer num;
47
    try {
48
      if (value instanceof Number) {
49
        num = ((Number) value).intValue();
50

    
51
      } else if (value instanceof Boolean) {
52
        num = ((boolean) value ? 1 : 0);
53

    
54
      } else if (value instanceof Date) {
55
        num = (int) ((Date) value).getTime();
56

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

    
69
        } else {
70
          if (s.startsWith("+")) {
71
            s = s.substring(1);
72
          }
73
          num = Integer.valueOf(s);
74
        }
75
      }
76
      return num;
77
    } catch (Exception e) {
78
      throw new CoercionException("Can't coerce '" + value.toString() + "' to integer.", e);
79
    }
80
  }
81

    
82
}