gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / CoercionContextDecimalImpl.java @ 2190
History | View | Annotate | Download (1.7 KB)
1 |
package org.gvsig.tools.dataTypes; |
---|---|
2 |
|
3 |
import java.math.BigDecimal; |
4 |
import java.math.MathContext; |
5 |
import java.math.RoundingMode; |
6 |
import java.util.Locale; |
7 |
|
8 |
/**
|
9 |
*
|
10 |
* @author jjdelcerro
|
11 |
*/
|
12 |
public class CoercionContextDecimalImpl |
13 |
extends CoercionContextLocaleImpl
|
14 |
implements CoercionContextDecimal {
|
15 |
|
16 |
final MathContext mathContext; |
17 |
final int scale; |
18 |
final int roundMode; |
19 |
|
20 |
public CoercionContextDecimalImpl(Locale locale, int precision, int scale, int roundMode) { |
21 |
// Precision 0 => MathContext.UNLIMITED
|
22 |
// scale <0 ==> Sin scale.
|
23 |
super(locale==null? Locale.ENGLISH:locale); |
24 |
if( precision<1 ) { |
25 |
this.mathContext = MathContext.UNLIMITED; |
26 |
} else {
|
27 |
this.mathContext = new MathContext(precision, RoundingMode.valueOf(roundMode)); |
28 |
} |
29 |
this.scale = scale;
|
30 |
this.roundMode = roundMode;
|
31 |
} |
32 |
|
33 |
public CoercionContextDecimalImpl(Locale locale, int precision, int scale) { |
34 |
this(locale, precision, scale, BigDecimal.ROUND_HALF_UP); |
35 |
} |
36 |
|
37 |
public CoercionContextDecimalImpl(int precision, int scale) { |
38 |
this(null, precision, scale, BigDecimal.ROUND_HALF_UP); |
39 |
} |
40 |
|
41 |
public CoercionContextDecimalImpl(Locale locale) { |
42 |
this(locale, 0, -1, BigDecimal.ROUND_HALF_UP); |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public MathContext getMathContext() { |
47 |
return this.mathContext; |
48 |
} |
49 |
|
50 |
@Override
|
51 |
public int precision() { |
52 |
return this.mathContext.getPrecision(); |
53 |
} |
54 |
|
55 |
@Override
|
56 |
public int scale() { |
57 |
return this.scale; |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public boolean hasScale() { |
62 |
return this.scale>=0; |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public int roundMode() { |
67 |
return this.roundMode; |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public String toString() { |
72 |
return this.locale.toString()+" ("+this.precision()+","+this.scale()+")"; |
73 |
} |
74 |
|
75 |
} |