Skip to content

Commit

Permalink
Merge pull request #1332 from pi-r-p/bigdecimalFix
Browse files Browse the repository at this point in the history
Bigdecimal easier
  • Loading branch information
hbs authored Nov 22, 2023
2 parents 6dc4c38 + b784b16 commit 1e33949
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object apply(WarpScriptStack stack) throws WarpScriptException {

BigDecimal bd2 = TOBD.toBigDecimal(getName(), o);

stack.push(bd1.compareTo(bd2));
stack.push(bd2.compareTo(bd1));

return stack;
}
Expand Down
4 changes: 2 additions & 2 deletions warp10/src/main/java/io/warp10/script/functions/BDDIV.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public Object apply(WarpScriptStack stack) throws WarpScriptException {
BigDecimal bd2 = TOBD.toBigDecimal(getName(), o);

if (this.integral) {
stack.push(bd1.divideToIntegralValue(bd2));
stack.push(bd2.divideToIntegralValue(bd1));
} else {
stack.push(bd1.divide(bd2));
stack.push(bd2.divide(bd1));
}

return stack;
Expand Down
2 changes: 1 addition & 1 deletion warp10/src/main/java/io/warp10/script/functions/BDREM.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object apply(WarpScriptStack stack) throws WarpScriptException {

BigDecimal bd2 = TOBD.toBigDecimal(getName(), o);

stack.push(bd1.remainder(bd2));
stack.push(bd2.remainder(bd1));

return stack;
}
Expand Down
2 changes: 1 addition & 1 deletion warp10/src/main/java/io/warp10/script/functions/BDSUB.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object apply(WarpScriptStack stack) throws WarpScriptException {

BigDecimal bd2 = TOBD.toBigDecimal(getName(), o);

stack.push(bd1.subtract(bd2));
stack.push(bd2.subtract(bd1));

return stack;
}
Expand Down
12 changes: 7 additions & 5 deletions warp10/src/main/java/io/warp10/script/functions/TOBD.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package io.warp10.script.functions;

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

import io.warp10.script.NamedWarpScriptFunction;
import io.warp10.script.WarpScriptException;
import io.warp10.script.WarpScriptStack;
import io.warp10.script.WarpScriptStackFunction;

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

public class TOBD extends NamedWarpScriptFunction implements WarpScriptStackFunction {

public TOBD(String name) {
Expand All @@ -45,7 +45,9 @@ public static BigDecimal toBigDecimal(String name, Object o) throws WarpScriptEx

BigDecimal bd = null;

if (o instanceof String) {
if (o instanceof BigDecimal) {
bd = (BigDecimal) o;
} else if (o instanceof String) {
bd = new BigDecimal((String) o);
} else if (o instanceof Long) {
bd = BigDecimal.valueOf(((Long) o).longValue());
Expand All @@ -55,7 +57,7 @@ public static BigDecimal toBigDecimal(String name, Object o) throws WarpScriptEx
BigInteger bi = new BigInteger((byte[]) o);
bd = new BigDecimal(bi);
} else {
throw new WarpScriptException(name + " can only be applied to a STRING, BYTES, LONG or DOUBLE argument.");
throw new WarpScriptException(name + " can only be applied to a BIGDECIMAL, STRING, BYTES, LONG or DOUBLE argument.");
}

return bd;
Expand Down
9 changes: 6 additions & 3 deletions warp10/src/main/java/io/warp10/script/functions/TYPEOF.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2018-2022 SenX S.A.S.
// Copyright 2018-2023 SenX S.A.S.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -92,6 +92,7 @@ public static interface TypeResolver {
public static final String TYPE_KEY = "KEY";
public static final String TYPE_CONTEXT = "CONTEXT";
public static final String TYPE_FUNCTION = "FUNCTION";
public static final String TYPE_BIGDECIMAL = "BIGDECIMAL";

/**
* Interface to be used by extensions and plugins to define a new type.
Expand Down Expand Up @@ -128,8 +129,10 @@ public static String typeof(Class c) {
return TYPE_STRING;
} else if (Long.class.isAssignableFrom(c) || Integer.class.isAssignableFrom(c) || Short.class.isAssignableFrom(c) || Byte.class.isAssignableFrom(c) || BigInteger.class.isAssignableFrom(c)) {
return TYPE_LONG;
} else if (Double.class.isAssignableFrom(c) || Float.class.isAssignableFrom(c) || BigDecimal.class.isAssignableFrom(c)) {
return TYPE_DOUBLE;
} else if (Double.class.isAssignableFrom(c) || Float.class.isAssignableFrom(c)) {
return TYPE_DOUBLE;
} else if (BigDecimal.class.isAssignableFrom(c)) {
return TYPE_BIGDECIMAL;
} else if (Boolean.class.isAssignableFrom(c)) {
return TYPE_BOOLEAN;
} else if (Vector.class.isAssignableFrom(c)) { // place before List. Vector implements List.
Expand Down

0 comments on commit 1e33949

Please sign in to comment.