1、数据类型转换类

1.1、字节数组转为布尔数组

  1. // 101110101 转为布尔数组
  2. static public boolean[] toBitsArray(byte[] bytes, int bitCount) {
  3. boolean[] dst = new boolean[bitCount];
  4. for (int i = 0; i < dst.length; i++) {
  5. dst[i] = (bytes[i / 8] & (1 << (i % 8))) != 0;
  6. }
  7. return dst;
  8. }

1.2、字节数组转为浮点型

  1. public static float toFloatLittleEndian(byte[] bytes) {
  2. return Float.intBitsToFloat(
  3. ((bytes[0] & 0xff) << 8) |
  4. (bytes[1] & 0xff) |
  5. ((bytes[2] & 0xff) << 24) |
  6. ((bytes[3] & 0xff) << 16));
  7. }
  8. public static float toFloatBigEndian(byte[] bytes) {
  9. return Float.intBitsToFloat(
  10. ((bytes[0] & 0xff) << 24) |
  11. ((bytes[1] & 0xff) << 16) |
  12. ((bytes[2] & 0xff) << 8) |
  13. ((bytes[3] & 0xff)));
  14. }
  15. public static float toFloatBigEndianBySwap(byte[] bytes) {
  16. return Float.intBitsToFloat(
  17. ((bytes[0] & 0xff) << 16) |
  18. ((bytes[1] & 0xff) << 24) |
  19. (bytes[2] & 0xff) |
  20. ((bytes[3] & 0xff) << 8));
  21. }
  22. public static float toFloatLittleEndianBySwap(byte[] bytes) {
  23. return Float.intBitsToFloat(
  24. (bytes[0] & 0xff) |
  25. (bytes[1] & 0xff) << 8 |
  26. (bytes[2] & 0xff) << 16 |
  27. (bytes[3] & 0xff) << 24);
  28. }

1.3、字节数组转为整数

  1. //两个字节不带符合
  2. public static int byteArrayToInt(byte[] data) {
  3. return (data[0] & 255) << 8 | data[1] & 255;
  4. }
  5. // 两个字节 带符号
  6. public static short byteArrayToShort(byte[] data) {
  7. return (short) ((data[0] & 255) << 8 | data[1] & 255);
  8. }

2、枚举类示例

  1. public enum FunctionCode {
  2. /**
  3. * 读线圈状态
  4. */
  5. READ_COIL_STATUS("0x01") {
  6. @Override
  7. public SimpleModbusRequest buildRequest(int offset, int num) {
  8. return new ReadCoilsRequest(offset, num);
  9. }
  10. },
  11. /**
  12. * 未知功能码
  13. */
  14. UNKNOWN("0x00") {
  15. @Override
  16. public SimpleModbusRequest buildRequest(int offset, int num) {
  17. return null;
  18. }
  19. };
  20. private final String code;
  21. public String getCode() {
  22. return code;
  23. }
  24. public static FunctionCode isCode(String code) {
  25. Class<FunctionCode> codeClass = FunctionCode.class;
  26. FunctionCode functionCode = UNKNOWN;
  27. try {
  28. for (FunctionCode ele : codeClass.getEnumConstants()) {
  29. if (codeClass.getMethod("getCode").invoke(ele).equals(code)) {
  30. functionCode = ele;
  31. break;
  32. }
  33. }
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. return functionCode;
  38. }
  39. FunctionCode(String code) {
  40. this.code = code;
  41. }
  42. public abstract SimpleModbusRequest buildRequest(int offset, int num);
  43. }

3、打印进度条

  1. Random random = new Random();
  2. for (int i = 0; i <=100 ; i++) {
  3. Thread.sleep(random.nextInt(100));
  4. System.out.print("\r"+i+"%");
  5. }

4、CompletableFuture

参考

6、Map API

  1. map.computeIfAbsent(key, k -> new ArrayList<>())

7、反射

7.1、给属性赋值,获取值

  1. PropertyDescriptor pd = new PropertyDescriptor(name, cls);
  2. Method setMethod = pd.getWriteMethod();
  3. setMethod.invoke(object, value);
  4. Method getMethod=pd.getReadMethod();
  5. getMethod.invoke(object)
  6. // apache 工具包 beanutils
  7. BeanUtils.setProperty(ipNetToMediaEntry, name, variable.toString());