Skip to content

Commit

Permalink
Added converters for BigDecimal and BigInteger (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Mar 1, 2013
1 parent 24bda8a commit ca61e51
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jolokia.converter.object;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

import javax.management.ObjectName;
Expand Down Expand Up @@ -52,6 +54,9 @@ public class StringToObjectConverter {
PARSER_MAP.put("double",new DoubleParser());
PARSER_MAP.put(Float.class.getName(),new FloatParser());
PARSER_MAP.put("float",new FloatParser());
PARSER_MAP.put(BigDecimal.class.getName(),new BigDecimalParser());
PARSER_MAP.put(BigInteger.class.getName(),new BigIntegerParser());

PARSER_MAP.put(Boolean.class.getName(),new BooleanParser());
PARSER_MAP.put("boolean",new BooleanParser());
PARSER_MAP.put("char",new CharParser());
Expand Down Expand Up @@ -291,6 +296,16 @@ private static class ShortParser implements Parser {
public Object extract(String pValue) { return Short.parseShort(pValue); }
}

private static class BigDecimalParser implements Parser {
/** {@inheritDoc} */
public Object extract(String pValue) { return new BigDecimal(pValue); }
}

private static class BigIntegerParser implements Parser {
/** {@inheritDoc} */
public Object extract(String pValue) { return new BigInteger(pValue); }
}

private static class DateParser implements Parser {
/** {@inheritDoc} */
public Object extract(String pValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jolokia.converter.object;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

import javax.management.MalformedObjectNameException;
Expand Down Expand Up @@ -77,6 +79,10 @@ public void simpleConversions() {
assertEquals("Double conversion",10.5d,obj);
obj = converter.convertFromString(double.class.getCanonicalName(),"21.3");
assertEquals("double conversion",21.3d,obj);
obj = converter.convertFromString(BigDecimal.class.getCanonicalName(),"83.4e+4");
assertEquals("BigDecimal conversion", new BigDecimal("8.34e+5"), obj);
obj = converter.convertFromString(BigInteger.class.getCanonicalName(),"47110815471108154711");
assertEquals("BigInteger conversion", new BigInteger("47110815471108154711"), obj);

obj = converter.convertFromString(Boolean.class.getCanonicalName(),"false");
assertEquals("Boolean conversion",false,obj);
Expand Down Expand Up @@ -116,8 +122,8 @@ public void jsonConversion() {

@Test
public void enumConversion() {
ConfigKey key = (ConfigKey) converter.prepareValue(ConfigKey.class.getName(),"MAX_DEPTH");
assertEquals(key,ConfigKey.MAX_DEPTH);
ConfigKey key = (ConfigKey) converter.prepareValue(ConfigKey.class.getName(), "MAX_DEPTH");
assertEquals(key, ConfigKey.MAX_DEPTH);
}


Expand Down
5 changes: 5 additions & 0 deletions it/core/src/main/java/org/jolokia/it/OperationChecking.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -107,6 +108,10 @@ public double doubleArguments(double arg1, Double arg2) {
return arg1 + arg2;
}

public BigDecimal addBigDecimal(int first,BigDecimal second) {
return second.add(BigDecimal.valueOf(first));
}

public int overloadedMethod() {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -65,4 +66,6 @@ public interface OperationCheckingMBean {
public String echo(String pEcho);

TimeUnit findTimeUnit(TimeUnit unit);

BigDecimal addBigDecimal(int first, BigDecimal second);
}

0 comments on commit ca61e51

Please sign in to comment.