jaxp/src/com/sun/org/apache/xerces/internal/impl/dv/xs/DecimalDV.java
Print this page
@@ -24,10 +24,11 @@
import java.math.BigInteger;
import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
import com.sun.org.apache.xerces.internal.xs.datatypes.XSDecimal;
+import java.util.Objects;
/**
* Represent the schema type "decimal"
*
* @xerces.internal
@@ -36,36 +37,41 @@
* @author Sandy Gao, IBM
*
*/
public class DecimalDV extends TypeValidator {
+ @Override
public final short getAllowedFacets(){
return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE | XSSimpleTypeDecl.FACET_MINEXCLUSIVE | XSSimpleTypeDecl.FACET_TOTALDIGITS | XSSimpleTypeDecl.FACET_FRACTIONDIGITS);
}
+ @Override
public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
try {
return new XDecimal(content);
} catch (NumberFormatException nfe) {
throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "decimal"});
}
}
+ @Override
public final int compare(Object value1, Object value2){
return ((XDecimal)value1).compareTo((XDecimal)value2);
}
+ @Override
public final int getTotalDigits(Object value){
return ((XDecimal)value).totalDigits;
}
+ @Override
public final int getFractionDigits(Object value){
return ((XDecimal)value).fracDigits;
}
// Avoid using the heavy-weight java.math.BigDecimal
- static class XDecimal implements XSDecimal {
+ static final class XDecimal implements XSDecimal {
// sign: 0 for vlaue 0; 1 for positive values; -1 for negative values
int sign = 1;
// total digits. >= 1
int totalDigits = 0;
// integer digits when sign != 0
@@ -214,10 +220,12 @@
sign = 0;
}
integer = true;
}
+
+ @Override
public boolean equals(Object val) {
if (val == this)
return true;
if (!(val instanceof XDecimal))
@@ -230,10 +238,23 @@
return true;
return intDigits == oval.intDigits && fracDigits == oval.fracDigits &&
ivalue.equals(oval.ivalue) && fvalue.equals(oval.fvalue);
}
+
+ @Override
+ public int hashCode() {
+ int hash = 7;
+ hash = 17 * hash + this.sign;
+ if (this.sign == 0) return hash;
+ hash = 17 * hash + this.intDigits;
+ hash = 17 * hash + this.fracDigits;
+ hash = 17 * hash + Objects.hashCode(this.ivalue);
+ hash = 17 * hash + Objects.hashCode(this.fvalue);
+ return hash;
+ }
+
public int compareTo(XDecimal val) {
if (sign != val.sign)
return sign > val.sign ? 1 : -1;
if (sign == 0)
return 0;
@@ -246,11 +267,13 @@
if (ret != 0)
return ret > 0 ? 1 : -1;;
ret = fvalue.compareTo(val.fvalue);
return ret == 0 ? 0 : (ret > 0 ? 1 : -1);
}
+
private String canonical;
+ @Override
public synchronized String toString() {
if (canonical == null) {
makeCanonical();
}
return canonical;
@@ -267,11 +290,11 @@
if (integer && sign > 0) {
canonical = ivalue;
return;
}
// for -0.1, total digits is 1, so we need 3 extra spots
- StringBuffer buffer = new StringBuffer(totalDigits+3);
+ final StringBuilder buffer = new StringBuilder(totalDigits+3);
if (sign == -1)
buffer.append('-');
if (intDigits != 0)
buffer.append(ivalue);
else
@@ -286,17 +309,19 @@
}
}
canonical = buffer.toString();
}
+ @Override
public BigDecimal getBigDecimal() {
if (sign == 0) {
return new BigDecimal(BigInteger.ZERO);
}
return new BigDecimal(toString());
}
+ @Override
public BigInteger getBigInteger() throws NumberFormatException {
if (fracDigits != 0) {
throw new NumberFormatException();
}
if (sign == 0) {
@@ -306,10 +331,11 @@
return new BigInteger(ivalue);
}
return new BigInteger("-" + ivalue);
}
+ @Override
public long getLong() throws NumberFormatException {
if (fracDigits != 0) {
throw new NumberFormatException();
}
if (sign == 0) {
@@ -319,10 +345,11 @@
return Long.parseLong(ivalue);
}
return Long.parseLong("-" + ivalue);
}
+ @Override
public int getInt() throws NumberFormatException {
if (fracDigits != 0) {
throw new NumberFormatException();
}
if (sign == 0) {
@@ -332,10 +359,11 @@
return Integer.parseInt(ivalue);
}
return Integer.parseInt("-" + ivalue);
}
+ @Override
public short getShort() throws NumberFormatException {
if (fracDigits != 0) {
throw new NumberFormatException();
}
if (sign == 0) {
@@ -345,10 +373,11 @@
return Short.parseShort(ivalue);
}
return Short.parseShort("-" + ivalue);
}
+ @Override
public byte getByte() throws NumberFormatException {
if (fracDigits != 0) {
throw new NumberFormatException();
}
if (sign == 0) {