Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/org/apache/xmlbeans/GDateBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Date;
import java.util.TimeZone;

import org.apache.xmlbeans.impl.util.MathUtil;

/**
* Used to build {@link GDate GDates}.
* <p>
Expand Down Expand Up @@ -811,7 +813,7 @@ private long _normalizeTime() {
if (_fs != null && (_fs.signum() < 0 || _fs.compareTo(GDate._one) >= 0)) {
BigDecimal bdcarry = _fs.setScale(0, RoundingMode.FLOOR);
_fs = _fs.subtract(bdcarry);
carry = bdcarry.longValue();
carry = MathUtil.toLong(bdcarry);
}

if (carry != 0 || _s < 0 || _s > 59 || _m < 0 || _m > 50 || _h < 0 || _h > 23) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/apache/xmlbeans/GDurationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.math.BigInteger;
import java.math.RoundingMode;

import org.apache.xmlbeans.impl.util.MathUtil;

/**
* Used to build {@link GDuration GDurations}.
*/
Expand Down Expand Up @@ -348,7 +350,7 @@ private void _normalizeImpl(boolean adjustSign) {
if (_fs != null && (_fs.signum() < 0 || _fs.compareTo(GDate._one) >= 0)) {
BigDecimal bdcarry = _fs.setScale(0, RoundingMode.FLOOR);
_fs = _fs.subtract(bdcarry);
carry = bdcarry.intValue();
carry = MathUtil.toInt(bdcarry);
}

if (carry != 0 || _s < 0 || _s > 59 || _m < 0 || _m > 50 || _h < 0 || _h > 23) {
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static int parseAsInt(String s) {
* @param value BigDecimal to convert
* @return valid BigInteger
* @throws IllegalArgumentException if the input has an absolute exponent that is too large to safely convert
* @throws NullPointerException if string is null
* @throws NullPointerException if value is null
*/
public static BigInteger toBigInteger(BigDecimal value) {
BigDecimal normalized = value.stripTrailingZeros();
Expand All @@ -207,4 +207,29 @@ public static BigInteger toBigInteger(BigDecimal value) {
}
return normalized.toBigInteger();
}

/**
* @param value BigDecimal to convert
* @return valid long
* @throws IllegalArgumentException if the input has an absolute exponent that is too large to safely convert
* @throws ArithmeticException if the value cannot be represented as a long
* @throws NullPointerException if value is null
*/
public static long toLong(BigDecimal value) {
BigInteger bigInt = toBigInteger(value);
return bigInt.longValueExact();
}

/**
* @param value BigDecimal to convert
* @return valid int
* @throws IllegalArgumentException if the input has an absolute exponent that is too large to safely convert
* @throws ArithmeticException if the value cannot be represented as an int
* @throws NullPointerException if value is null
*/
public static int toInt(BigDecimal value) {
BigInteger bigInt = toBigInteger(value);
return bigInt.intValueExact();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static String printDecimal(BigDecimal value) {
// The following code comes from Apache Harmony
String intStr = value.unscaledValue().toString();
int scale = value.scale();
if ((scale == 0) || ((value.longValue() == 0) && (scale < 0))) {
if (scale == 0 || (MathUtil.toLong(value) == 0 && scale < 0)) {
return intStr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.util.XsTypeConverter;

public abstract class JavaIntHolderEx extends JavaIntHolder {
Expand Down Expand Up @@ -150,7 +151,7 @@ private static int getIntValue(XmlObject o) {
SchemaType s = o.schemaType();
switch (s.getDecimalSize()) {
case SchemaType.SIZE_BIG_DECIMAL:
return ((XmlObjectBase) o).getBigDecimalValue().intValue();
return MathUtil.toInt(((XmlObjectBase) o).getBigDecimalValue());
case SchemaType.SIZE_BIG_INTEGER:
return ((XmlObjectBase) o).getBigIntegerValue().intValue();
case SchemaType.SIZE_LONG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.util.XsTypeConverter;

public abstract class JavaLongHolderEx extends JavaLongHolder {
Expand Down Expand Up @@ -150,7 +151,7 @@ private static long getLongValue(XmlObject o) {
SchemaType s = o.schemaType();
switch (s.getDecimalSize()) {
case SchemaType.SIZE_BIG_DECIMAL:
return ((XmlObjectBase) o).getBigDecimalValue().longValue();
return MathUtil.toLong(((XmlObjectBase) o).getBigDecimalValue());
case SchemaType.SIZE_BIG_INTEGER:
return ((XmlObjectBase) o).getBigIntegerValue().longValue();
case SchemaType.SIZE_LONG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private String formatDecimal(String start, SchemaType sType) {
xmlD = (XmlDecimal) sType.getFacet(SchemaType.FACET_TOTAL_DIGITS);
int totalDigits = -1;
if (xmlD != null) {
totalDigits = xmlD.getBigDecimalValue().intValue();
totalDigits = MathUtil.toInt(xmlD.getBigDecimalValue());

StringBuilder sb = new StringBuilder(totalDigits);
for (int i = 0; i < totalDigits; i++) {
Expand Down Expand Up @@ -439,7 +439,7 @@ private String formatDecimal(String start, SchemaType sType) {
if (xmlD == null) {
increment = new BigDecimal(1);
} else {
fractionDigits = xmlD.getBigDecimalValue().intValue();
fractionDigits = MathUtil.toInt(xmlD.getBigDecimalValue());
if (fractionDigits > 0) {
StringBuilder sb = new StringBuilder("0.");
for (int i = 1; i < fractionDigits; i++) {
Expand Down
Loading