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
17 changes: 17 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/util/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,21 @@ public static Integer parseAsInteger(String s) {
}
return Integer.parseInt(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
*/
public static BigInteger toBigInteger(BigDecimal value) {
BigDecimal normalized = value.stripTrailingZeros();
int integerDigits = normalized.precision() - normalized.scale();
if (integerDigits > MAX_NUMBER_LENGTH || normalized.scale() < -MAX_NUMBER_LENGTH) {
throw new IllegalArgumentException(
"BigDecimal magnitude too large to convert safely: approx "
+ integerDigits + " integer digits (limit " + MAX_NUMBER_LENGTH + ")");
}
return normalized.toBigInteger();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected int value_hash_code() {
}
}

BigInteger intval = _value.toBigInteger();
BigInteger intval = MathUtil.toBigInteger(_value);

if (intval.compareTo(_maxlong) > 0 ||
intval.compareTo(_minlong) < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.XmlErrorCodes;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.util.XsTypeConverter;

import java.math.BigDecimal;
Expand Down Expand Up @@ -77,7 +78,7 @@ public int getIntValue() {

// setters
protected void set_BigDecimal(BigDecimal v) {
set_BigInteger(v.toBigInteger());
set_BigInteger(MathUtil.toBigInteger(v));
}

protected void set_BigInteger(BigInteger v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public BigInteger getBigIntegerValue() {

// setters
protected void set_BigDecimal(BigDecimal v) {
_value = v.toBigInteger();
_value = MathUtil.toBigInteger(v);
}

protected void set_BigInteger(BigInteger v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.xmlbeans.XmlPositiveInteger;
import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.common.ValidationContext;
import org.apache.xmlbeans.impl.util.MathUtil;

import java.math.BigInteger;

Expand Down Expand Up @@ -152,7 +153,7 @@ private static BigInteger getBigIntegerValue(XmlObject o) {
SchemaType s = o.schemaType();
switch (s.getDecimalSize()) {
case SchemaType.SIZE_BIG_DECIMAL:
return ((XmlObjectBase) o).getBigDecimalValue().toBigInteger();
return MathUtil.toBigInteger(((XmlObjectBase) o).getBigDecimalValue());
case SchemaType.SIZE_BIG_INTEGER:
return ((XmlObjectBase) o).getBigIntegerValue();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.XmlErrorCodes;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.util.XsTypeConverter;

import java.math.BigDecimal;
Expand Down Expand Up @@ -72,7 +73,7 @@ public long getLongValue() {

// setters
protected void set_BigDecimal(BigDecimal v) {
set_BigInteger(v.toBigInteger());
set_BigInteger(MathUtil.toBigInteger(v));
}

protected void set_BigInteger(BigInteger v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.xmlbeans.impl.schema.SchemaTypeImpl;
import org.apache.xmlbeans.impl.schema.SchemaTypeVisitorImpl;
import org.apache.xmlbeans.impl.util.LongUTFDataInputStream;
import org.apache.xmlbeans.impl.util.MathUtil;
import org.apache.xmlbeans.impl.validator.Validator;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
Expand Down Expand Up @@ -1346,7 +1347,7 @@ public BigDecimal getBigDecimalValue() {
// numerics: integral
public BigInteger getBigIntegerValue() {
BigDecimal bd = getBigDecimalValue();
return bd == null ? null : bd.toBigInteger();
return bd == null ? null : MathUtil.toBigInteger(bd);
}

public byte getByteValue() {
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/apache/xmlbeans/impl/util/TestMathUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.xmlbeans.impl.util;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class TestMathUtil {
@Test
public void testToBigInteger() {
BigInteger expected = new BigInteger("1234567890");
assertEquals(expected, MathUtil.toBigInteger(new BigDecimal(expected)));
}

@Test
public void testToBigIntegerBigExponent() {
BigDecimal expected = new BigDecimal("1E+2000");
assertThrows(IllegalArgumentException.class, () -> MathUtil.toBigInteger(expected));
}
}
Loading