Browse Source

moved number utility method to a new utility class

pull/14790/head
IrynaMatveieva 7 months ago
parent
commit
0e9756f8ce
  1. 2
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/SimpleCalculatedFieldState.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/AvgAggEntry.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/MaxAggEntry.java
  4. 2
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/MinAggEntry.java
  5. 2
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/SumAggEntry.java
  6. 2
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/single/EntityAggregationCalculatedFieldState.java
  7. 32
      application/src/test/java/org/thingsboard/server/utils/NumberUtilsTest.java
  8. 27
      common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java
  9. 27
      common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java
  10. 22
      common/util/src/main/java/org/thingsboard/common/util/NumberUtils.java
  11. 61
      common/util/src/test/java/org/thingsboard/common/util/NumberUtilsTest.java

2
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/SimpleCalculatedFieldState.java

@ -28,7 +28,7 @@ import org.thingsboard.server.common.data.cf.configuration.Output;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.service.cf.CalculatedFieldResult;
import org.thingsboard.server.service.cf.TelemetryCalculatedFieldResult;
import org.thingsboard.server.utils.NumberUtils;
import org.thingsboard.common.util.NumberUtils;
import java.util.Map;

2
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/AvgAggEntry.java

@ -16,7 +16,7 @@
package org.thingsboard.server.service.cf.ctx.state.aggregation.function;
import org.thingsboard.server.common.data.cf.configuration.aggregation.AggFunction;
import org.thingsboard.server.utils.NumberUtils;
import org.thingsboard.common.util.NumberUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;

2
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/MaxAggEntry.java

@ -16,7 +16,7 @@
package org.thingsboard.server.service.cf.ctx.state.aggregation.function;
import org.thingsboard.server.common.data.cf.configuration.aggregation.AggFunction;
import org.thingsboard.server.utils.NumberUtils;
import org.thingsboard.common.util.NumberUtils;
public class MaxAggEntry extends BaseAggEntry {

2
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/MinAggEntry.java

@ -16,7 +16,7 @@
package org.thingsboard.server.service.cf.ctx.state.aggregation.function;
import org.thingsboard.server.common.data.cf.configuration.aggregation.AggFunction;
import org.thingsboard.server.utils.NumberUtils;
import org.thingsboard.common.util.NumberUtils;
public class MinAggEntry extends BaseAggEntry {

2
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/function/SumAggEntry.java

@ -16,7 +16,7 @@
package org.thingsboard.server.service.cf.ctx.state.aggregation.function;
import org.thingsboard.server.common.data.cf.configuration.aggregation.AggFunction;
import org.thingsboard.server.utils.NumberUtils;
import org.thingsboard.common.util.NumberUtils;
import java.math.BigDecimal;

2
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/single/EntityAggregationCalculatedFieldState.java

@ -40,7 +40,7 @@ import org.thingsboard.server.service.cf.ctx.state.ArgumentEntry;
import org.thingsboard.server.service.cf.ctx.state.BaseCalculatedFieldState;
import org.thingsboard.server.service.cf.ctx.state.CalculatedFieldCtx;
import org.thingsboard.server.service.cf.ctx.state.SingleValueArgumentEntry;
import org.thingsboard.server.utils.NumberUtils;
import org.thingsboard.common.util.NumberUtils;
import java.time.Instant;
import java.time.ZoneId;

32
application/src/test/java/org/thingsboard/server/utils/NumberUtilsTest.java

@ -1,32 +0,0 @@
/**
* Copyright © 2016-2026 The Thingsboard Authors
*
* Licensed 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.thingsboard.server.utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class NumberUtilsTest {
private final double doubleVal = 1729.1729;
@Test
public void roundResult() {
Assertions.assertEquals(1729.1729, NumberUtils.roundResult(doubleVal, null));
Assertions.assertEquals(1729, NumberUtils.roundResult(doubleVal, 0));
Assertions.assertEquals(1729.17, NumberUtils.roundResult(doubleVal, 2));
}
}

27
common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java

@ -26,6 +26,7 @@ import org.mvel2.execution.ExecutionHashMap;
import org.mvel2.execution.ExecutionLinkedHashSet;
import org.mvel2.util.MethodStub;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.NumberUtils;
import org.thingsboard.common.util.geo.Coordinates;
import org.thingsboard.common.util.geo.GeoUtil;
import org.thingsboard.common.util.geo.RangeUnit;
@ -35,7 +36,6 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
@ -258,13 +258,13 @@ public class TbUtils {
byte[].class, int.class, int.class)));
parserConfig.addImport("parseBytesLongToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesLongToDouble",
byte[].class, int.class, int.class, boolean.class)));
parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed",
parserConfig.addImport("toFixed", new MethodStub(NumberUtils.class.getMethod("toFixed",
double.class, int.class)));
parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed",
parserConfig.addImport("toFixed", new MethodStub(NumberUtils.class.getMethod("toFixed",
float.class, int.class)));
parserConfig.addImport("toInt", new MethodStub(TbUtils.class.getMethod("toInt",
parserConfig.addImport("toInt", new MethodStub(NumberUtils.class.getMethod("toInt",
double.class)));
parserConfig.addImport("isNaN", new MethodStub(TbUtils.class.getMethod("isNaN",
parserConfig.addImport("isNaN", new MethodStub(NumberUtils.class.getMethod("isNaN",
double.class)));
parserConfig.addImport("hexToBytes", new MethodStub(TbUtils.class.getMethod("hexToBytes",
ExecutionContext.class, String.class)));
@ -1174,22 +1174,6 @@ public class TbUtils {
return new String(hexChars, StandardCharsets.UTF_8);
}
public static double toFixed(double value, int precision) {
return BigDecimal.valueOf(value).setScale(precision, RoundingMode.HALF_UP).doubleValue();
}
public static float toFixed(float value, int precision) {
return BigDecimal.valueOf(value).setScale(precision, RoundingMode.HALF_UP).floatValue();
}
public static int toInt(double value) {
return BigDecimal.valueOf(value).setScale(0, RoundingMode.HALF_UP).intValue();
}
public static boolean isNaN(double value) {
return Double.isNaN(value);
}
public static ExecutionHashMap<String, Object> toFlatMap(ExecutionContext ctx, Map<String, Object> json) {
return toFlatMap(ctx, json, new ArrayList<>(), true);
}
@ -1208,7 +1192,6 @@ public class TbUtils {
return map;
}
public static String encodeURI(String uri) {
String encoded = URLEncoder.encode(uri, StandardCharsets.UTF_8);
for (var entry : mdnEncodingReplacements.entrySet()) {

27
common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

@ -279,13 +279,6 @@ public class TbUtilsTest {
Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseLittleEndianHexToFloat(floatValHexRev)));
}
@Test
public void toFixedFloat() {
float actualF = TbUtils.toFixed(floatVal, 3);
Assertions.assertEquals(1, Float.compare(floatVal, actualF));
Assertions.assertEquals(0, Float.compare(29.298f, actualF));
}
@Test
public void parseBytesToFloat() {
byte[] floatValByte = {0x0A};
@ -461,13 +454,6 @@ public class TbUtilsTest {
Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseLittleEndianHexToDouble(longValHexRev)));
}
@Test
public void toFixedDouble() {
double actualD = TbUtils.toFixed(doubleVal, 3);
Assertions.assertEquals(-1, Double.compare(doubleVal, actualD));
Assertions.assertEquals(0, Double.compare(1729.173, actualD));
}
@Test
public void parseBytesToDouble() {
byte[] doubleValByte = {0x0A};
@ -1147,19 +1133,6 @@ public class TbUtilsTest {
Assertions.assertEquals(expected, actual);
}
@Test
public void toInt() {
Assertions.assertEquals(1729, TbUtils.toInt(doubleVal));
Assertions.assertEquals(13, TbUtils.toInt(12.8));
Assertions.assertEquals(28, TbUtils.toInt(28.0));
}
@Test
public void isNaN() {
assertFalse(TbUtils.isNaN(doubleVal));
assertTrue(TbUtils.isNaN(Double.NaN));
}
@Test
public void isInsidePolygon() {
// outside the polygon

22
application/src/main/java/org/thingsboard/server/utils/NumberUtils.java → common/util/src/main/java/org/thingsboard/common/util/NumberUtils.java

@ -13,13 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.utils;
package org.thingsboard.common.util;
import static org.thingsboard.script.api.tbel.TbUtils.toFixed;
import static org.thingsboard.script.api.tbel.TbUtils.toInt;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class NumberUtils {
public static boolean isNaN(double value) {
return Double.isNaN(value);
}
public static double toFixed(double value, int precision) {
return BigDecimal.valueOf(value).setScale(precision, RoundingMode.HALF_UP).doubleValue();
}
public static float toFixed(float value, int precision) {
return BigDecimal.valueOf(value).setScale(precision, RoundingMode.HALF_UP).floatValue();
}
public static int toInt(double value) {
return BigDecimal.valueOf(value).setScale(0, RoundingMode.HALF_UP).intValue();
}
public static Object roundResult(double value, Integer precision) {
if (precision == null) {
return value;

61
common/util/src/test/java/org/thingsboard/common/util/NumberUtilsTest.java

@ -0,0 +1,61 @@
/**
* Copyright © 2016-2026 The Thingsboard Authors
*
* Licensed 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.thingsboard.common.util;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class NumberUtilsTest {
private final Float floatVal = 29.29824f;
private final double doubleVal = 1729.1729;
@Test
public void isNaN() {
assertThat(NumberUtils.isNaN(doubleVal)).isFalse();
assertThat(NumberUtils.isNaN(Double.NaN)).isTrue();
}
@Test
public void toFixedFloat() {
float actualF = NumberUtils.toFixed(floatVal, 3);
assertThat(Float.compare(floatVal, actualF)).isEqualTo(1);
assertThat(Float.compare(29.298f, actualF)).isEqualTo(0);
}
@Test
public void toFixedDouble() {
double actualD = NumberUtils.toFixed(doubleVal, 3);
assertThat(Double.compare(doubleVal, actualD)).isEqualTo(-1);
assertThat(Double.compare(1729.173, actualD)).isEqualTo(0);
}
@Test
public void toInt() {
assertThat(NumberUtils.toInt(doubleVal)).isEqualTo(1729);
assertThat(NumberUtils.toInt(12.8)).isEqualTo(13);
assertThat(NumberUtils.toInt(28.0)).isEqualTo(28);
}
@Test
public void roundResult() {
assertThat(NumberUtils.roundResult(doubleVal, null)).isEqualTo(1729.1729);
assertThat(NumberUtils.roundResult(doubleVal, 0)).isEqualTo(1729);
assertThat(NumberUtils.roundResult(doubleVal, 2)).isEqualTo(1729.17);
}
}
Loading…
Cancel
Save