通用工具类

1. 时间工具类

  1. 月份枚举

    package cn.flyrise.common.util.date;
    
    public enum MonthEnum {
      JAN(1, "1月"),
      FEB(2, "2月"),
      MAR(3, "3月"),
      APR(4, "4月"),
      MAY(5, "5月"),
      JUN(6, "6月"),
      JUL(7, "7月"),
      AUG(8, "8月"),
      SEP(9, "9月"),
      OCT(10, "10月"),
      NOV(11, "11月"),
      DEC(12, "12月");
    }
  2. 季度枚举

    package cn.flyrise.common.util.date;
    
    public enum QuarterEnum {
      Q1(1, "第一季度", "01-01"),
      Q2(2, "第二季度", "04-01"),
      Q3(3, "第三季度", "07-01"),
      Q4(4, "第四季度", "10-01");
    }
  3. 时间区间实体类

    package cn.flyrise.common.util.date;
    
    @Api("时间区间实体类")
    public class DateRange {
    
      @ApiModelProperty("起始时间")
      private Date begin;
    
      @ApiModelProperty("结束时间")
      private Date end;
    }
  4. 时间区间区域获取接口

    package cn.flyrise.common.util.date;
    
    @Api("时间区间区域接口")
    public interface IDateRange {
    
      /**
       * 获取当前时间对应的时间区间
       *
       * @return 时间区间
       */
      DateRange get();
    
      /**
       * 获取指定时间的区间
       *
       * @param date 指定时间
       * @return 时间区间
       */
      DateRange get(Date date);
    }

    实现类:

    package cn.flyrise.common.util.date.impl;
    1. 年度区间(YearRange):获取当前时间或指定时间的年度起始时间、终止时间

    2. 季度区间(QuarterRange):获取当前时间或指定时间的季度起始时间、终止时间

    3. 月度区间(MonthRange):获取当前时间或指定时间的月份起始时间、终止时间

    4. 周区间(WeekRange):获取当前时间或指定时间的星期起始时间、终止时间

    5. 上半年区间(FirstHalfOfYearRange):获取当前时间或指定时间的上半年起始时间、终止时间

    6. 下半年区间(SecondHalfOfYearRange):获取当前时间或指定时间的下半年起始时间、终止时间

  1. 时间区间枚举

    public enum DateRangeTypeEnum {
      /**
       * 年度
       */
      YEAR(1),
      /**
       * 季度
       */
      QUARTER(2),
      /**
       * 月
       */
      MONTH(3),
      /**
       * 周
       */
      WEEK(4),
      /**
       * 上半年
       */
      FIRST_HALF_OF_YEAR(5),
      /**
       * 下半年
       */
      SECOND_HALF_OF_YEAR(6);
    }
  2. 时间区间工具类

    package cn.flyrise.common.util.date;
    
    public class DateRangeUtil {
          /**
       * 获取当前时间内对应类型的时间区间
       *
       * @param type 时间区间枚举
       * @return 时间区间
       */
      public static DateRange get(DateRangeTypeEnum type) {
        return get(type, DateUtil.date());
      }
    
      /**
       * 获取指定时间内对应类型的时间区间
       *
       * @param type 时间区间枚举
       * @param date 年份
       * @return 时间区间
       */
      public static DateRange get(DateRangeTypeEnum type, Date date) {
        DateRange range = DATE_RANGE_MAP.get(type).get(date);
        //据中台的说法数据库会自动将 23:59:59 四舍五入 到第二天 00:00:00,此处-999进行修正
        range.setEnd(DateUtil.date(range.getEnd().getTime() - 999));
        return range;
      } 
    }

    使用示例,查询某时间区间段的数量:

    public int count(int dateType) {
      DateRange range = DateRangeUtil.get(DateRangeTypeEnum.of(dateType));
      int cnt;
      if (null != range) {
        cnt = baseMapper.count(range.getBegin(), range.getEnd());
      } else {
        throw new OutOfRangeException("dateType", "0", "6");
      }
      return cnt;
    }

2. 正则表达式常量

cn.hutool.core.lang.PatternPool中已存在部分正则表达式,无谓重复造轮子

package cn.flyrise.common.util.regular;

public class RegExpression {

  private RegExpression() {
  }

  /**
   * PatternPool.MOBILE 手机号正则表达式,用于VO验证
   */
  public static final String MOBILE = PatternPool.MOBILE.pattern();
}

3. 通用线程池

package cn.flyrise.common.util.thread;

public class CommonThreadPool {
    // 网上推荐线程池相关,可根据实际情况调优
  private static final int CORE_SIZE = Runtime.getRuntime().availableProcessors() + 1;
  private static final int MAX_SIZE = Runtime.getRuntime().availableProcessors() << 1;
  private static final int QUEUE_SIZE = Runtime.getRuntime().availableProcessors() << 5;
  private static final int KEEP_ALIVE_TIME = 1;

  /**
   * 创建线程池
   *
   * @param prefix 线程名称前缀
   * @return 线程池
   */
  public static ThreadPoolExecutor newThreadPoolExecutor(String prefix) {
    return new ThreadPoolExecutor(CORE_SIZE, MAX_SIZE, KEEP_ALIVE_TIME, TimeUnit.MINUTES,
        new ArrayBlockingQueue<>(QUEUE_SIZE), new NamedThreadFactory(prefix, false));
  }
}

使用示例:

  1. 创建ThreadPoolUtil类,自定义线程名称前缀

    public class ThreadPoolUtil {
    
      private static final String THREAD_NAME_PREFIX = "BUSINESS-LEASE-";
      private static final ThreadPoolExecutor EXECUTOR = CommonThreadPool
          .newThreadPoolExecutor(THREAD_NAME_PREFIX);
    
      /**
       * 执行线程任务
       *
       * @param command 任务
       */
      public static void execute(Runnable command) {
        EXECUTOR.execute(command);
      }
    
      /**
       * 获取线程池
       *
       * @return 线程池
       */
      public static ThreadPoolExecutor getExecutor() {
        return EXECUTOR;
      }
    }
  2. 使用

    // 1、 直接使用execte方法
    ThreadPoolUtil.execute(() -> {
      //转换租赁意向为意向客户业务
      leaseConverter.convertIntention(vo);
    });
    
    //2、 使用线程池
    // 批量验证数据合法性
    CompletableFuture<StaffEntity> staffFuture = CompletableFuture.supplyAsync(() -> 
        staffService.findByIdNoAuth(vo.getSigner(), FROM_IN).getData(), ThreadPoolUtil.getExecutor());
    CompletableFuture<DeptEntity> deptFuture = CompletableFuture.supplyAsync(() ->
        deptService.findSimpleById(vo.getDeptId(), FROM_IN).getData(), ThreadPoolUtil.getExecutor());
    CompletableFuture<ParkModel> parkFuture = CompletableFuture.supplyAsync(() ->
        parkService.findParkByIdNoAuth(vo.getParkId(), FROM_IN).getData(), ThreadPoolUtil.getExecutor());
    CompletableFuture<String> entFuture = CompletableFuture.supplyAsync(() ->
    enterpriseService.queryName(vo.getEnterpriseId(), FROM_IN).getData(), ThreadPoolUtil.getExecutor());

4. CastUtils

/**
 * copy from org.springframework.data.util.CastUtils,在没有使用data包的情况可使用此类
 */
public interface CastUtils {

  /**
   * 抑制ide提示warnings
   *
   * @param object 转换对象
   * @param <T>    任意类型
   * @return 转换后的对象
   */
  @SuppressWarnings("unchecked")
  static <T> T cast(Object object) {
    return (T) object;
  }
}

5. 日期验证类

package cn.flyrise.common.valid;

public class DateValidator {


  /**
   * 校验年份是否在合理范围
   *
   * @param year 年份
   */
  public static void year(Integer year) {
    //...
  }

  /**
   * 校验区间是否在合理范围
   *
   * @param section 区间
   */
  public static void section(Integer section) {
    //...
  }

  /**
   * 校验月份是否在合理范围
   *
   * @param month 月份
   */
  public static void month(Integer month) {
    //...
  }
}

6. bean对象属性复制类

public class BeanUtils {

  /**
   * 复制bean属性,忽略tenant_id字段
   *
   * @param source bean对象
   * @param clazz  bean类型
   * @return 填充后的对象
   */
  public static <T> T copyProperties(Object source, Class<T> clazz) {
    return BeanUtil.copyProperties(source, clazz, TENANT_ID);
  }

  /**
   * 复制bean属性,忽略id、tenant_id字段
   *
   * @param source bean对象
   * @param clazz  bean类型
   * @return 填充后的对象
   */
  public static <T> T copyInsertProperties(Object source, Class<T> clazz) {
    return BeanUtil.copyProperties(source, clazz, TENANT_ID, ID);
  }
}
文档更新时间: 2022-03-23 22:57   作者:陆鸿睿