Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.timesupport / org.gvsig.timesupport.lib / org.gvsig.timesupport.lib.impl / src / main / java / org / gvsig / timesupport / impl / coercion / CoerceToInterval.java @ 44136

History | View | Annotate | Download (2.78 KB)

1 44078 jjdelcerro
package org.gvsig.timesupport.impl.coercion;
2
3
import java.text.DateFormat;
4
import java.text.ParseException;
5
import java.util.Date;
6
import java.util.Locale;
7
import org.apache.commons.lang3.math.NumberUtils;
8
import org.gvsig.timesupport.RelativeInstant;
9
import org.gvsig.timesupport.RelativeInterval;
10
import org.gvsig.timesupport.TimeSupportLocator;
11
import org.gvsig.timesupport.TimeSupportManager;
12
import org.gvsig.tools.dataTypes.CoercionException;
13
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
14
15
/**
16
 *
17
 * @author jjdelcerro
18
 */
19
public class CoerceToInterval implements CoercionWithLocale {
20
21
    public CoerceToInterval() {
22
23
    }
24
25
    @Override
26
    public Object coerce(Object o) throws CoercionException {
27
        return this.coerce(o, null);
28
    }
29
30
    public Object coerce(Object o, Locale locale) throws CoercionException {
31
        if( o == null ) {
32
            return null;
33
        }
34
        if( o instanceof RelativeInterval ) {
35
            return o;
36
        }
37
        TimeSupportManager manager = TimeSupportLocator.getManager();
38
        if( o instanceof RelativeInstant ) {
39
            RelativeInterval interval = manager.createRelativeInterval(((RelativeInstant) o), ((RelativeInstant) o));
40
            return interval;
41
        }
42
        if( o instanceof Date ) {
43
            RelativeInterval interval = manager.createRelativeInterval(((Date) o).getTime(), ((Date) o).getTime());
44
            return interval;
45
        }
46
        if( o instanceof Number ) {
47
            RelativeInterval interval = manager.createRelativeInterval(((Number)o).longValue(), ((Number)o).longValue());
48
            return interval;
49
        }
50
        if( o instanceof String ) {
51
            long milis_l = NumberUtils.toLong((String) o, Long.MIN_VALUE);
52
            if( milis_l != Long.MIN_VALUE ) {
53
            RelativeInterval interval = manager.createRelativeInterval(milis_l, milis_l);
54
            return interval;
55
            }
56
            double milis_d = NumberUtils.toDouble((String) o, Double.NaN);
57
            if( milis_d != Double.NaN ) {
58
                RelativeInterval interval = manager.createRelativeInterval((long)milis_d, (long)milis_d);
59
                return interval;
60
            }
61
            DateFormat df = DateFormat.getDateTimeInstance(
62
                    DateFormat.DEFAULT,
63
                    DateFormat.DEFAULT,
64
                    locale==null? Locale.getDefault():locale
65
            );
66
            try {
67
                Date date = df.parse((String) o);
68
                RelativeInterval interval = manager.createRelativeInterval(date.getTime(), date.getTime());
69
                return interval;
70
            } catch (ParseException ex) {
71
            }
72
        }
73
        throw new CoercionException("Can't coerce ("+o.getClass().getSimpleName()+") '"+o.toString()+"' to Interval.");
74
    }
75
}