Revision 44669 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/CoerceToInstant.java

View differences:

CoerceToInstant.java
3 3
import java.text.DateFormat;
4 4
import java.text.ParseException;
5 5
import java.util.Date;
6
import java.util.Locale;
7 6
import org.apache.commons.lang3.math.NumberUtils;
8 7
import org.gvsig.timesupport.RelativeInstant;
9 8
import org.gvsig.timesupport.TimeSupportLocator;
10 9
import org.gvsig.timesupport.TimeSupportManager;
10
import org.gvsig.tools.dataTypes.AbstractCoercion;
11 11
import org.gvsig.tools.dataTypes.CoercionException;
12
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
12
import org.gvsig.tools.dataTypes.DataTypeUtils;
13
import org.gvsig.tools.dataTypes.CoercionContext;
14
import org.gvsig.tools.dataTypes.CoercionContextLocale;
13 15

  
14 16
/**
15 17
 *
16 18
 * @author jjdelcerro
17 19
 */
18
public class CoerceToInstant implements CoercionWithLocale {
20
public class CoerceToInstant extends AbstractCoercion {
19 21

  
20
    public CoerceToInstant() {
21
        
22
  public CoerceToInstant() {
23

  
24
  }
25

  
26
  @Override
27
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
28
    if (value == null || value instanceof RelativeInstant) {
29
      return value;
22 30
    }
23
    
24
    @Override
25
    public Object coerce(Object o) throws CoercionException {
26
        return this.coerce(o, null);
31
    TimeSupportManager manager = TimeSupportLocator.getManager();
32
    if (value instanceof Date) {
33
      RelativeInstant instant = manager.createRelativeInstant((Date) value);
34
      return instant;
27 35
    }
28
    
29
    public Object coerce(Object o, Locale locale) throws CoercionException {
30
        if( o == null ) {
31
            return null;
32
        }
33
        if( o instanceof RelativeInstant ) {
34
            return o;
35
        }
36
        TimeSupportManager manager = TimeSupportLocator.getManager();
37
        if( o instanceof Date ) {
38
            RelativeInstant instant = manager.createRelativeInstant((Date)o);
39
            return instant;
40
        }
41
        if( o instanceof Number ) {
42
            RelativeInstant instant = manager.createRelativeInstant( ((Number)o).longValue());
43
            return instant;
44
        }
45
        if( o instanceof String ) {
46
            long milis_l = NumberUtils.toLong((String) o, Long.MIN_VALUE);
47
            if( milis_l != Long.MIN_VALUE ) {
48
                RelativeInstant instant = manager.createRelativeInstant(milis_l);
49
                return instant;
50
            }
51
            double milis_d = NumberUtils.toDouble((String) o, Double.NaN);
52
            if( milis_d != Double.NaN ) {
53
                RelativeInstant instant = manager.createRelativeInstant((long)milis_d);
54
                return instant;
55
            }
56
            DateFormat df = DateFormat.getDateTimeInstance(
57
                    DateFormat.DEFAULT,
58
                    DateFormat.DEFAULT,
59
                    locale==null? Locale.getDefault():locale
60
            );
61
            try {
62
                Date date = df.parse((String) o);
63
                RelativeInstant instant = manager.createRelativeInstant(date);
64
                return instant;
65
            } catch (ParseException ex) {
66
            }
67
        }
68
        throw new CoercionException("Can't coerce ("+o.getClass().getSimpleName()+") '"+o.toString()+"' to Instant.");
36
    if (value instanceof Number) {
37
      RelativeInstant instant = manager.createRelativeInstant(((Number) value).longValue());
38
      return instant;
69 39
    }
40
    if (value instanceof String) {
41
      long milis_l = NumberUtils.toLong((String) value, Long.MIN_VALUE);
42
      if (milis_l != Long.MIN_VALUE) {
43
        RelativeInstant instant = manager.createRelativeInstant(milis_l);
44
        return instant;
45
      }
46
      double milis_d = NumberUtils.toDouble((String) value, Double.NaN);
47
      if (milis_d != Double.NaN) {
48
        RelativeInstant instant = manager.createRelativeInstant((long) milis_d);
49
        return instant;
50
      }
51
      CoercionContextLocale theContext;
52
      if (context instanceof CoercionContextLocale) {
53
        theContext = (CoercionContextLocale) context;
54
      } else {
55
        theContext = DataTypeUtils.coerceContextDefaultLocale();
56
      }
57
      DateFormat df = DateFormat.getDateTimeInstance(
58
              DateFormat.DEFAULT,
59
              DateFormat.DEFAULT,
60
              theContext.locale()
61
      );
62
      try {
63
        Date date = df.parse((String) value);
64
        RelativeInstant instant = manager.createRelativeInstant(date);
65
        return instant;
66
      } catch (ParseException ex) {
67
      }
68
    }
69
    throw new CoercionException("Can't coerce (" + value.getClass().getSimpleName() + ") '" + value.toString() + "' to Instant.");
70
  }
70 71
}

Also available in: Unified diff