Revision 2190

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionContextDecimal.java
24 24
package org.gvsig.tools.dataTypes;
25 25

  
26 26
import java.math.MathContext;
27
import java.util.Locale;
27 28

  
28 29
/**
29 30
 *
......
31 32
 */
32 33
public interface CoercionContextDecimal extends CoercionContextLocale {
33 34

  
35
  public static CoercionContextDecimal create(Locale locale) {
36
    return new CoercionContextDecimalImpl(locale);
37
  }
38

  
39
  public static CoercionContextDecimal create(int precision, int scale) {
40
    return new CoercionContextDecimalImpl(precision,scale);
41
  }
42

  
43
  public static CoercionContextDecimal create(Locale locale, int precision, int scale) {
44
    return new CoercionContextDecimalImpl(locale, precision,scale);
45
  }
46

  
47
  public static CoercionContextDecimal create(Locale locale, int precision, int scale, int roundMode) {
48
    return new CoercionContextDecimalImpl(locale, precision,scale, roundMode);
49
  }
50

  
34 51
  /**
35 52
   * MathContext used to make a BigDecimal.
36 53
   * Should never return null.
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionContextLocale.java
31 31
 */
32 32
public interface CoercionContextLocale extends CoercionContext {
33 33

  
34
  public static CoercionContextLocale create() {
35
    return new CoercionContextLocaleImpl(null);
36
  }
37

  
38
  public static CoercionContextLocale create(Locale locale) {
39
    return new CoercionContextLocaleImpl(locale);
40
  }
41

  
34 42
  /**
35 43
   * Return the locale to ese in the coerce operation.
36 44
   * Should never return null.
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionContextDecimalImpl.java
34 34
    this(locale, precision, scale, BigDecimal.ROUND_HALF_UP);
35 35
  }
36 36

  
37
  public CoercionContextDecimalImpl(int precision, int scale) {
38
    this(null, precision, scale, BigDecimal.ROUND_HALF_UP);
39
  }
40

  
37 41
  public CoercionContextDecimalImpl(Locale locale) {
38 42
    this(locale, 0, -1, BigDecimal.ROUND_HALF_UP);
39 43
  }
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypeUtils.java
19 19
    }
20 20

  
21 21
    public static CoercionContextLocale coerceContextLocale(Locale locale) {
22
      return new CoercionContextLocaleImpl(locale);
22
      return CoercionContextLocale.create(locale);
23 23
    }
24 24
    
25 25
    public static CoercionContextLocale coerceContextDefaultLocale() {
26 26
      if( COERCE_CONTEXT_DEFAULT_LOCALE==null || 
27 27
          Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_LOCALE.locale() ) {
28
        COERCE_CONTEXT_DEFAULT_LOCALE = new CoercionContextLocaleImpl(Locale.getDefault());
28
        COERCE_CONTEXT_DEFAULT_LOCALE = CoercionContextLocale.create(Locale.getDefault());
29 29
      }
30 30
      return COERCE_CONTEXT_DEFAULT_LOCALE;
31 31
    }
32 32
    
33 33
    public static CoercionContextDecimal coerceContextDefaultDecimal() {
34 34
      if( COERCE_CONTEXT_DEFAULT_DECIMAL==null ) {
35
        COERCE_CONTEXT_DEFAULT_DECIMAL = new CoercionContextDecimalImpl(
35
        COERCE_CONTEXT_DEFAULT_DECIMAL = CoercionContextDecimal.create(
36 36
                Locale.ENGLISH
37 37
        );
38 38
      }
......
44 44
          locale==COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) {
45 45
          return COERCE_CONTEXT_DEFAULT_DECIMAL;
46 46
      }
47
      return new CoercionContextDecimalImpl(locale);
47
      return CoercionContextDecimal.create(locale);
48 48
    }
49 49

  
50 50
    public static CoercionContextDecimal coerceContextDecimal(Locale locale, int precision, int scale, int roundMode) {
51
      return new CoercionContextDecimalImpl(locale,precision, scale, roundMode);
51
      return CoercionContextDecimal.create(locale,precision, scale, roundMode);
52 52
    }
53 53
    
54
    public static Object coerce(int type, Object value, Object defaultValue) {
54
    public static Coercion getCoercion(int type) {
55 55
        DataTypesManager manager = ToolsLocator.getDataTypesManager();
56 56
        DataType dataType = manager.get(type);
57
        Coercion c = dataType.getCoercion();
58
        return c;
59
    }
60
    
61
    public static Object coerce(int type, Object value, Object defaultValue) {
62
        Coercion coercer = getCoercion(type);
57 63
        try {
58
            Object x = dataType.coerce(value);
64
            Object x = coercer.coerce(value);
59 65
            if( x == null ) {
60 66
                return defaultValue;
61 67
            }
......
66 72
    }
67 73

  
68 74
    public static Object coerce(int type, Object value) throws CoercionException {
69
        DataTypesManager manager = ToolsLocator.getDataTypesManager();
70
        DataType dataType = manager.get(type);
71
        Object x = dataType.coerce(value);
75
        Coercion c = getCoercion(type);
76
        Object x = c.coerce(value);
72 77
        return x;
73 78
    }
74 79
    
......
145 150
    }
146 151
    
147 152
    public static BigDecimal toBigDecimal(Object value, int precision, int scale, BigDecimal defaultValue) {
148
      CoercionContextDecimalImpl context = new CoercionContextDecimalImpl(Locale.ENGLISH, precision, scale);
149
      DataTypesManager manager = ToolsLocator.getDataTypesManager();
150
      DataType dataType = manager.get(DataTypes.DECIMAL);
153
      CoercionContextDecimal context = CoercionContextDecimal.create(precision, scale);
154
      Coercion c = getCoercion(DataTypes.DECIMAL);
151 155
      try {
152
          BigDecimal x = (BigDecimal) dataType.coerce(value,context);
156
          BigDecimal x = (BigDecimal) c.coerce(value,context);
153 157
          if( x == null ) {
154 158
              return defaultValue;
155 159
          }

Also available in: Unified diff