Revision 2081

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataType.java
27 27

  
28 28
public interface DataType extends LabeledValue<DataType>, org.gvsig.tools.lang.Cloneable {
29 29

  
30
	public boolean isObject() ;
31
	
32
	public boolean isDynObject();
30
  public boolean isObject();
33 31

  
34
	public boolean isContainer() ;
35
	
36
	public boolean isNumeric();
37
	
38
	public String getName() ;
39
	
40
	public int getType() ;
32
  public boolean isDynObject();
41 33

  
42
	public Class getDefaultClass() ;
34
  public boolean isContainer();
43 35

  
44
	public String getSubtype() ;
36
  public boolean isNumeric();
45 37

  
46
	public Coercion getCoercion();
47
	
38
  public String getName();
39

  
40
  public int getType();
41

  
42
  public Class getDefaultClass();
43

  
44
  public String getSubtype();
45

  
46
  public Coercion getCoercion();
47

  
48 48
//	public void setCoercion(Coercion coercion);
49
	
50
	public void addCoercion(Coercion coercion);
51
	
52
	public Object coerce(Object value) throws CoercionException;
53
        
54
        public String getIconName();
49
  public void addCoercion(Coercion coercion);
55 50

  
56
        @Override
57
        public DataType clone() throws CloneNotSupportedException;
51
  public Object coerce(Object value) throws CoercionException;
58 52

  
53
  public Object coerce(Object value, CoercionContext context) throws CoercionException;
54

  
55
  public String getIconName();
56

  
57
  @Override
58
  public DataType clone() throws CloneNotSupportedException;
59

  
59 60
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataType.java
99 99
    throw new CoercionException();
100 100
  }
101 101

  
102

  
102 103
  @Override
104
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
105
    // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
106
    if (this.coercion != null) {
107
      return this.coercion.coerce(value, context);
108
    }
109
    if (defaultClass == null) {
110
      return value; // ??
111
    }
112
    if (defaultClass.isInstance(value)) {
113
      return value;
114
    }
115
    throw new CoercionException();
116
  }
117

  
118
  @Override
103 119
  public Coercion getCoercion() {
104 120
    return this.coercion;
105 121
  }
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDecimal.java
94 94
          }
95 95
        }
96 96
      }
97
      num.setScale(theContext.scale(), theContext.roundMode());
97
      if( theContext.hasScale() ) {
98
        // Ojo, que este set no es un set. Contruye un nuevo BigDecimal, no
99
        // modifica el existente.
100
        num = num.setScale(theContext.scale(), theContext.roundMode());
101
      }
98 102
      return num;
99 103
    } catch (CoercionException e) {
100 104
      throw e;
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionContextDecimal.java
50 50

  
51 51
  /**
52 52
   * Number of decimal digits you can have.
53
   * -1 if it has no limit.
53
   * hasScale() to false if it has no limit.
54 54
   *
55 55
   * @return
56 56
   */
57 57
  public int scale();
58 58

  
59
  public boolean hasScale();
60
  
59 61
  /**
60 62
   * Return the round mode to use.
61 63
   *
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionContextDecimalImpl.java
18 18
  final int roundMode;
19 19

  
20 20
  public CoercionContextDecimalImpl(Locale locale, int precision, int scale, int roundMode) {
21
    // Precision 0 => MathContext.UNLIMITED
22
    // scale <0 ==> Sin scale.
21 23
    super(locale==null? Locale.ENGLISH:locale);
22 24
    if( precision<1 ) {
23 25
      this.mathContext = MathContext.UNLIMITED;
24 26
    } else {
25 27
      this.mathContext = new MathContext(precision, RoundingMode.valueOf(roundMode));
26 28
    }
27
    this.scale = -1;
29
    this.scale = scale;
28 30
    this.roundMode = roundMode;
29 31
  }
30 32

  
31 33
  public CoercionContextDecimalImpl(Locale locale, int precision, int scale) {
32
    this(locale, precision, scale, BigDecimal.ROUND_UNNECESSARY);
34
    this(locale, precision, scale, BigDecimal.ROUND_HALF_UP);
33 35
  }
34 36

  
35 37
  public CoercionContextDecimalImpl(Locale locale) {
36
    super(locale==null? Locale.ENGLISH:locale);
37
    this.mathContext = MathContext.UNLIMITED;
38
    this.scale = -1;
39
    this.roundMode = BigDecimal.ROUND_UNNECESSARY;
38
    this(locale, 0, -1, BigDecimal.ROUND_HALF_UP);
40 39
  }
41 40

  
42 41
  @Override
......
55 54
  }
56 55

  
57 56
  @Override
57
  public boolean hasScale() {
58
    return this.scale>=0;
59
  }
60

  
61
  @Override
58 62
  public int roundMode() {
59 63
    return this.roundMode;
60 64
  }

Also available in: Unified diff