tbel: refactoring tbUtils with docs

This commit is contained in:
nick 2024-09-02 21:44:23 +03:00
parent 04e4116f30
commit b86a9d3802
2 changed files with 68 additions and 41 deletions

View File

@ -273,13 +273,13 @@ public class TbUtils {
Long.class, boolean.class, boolean.class))); Long.class, boolean.class, boolean.class)));
parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex",
Long.class, boolean.class, boolean.class, int.class))); Long.class, boolean.class, boolean.class, int.class)));
parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", parserConfig.addImport("intLongToRadixString", new MethodStub(TbUtils.class.getMethod("intLongToRadixString",
Long.class))); Long.class)));
parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", parserConfig.addImport("intLongToRadixString", new MethodStub(TbUtils.class.getMethod("intLongToRadixString",
Long.class, int.class))); Long.class, int.class)));
parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", parserConfig.addImport("intLongToRadixString", new MethodStub(TbUtils.class.getMethod("intLongToRadixString",
Long.class, int.class, boolean.class))); Long.class, int.class, boolean.class)));
parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", parserConfig.addImport("intLongToRadixString", new MethodStub(TbUtils.class.getMethod("intLongToRadixString",
Long.class, int.class, boolean.class, boolean.class))); Long.class, int.class, boolean.class, boolean.class)));
parserConfig.addImport("floatToHex", new MethodStub(TbUtils.class.getMethod("floatToHex", parserConfig.addImport("floatToHex", new MethodStub(TbUtils.class.getMethod("floatToHex",
Float.class))); Float.class)));
@ -327,7 +327,7 @@ public class TbUtils {
String.class))); String.class)));
parserConfig.addImport("isHexadecimal", new MethodStub(TbUtils.class.getMethod("isHexadecimal", parserConfig.addImport("isHexadecimal", new MethodStub(TbUtils.class.getMethod("isHexadecimal",
String.class))); String.class)));
parserConfig.addImport("byteArrayToExecutionArrayList", new MethodStub(TbUtils.class.getMethod("bytesToExecutionArrayList", parserConfig.addImport("bytesToExecutionArrayList", new MethodStub(TbUtils.class.getMethod("bytesToExecutionArrayList",
ExecutionContext.class, byte[].class))); ExecutionContext.class, byte[].class)));
parserConfig.addImport("padStart", new MethodStub(TbUtils.class.getMethod("padStart", parserConfig.addImport("padStart", new MethodStub(TbUtils.class.getMethod("padStart",
String.class, int.class, char.class))); String.class, int.class, char.class)));
@ -725,19 +725,19 @@ public class TbUtils {
return prepareNumberHexString(l, bigEndian, pref, len, HEX_LEN_LONG_MAX); return prepareNumberHexString(l, bigEndian, pref, len, HEX_LEN_LONG_MAX);
} }
public static String intLongToString(Long number) { public static String intLongToRadixString(Long number) {
return intLongToString(number, DEC_RADIX); return intLongToRadixString(number, DEC_RADIX);
} }
public static String intLongToString(Long number, int radix) { public static String intLongToRadixString(Long number, int radix) {
return intLongToString(number, radix, true); return intLongToRadixString(number, radix, true);
} }
public static String intLongToString(Long number, int radix, boolean bigEndian) { public static String intLongToRadixString(Long number, int radix, boolean bigEndian) {
return intLongToString(number, radix, bigEndian, false); return intLongToRadixString(number, radix, bigEndian, false);
} }
public static String intLongToString(Long number, int radix, boolean bigEndian, boolean pref) { public static String intLongToRadixString(Long number, int radix, boolean bigEndian, boolean pref) {
if (radix >= 25 && radix <= MAX_RADIX) { if (radix >= 25 && radix <= MAX_RADIX) {
return Long.toString(number, radix); return Long.toString(number, radix);
} }
@ -1344,8 +1344,8 @@ public class TbUtils {
* Writes the bit value to the appropriate location in the bins array, starting at the end of the array, * Writes the bit value to the appropriate location in the bins array, starting at the end of the array,
* to ensure proper alignment (highest bit to low end). * to ensure proper alignment (highest bit to low end).
*/ */
public static byte[] parseLongToBinaryArray(long longValue, int binsLength) { public static byte[] parseLongToBinaryArray(long longValue, int binLength) {
int len = Math.min(binsLength, BYTES_LEN_LONG_MAX * BIN_LEN_MAX); int len = Math.min(binLength, BYTES_LEN_LONG_MAX * BIN_LEN_MAX);
byte[] bins = new byte[len]; byte[] bins = new byte[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
bins[len - 1 - i] = (byte) ((longValue >> i) & 1); bins[len - 1 - i] = (byte) ((longValue >> i) & 1);

View File

@ -330,8 +330,8 @@ public class TbUtilsTest {
@Test @Test
public void parseBytesIntToFloat() { public void parseBytesIntToFloat() {
byte[] intValByte = {0x00, 0x00, 0x00, 0x0A}; byte[] intValByte = {0x00, 0x00, 0x00, 0x0A};
Float valueExpected = 10.0f; float valueExpected = 10.0f;
Float valueActual = TbUtils.parseBytesIntToFloat(intValByte, 3, 1, true); float valueActual = TbUtils.parseBytesIntToFloat(intValByte, 3, 1, true);
Assertions.assertEquals(valueExpected, valueActual); Assertions.assertEquals(valueExpected, valueActual);
valueActual = TbUtils.parseBytesIntToFloat(intValByte, 3, 1, false); valueActual = TbUtils.parseBytesIntToFloat(intValByte, 3, 1, false);
Assertions.assertEquals(valueExpected, valueActual); Assertions.assertEquals(valueExpected, valueActual);
@ -345,20 +345,23 @@ public class TbUtilsTest {
valueExpected = 10.0f; valueExpected = 10.0f;
valueActual = TbUtils.parseBytesIntToFloat(intValByte, 0, 4, true); valueActual = TbUtils.parseBytesIntToFloat(intValByte, 0, 4, true);
Assertions.assertEquals(valueExpected, valueActual); Assertions.assertEquals(valueExpected, valueActual);
valueExpected = 1.6777216E8f; valueExpected = 167772.16f;
valueActual = TbUtils.parseBytesIntToFloat(intValByte, 0, 4, false); double factor = 1e3;
valueActual = (float) (TbUtils.parseBytesIntToFloat(intValByte, 0, 4, false) / factor);
Assertions.assertEquals(valueExpected, valueActual); Assertions.assertEquals(valueExpected, valueActual);
String dataAT101 = "0x01756403671B01048836BF7701F000090722050000"; String dataAT101 = "0x01756403671B01048836BF7701F000090722050000";
List<Byte> byteAT101 = TbUtils.hexToBytes(ctx, dataAT101); List<Byte> byteAT101 = TbUtils.hexToBytes(ctx, dataAT101);
float latitudeExpected = 24.62495f; float latitudeExpected = 24.62495f;
int offset = 9; int offset = 9;
valueActual = TbUtils.parseBytesIntToFloat(byteAT101, offset, 4, false); factor = 1e6;
Assertions.assertEquals(latitudeExpected, valueActual / 1000000); valueActual = (float) (TbUtils.parseBytesIntToFloat(byteAT101, offset, 4, false) / factor);
Assertions.assertEquals(latitudeExpected, valueActual);
float longitudeExpected = 118.030576f; float longitudeExpected = 118.030576f;
valueActual = TbUtils.parseBytesIntToFloat(byteAT101, offset + 4, 4, false); valueActual = (float) (TbUtils.parseBytesIntToFloat(byteAT101, offset + 4, 4, false) / factor);
Assertions.assertEquals(longitudeExpected, valueActual / 1000000); Assertions.assertEquals(longitudeExpected, valueActual);
valueExpected = 9.185175E8f; valueExpected = 9.185175E8f;
valueActual = TbUtils.parseBytesIntToFloat(byteAT101, offset); valueActual = TbUtils.parseBytesIntToFloat(byteAT101, offset);
@ -520,6 +523,18 @@ public class TbUtilsTest {
valueExpected = 7.2057594037927936E17d; valueExpected = 7.2057594037927936E17d;
valueActual = TbUtils.parseBytesLongToDouble(longValByte, 0, 8, false); valueActual = TbUtils.parseBytesLongToDouble(longValByte, 0, 8, false);
Assertions.assertEquals(valueExpected, valueActual); Assertions.assertEquals(valueExpected, valueActual);
String dataPalacKiyv = "0x32D009423F23B300B0106E08D96B6C00";
List<Byte> byteAT101 = TbUtils.hexToBytes(ctx, dataPalacKiyv);
double latitudeExpected = 50.422775429058610d;
int offset = 0;
double factor = 1e15;
valueActual = TbUtils.parseBytesLongToDouble(byteAT101, offset, 8, false);
Assertions.assertEquals(latitudeExpected, valueActual / factor);
double longitudeExpected = 30.517877378257072d;
valueActual = TbUtils.parseBytesLongToDouble(byteAT101, offset + 8, 8, false);
Assertions.assertEquals(longitudeExpected, valueActual / factor);
} }
@Test @Test
@ -720,27 +735,27 @@ public class TbUtilsTest {
@Test @Test
public void numberToString_Test() { public void numberToString_Test() {
Assertions.assertEquals("00111010", TbUtils.intLongToString(58L, MIN_RADIX)); Assertions.assertEquals("00111010", TbUtils.intLongToRadixString(58L, MIN_RADIX));
Assertions.assertEquals("0000000010011110", TbUtils.intLongToString(158L, MIN_RADIX)); Assertions.assertEquals("0000000010011110", TbUtils.intLongToRadixString(158L, MIN_RADIX));
Assertions.assertEquals("00000000000000100000001000000001", TbUtils.intLongToString(131585L, MIN_RADIX)); Assertions.assertEquals("00000000000000100000001000000001", TbUtils.intLongToRadixString(131585L, MIN_RADIX));
Assertions.assertEquals("0111111111111111111111111111111111111111111111111111111111111111", TbUtils.intLongToString(Long.MAX_VALUE, MIN_RADIX)); Assertions.assertEquals("0111111111111111111111111111111111111111111111111111111111111111", TbUtils.intLongToRadixString(Long.MAX_VALUE, MIN_RADIX));
Assertions.assertEquals("1000000000000000000000000000000000000000000000000000000000000001", TbUtils.intLongToString(-Long.MAX_VALUE, MIN_RADIX)); Assertions.assertEquals("1000000000000000000000000000000000000000000000000000000000000001", TbUtils.intLongToRadixString(-Long.MAX_VALUE, MIN_RADIX));
Assertions.assertEquals("1111111111111111111111111111111111111111111111111111111110011010", TbUtils.intLongToString(-102L, MIN_RADIX)); Assertions.assertEquals("1111111111111111111111111111111111111111111111111111111110011010", TbUtils.intLongToRadixString(-102L, MIN_RADIX));
Assertions.assertEquals("1111111111111111111111111111111111111111111111111100110010011010", TbUtils.intLongToString(-13158L, MIN_RADIX)); Assertions.assertEquals("1111111111111111111111111111111111111111111111111100110010011010", TbUtils.intLongToRadixString(-13158L, MIN_RADIX));
Assertions.assertEquals("777777777777777777777", TbUtils.intLongToString(Long.MAX_VALUE, 8)); Assertions.assertEquals("777777777777777777777", TbUtils.intLongToRadixString(Long.MAX_VALUE, 8));
Assertions.assertEquals("1000000000000000000000", TbUtils.intLongToString(Long.MIN_VALUE, 8)); Assertions.assertEquals("1000000000000000000000", TbUtils.intLongToRadixString(Long.MIN_VALUE, 8));
Assertions.assertEquals("9223372036854775807", TbUtils.intLongToString(Long.MAX_VALUE)); Assertions.assertEquals("9223372036854775807", TbUtils.intLongToRadixString(Long.MAX_VALUE));
Assertions.assertEquals("-9223372036854775808", TbUtils.intLongToString(Long.MIN_VALUE)); Assertions.assertEquals("-9223372036854775808", TbUtils.intLongToRadixString(Long.MIN_VALUE));
Assertions.assertEquals("3366", TbUtils.intLongToString(13158L, 16)); Assertions.assertEquals("3366", TbUtils.intLongToRadixString(13158L, 16));
Assertions.assertEquals("FFCC9A", TbUtils.intLongToString(-13158L, 16)); Assertions.assertEquals("FFCC9A", TbUtils.intLongToRadixString(-13158L, 16));
Assertions.assertEquals("0xFFCC9A", TbUtils.intLongToString(-13158L, 16, true, true)); Assertions.assertEquals("0xFFCC9A", TbUtils.intLongToRadixString(-13158L, 16, true, true));
Assertions.assertEquals("0x0400", TbUtils.intLongToString(1024L, 16, true, true)); Assertions.assertEquals("0x0400", TbUtils.intLongToRadixString(1024L, 16, true, true));
Assertions.assertNotEquals("400", TbUtils.intLongToString(1024L, 16)); Assertions.assertNotEquals("400", TbUtils.intLongToRadixString(1024L, 16));
Assertions.assertEquals("0xFFFC00", TbUtils.intLongToString(-1024L, 16, true, true)); Assertions.assertEquals("0xFFFC00", TbUtils.intLongToRadixString(-1024L, 16, true, true));
Assertions.assertNotEquals("0xFC00", TbUtils.intLongToString(-1024L, 16, true, true)); Assertions.assertNotEquals("0xFC00", TbUtils.intLongToRadixString(-1024L, 16, true, true));
Assertions.assertEquals("hazelnut", TbUtils.intLongToString(1356099454469L, MAX_RADIX)); Assertions.assertEquals("hazelnut", TbUtils.intLongToRadixString(1356099454469L, MAX_RADIX));
} }
@Test @Test
@ -1067,6 +1082,18 @@ public class TbUtilsTest {
String actual = TbUtils.hexToBase64(hex); String actual = TbUtils.hexToBase64(hex);
Assertions.assertEquals(expected, actual); Assertions.assertEquals(expected, actual);
} }
@Test
public void bytesToHex_Test() {
byte[] bb = {(byte) 0xBB, (byte) 0xAA};
String expected = "BBAA";
String actual = TbUtils.bytesToHex(bb);
Assertions.assertEquals(expected, actual);
ExecutionArrayList<Integer> expectedList = new ExecutionArrayList<>(ctx);
expectedList.addAll(List.of(-69, 83));
expected = "BB53";
actual = TbUtils.bytesToHex(expectedList);
Assertions.assertEquals(expected, actual);
}
private static List<Byte> toList(byte[] data) { private static List<Byte> toList(byte[] data) {
List<Byte> result = new ArrayList<>(data.length); List<Byte> result = new ArrayList<>(data.length);