• Java开源工具库使用之Apache common-lang3


    前言

    标准的 Java 库未能提供足够的方法来操作其核心类,Apache-commons-lang 提供了许多辅助工具,特别是字符串操作方法、基本数值方法、对象反射、并发、对象创建和序列化以及系统属性。此外,它还包含了对 java.util 的基本增强功能,日期和一系列的实用工具。

    commons-lang 有两个主要版本,lang 和 lang3。lang 是早前版本,于 2002 年发布,而 lang3 是第三个大版本,摒弃了一些弱和旧的特性,不再兼容 lang,包名也更新为 lang3,现在使用主要以 lang3 为主 。

    pom依赖为:

    <dependency>
          <groupId>org.apache.commonsgroupId>
          <artifactId>commons-lang3artifactId>
          <version>3.12.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    下图表现了 commons-lang3 包中 Utils 类的分布

    lang3_utils

    下表描述了上述 Utils 类作用及提供官方测试例子

    类名作用官方测试例子
    AnnotationUtils注解相关AnnotationUtilsTest.java
    ArchUtils操作系统相关,使用System.getProperties("os.arch")系统属性的值ArchUtilsTest.java
    ArrayUtils数组相关,提供数组添加,删除,打乱等方法ArrayUtilsTest.java
    BooleanUtilsboolean 相关,提供 “yes”,“no”,“off”,“on” 转换 boolean 相关方法BooleanUtilsTest.java
    CalendarUtils日历相关CalendarUtilsTest.java
    CharSequenceUtilsnull 安全的字符序列操作方法CharSequenceUtilsTest.java
    CharSetUtils字符集合操作。提供包含,删除,压缩,计数等方法CharSetUtilsTest.java
    CharUtils字符相关CharUtilsTest.java
    ClassLoaderUtils类加载器相关ClassLoaderUtilsTest.java
    ClassPathUtilsClassPath 相关ClassPathUtilsTest.java
    ClassUtils不用反射操作类,提供 Class 对象转类名 String ,类缩略名等方法ClassUtilsTest.java
    ComparableUtils提供将 Comparable.compareTo(T) 结果转换为 boolean 的辅助方法ComparableUtilsTest.java
    ConcurrentUtils并发相关ConcurrentUtilsTest.java
    ConstructorUtils反射相关,专注于构造器ConstructorUtilsTest.java
    DateUtils日期相关DateUtilsTest.java
    DateFormatUtils提供日期和时间格式化实用方法和常量DateFormatUtilsTest.java
    DurationFormatUtils提供 Duration 格式化实用方法和常量DurationFormatUtilsTest.java
    DurationUtilsDuration 相关DurationUtilsTest.java
    EnumUtils枚举相关EnumUtilsTest.java
    EventUtils提供了一些基于事件的实用方法EventUtilsTest.java
    ExceptionUtils异常相关,提供用于操作和检查Throwable对象的方法ExceptionUtilsTest.java
    FieldUtils反射相关,专注于字段FieldUtilsTest.java
    IEEE754rUtils基于 IEEE-754 提供数字相关方法IEEE754rUtilsTest.java
    InheritanceUtils反射相关,主要关注于继承的实用方法InheritanceUtilsTest.java
    LocaleUtilsLocale 相关LocaleUtilsTest.java
    MethodUtils反射相关, 专注于方法MethodUtilsTest.java
    NumberUtils数字相关,提供检测字符串是否合法Java数字,是否可解析为数字等方法NumberUtilsTest.java
    ObjectUtils对象相关,提供对象数组判空,是否相等,最大,最小等方法ObjectUtilsTest.java
    RandomStringUtils随机字符串相关RandomStringUtilsTest.java
    RandomUtils随机数相关RandomUtilsTest.java
    RegExUtils正则相关RegExUtilsTest.java
    SerializationUtils序列化相关SerializationUtilsTest.java
    StringUtils字符串相关StringUtilsTest.java
    SystemUtilsjava.lang.System 相关SystemUtilsTest.java
    ThreadUtils线程相关ThreadUtilsTest.java
    TypeUtils反射相关,侧重于类型检查,特别是关于泛型TypeUtilsTest.java

    一、字符串

    1.1 StringUtils

    • 字符串缩略

      StringUtils.abbreviate("abcdefg", 6); // "abc..."
      
      • 1
    • 字符串 append

      如果字符串尚未以任何后缀结尾,则将后缀附加到字符串的末尾

      StringUtils.appendIfMissing("abcMNO", "xyz", "mno"); // "abcMNOxyz"
      
      • 1
    • 字符串删除末尾换行符

      删除字符串末尾\r, \n 或 \r\n,只会删一次

      StringUtils.chomp("abc\r\n\r\n"); // "abc\r\n"
      
      • 1
    • 字符串删除末尾单个字符

      StringUtils.chop("abc");         // "ab"
      
      • 1
    • 字符串是否包含空白字符

      StringUtils.containsWhitespace("hello world"); // true
      
      • 1
    • 字符串中字符/字符串出现次数

      StringUtils.countMatches("abba", 'a');   // 2
      StringUtils.countMatches("abba", "a");   // 2
      
      • 1
      • 2
    • 字符串去除空白字符

      StringUtils.deleteWhitespace("   ab  c  "); // "abc"
      
      • 1
    • 字符串差异

      StringUtils.difference("abcde", "abxyz"); // "xyz"
      StringUtils.difference("abcde", "xyz"); // "xyz"
      
      • 1
      • 2
    • 字符串是否已某个后缀结束

      StringUtils.endsWith("ABCDEF", "def"); // false
      StringUtils.endsWithAny("abcXYZ", "def", "XYZ"); // true
      StringUtils.endsWithIgnoreCase("ABCDEF", "def"); // true
      
      • 1
      • 2
      • 3
    • 字符串公共前缀

      StringUtils.getCommonPrefix("abcde", "abxyz"); // "ab"
      
      • 1
    • 字符串是否空,null,空白字符

      StringUtils.isBlank(" ");       // true
      StringUtils.isNotBlank(" ");    // false
      
      • 1
      • 2
    • 字符串是否空,null

      StringUtils.isEmpty("");        // true
      StringUtils.isEmpty(" ");       // false
      StringUtils.isNotEmpty(" ");    // true
      
      • 1
      • 2
      • 3
    • 字符串 join

      StringUtils.join(["a", "b", "c"], "--");  // "a--b--c"
      
      • 1
    • 字符串重复

      StringUtils.repeat("ab", 2); // "abab"
      
      • 1
    • 字符串替换

      StringUtils.replace("aba", "a", "z");   // "zbz"
      
      • 1
    • 字符串翻转

      StringUtils.reverse("bat"); // "tab"
      
      • 1
    • 字符串旋转

      StringUtils.rotate("abcdefg", 2);   // "fgabcde"
      StringUtils.rotate("abcdefg", -2);  // "cdefgab"
      
      • 1
      • 2
    • 字符串切割

      StringUtils.split("abc def");  // ["abc", "def"]
      StringUtils.split("abc  def"); // ["abc", "def"]
      StringUtils.split(" abc ");    // ["abc"]
      
      StringUtils.splitByCharacterType("ab de fg");   // ["ab", " ", "de", " ", "fg"]
      StringUtils.splitByCharacterType("ab   de fg"); // ["ab", "   ", "de", " ", "fg"]
      StringUtils.splitByCharacterType("ab:cd:ef");   // ["ab", ":", "cd", ":", "ef"]
      StringUtils.splitByCharacterType("number5");    // ["number", "5"]
      StringUtils.splitByCharacterType("fooBar");     // ["foo", "B", "ar"]
      StringUtils.splitByCharacterType("foo200Bar");  // ["foo", "200", "B", "ar"]
      StringUtils.splitByCharacterType("ASFRules");   // ["ASFR", "ules"]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • 字符串strip

      类似 trim, trim 是删除前后空格,而 trip 删除空白字符

      StringUtils.strip(" ab c "); // "ab c"
      
      • 1
    • 字符串截断

      StringUtils.truncate("abcdefg", 4);  // "abcd"
      
      • 1
    • 字符串包裹

      StringUtils.wrap("ab", "'");       // "'ab'"
      
      • 1

    1.2 CharSetUtils

    • 字符串是否包含字符集合字符

      CharSetUtils.containsAny("hello", "k-p"); // true
      CharSetUtils.containsAny("hello", "a-d"); // false
      
      • 1
      • 2
    • 字符串包含字符集合字符计数

      CharSetUtils.count("hello", "k-p"); // 3
      CharSetUtils.count("hello", "a-e"); // 1
      
      • 1
      • 2
    • 字符串删除字符集合

      CharSetUtils.delete("hello", "hl");  // "eo"
      
      • 1
    • 字符串只保留字符集合字符

      CharSetUtils.keep("hello", "hl");  // "hll"
      
      • 1
    • 字符串压缩字符集合字符

      CharSetUtils.squeeze("hello", "k-p"); // "helo"
      
      • 1

    1.3 RegExUtils

    • 字符串正则删除

      StringUtils.removeAll("any", Pattern.compile(".*"));  // ""
      StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>"));      // "A\nB"
      StringUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>"));  // "AB"
      StringUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"));     // "ABC123"
      
      • 1
      • 2
      • 3
      • 4
    • 字符串正则替换

      StringUtils.replaceAll("", Pattern.compile(".*"), "zzz");  // "zzz"
      StringUtils.replaceAll("", Pattern.compile(".+"), "zzz");  // ""
      StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "");   // "ABC123"
      
      • 1
      • 2
      • 3

    1.4 RandomStringUtils

    • n位长度字符串

      RandomStringUtils.random(10);
      
      • 1
    • n位长度字符串,字符为a-zA-Z

      RandomStringUtils.randomAlphabetic(10);
      
      • 1
    • n位长度字符串,字符为a-zA-Z0-9

      RandomStringUtils.randomAlphanumeric(10);
      
      • 1
    • n位长度字符串, 字符为Ascii字符

      RandomStringUtils.randomAscii(10);
      
      • 1
    • n位长度字符串, 字符为数字

      RandomStringUtils.randomNumeric(10);
      
      • 1

    二、数字

    2.1 NumberUtils

    • 字符串转数字

      NumberUtils.toInt("");   // 0
      NumberUtils.toInt("1");  // 1
          
      NumberUtils.toFloat("1.5");  // 1.5f
          
      NumberUtils.toDouble("1.5");  // 1.5d
          
      NumberUtils.toLong("1");  // 1L
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    • 最大最小值

      NumberUtils.max(1, 2, 3, 4); // 4
      
      NumberUtils.min(1.0f, 2.0f, 3.0f, 4.0f); // 1.0f
      
      • 1
      • 2
      • 3

    2.2 Fraction

    • 分数相加减乘除

      Fraction f1 = Fraction.getFraction(1, 3);
      Fraction f2 = Fraction.getFraction(1, 4);
      
      Fraction add = f1.add(f2); 				// 7/12
      Fraction subtract = f1.subtract(f2); 	// 1/12
      Fraction multi = f1.multiplyBy(f2); 	// 1/12
      Fraction divide = f1.divideBy(f2); 		// 4/3
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 获取分子分母

      f1.getNumerator();  	// 1
      f1.getDenominator();	// 3
      
      • 1
      • 2
    • 根据字符串,double, 分子分母获取分数

      Fraction.getFraction("12.3"); // 123/100
      Fraction.getFraction(12.3d);  // 123/100
      Fraction.getFraction(123, 100); //123/100
      
      • 1
      • 2
      • 3

    三、时间

    3.1 DateUtils

    • 是否相同日期

      DateUtils.isSameDay(date1, date2);
      
      • 1
    • 是否相同时间

      DateUtils.isSameInstant(date1, date2); 
      
      • 1
    • 时间范围迭代器

      
      // 一周以周日为起始,从 now 这周的周日生成一周
      Iterator<Calendar> iterator1 = DateUtils.iterator(now, DateUtils.RANGE_WEEK_SUNDAY); 
      
      // 一周以周一为起始,从 now 这周的周一生成一周
      Iterator<Calendar> iterator = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY);
      
      // 从 now 这个时间的日期作为起始生成一周
      Iterator<Calendar> iterator = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE);
      
      // 从 now 这个时间的日期作为中心生成一周
      Iterator<Calendar> iterator = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
      
      // 一周以周日为起始,从 now 这个月的第一周周日作为起始生成一个月
      Iterator<Calendar> iterator = DateUtils.iterator(now, DateUtils.RANGE_MONTH_SUNDAY);
      
      // 一周以周一为起始,从 now 这个月的第一周周一作为起始生成一个月
      Iterator<Calendar> iterator = DateUtils.iterator(now, DateUtils.RANGE_MONTH_MONDAY);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    • 解析字符串

      DateUtils.parseDate(now, "yyyy-MM--dd HH:mm:ss");
      
      • 1
    • 日期四舍五入

      SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd");
      SimpleDateFormat dateTimeParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
      Date date = dateParser.parse("2022-08-08");
      DateUtils.round(date, Calendar.YEAR);       	// 2023-01-01
      DateUtils.round(date, Calendar.MONTH); 			// 2022-08-01
      
      Date date_half_month = dateParser.parse("2022-08-28");
      DateUtils.round(date, DateUtils.SEMI_MONTH);	// 2022-08-01
      DateUtils.round(date_half_month, DateUtils.SEMI_MONTH);	// 2022-08-16
      
      Date date1 = dateTimeParser.parse("2022-08-08 08:08:08");
      DateUtils.round(date1, Calendar.HOUR);			// 2022-08-08 08:00:00
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 日期截断

      DateUtils.truncate(date, Calendar.YEAR);  // 2022-01-01
      DateUtils.truncate(date, Calendar.MONTH); // 2022-08-01
      
      DateUtils.truncate(date1, Calendar.HOUR); // 2022-08-08 08:00:00
      
      • 1
      • 2
      • 3
      • 4

    3.2 DateFormatUtils

    • 时间格式化
    DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"); // 2022-08-08 00:00:00
      
    DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(date); // 2022-08-08
    DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format(date); // 2022-08-08T00:00:00+08:00
    DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(date); // 2022-08-08T00:00:00
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、数组

    4.1 ArrayUtils

    • 数组增删

      int [] arr = {1, 0};
      ArrayUtils.add(arr, 1); 			// [1, 0, 1]
      ArrayUtils.addFirst(arr, 1); 		// [1, 1, 0]
      ArrayUtils.insert(1, arr, 2, 3);    // [1, 2, 3, 0]
      
      ArrayUtils.remove(arr, 1);          // [1]
      ArrayUtils.removeAll(arr, 0, 1);    // []
      
      int [] arr2 = {1, 0, 1, 0}
      ArrayUtils.removeAllOccurrences(arr2, 1); // [0, 0]
      ArrayUtils.removeElements(arr2, 1);  // [0, 1, 0]
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    • 数组打乱顺序

      ArrayUtils.shuffle(arr);
      
      • 1
    • 移位

      int [] arr2 = {1, 2, 3, 4, 5};
      ArrayUtils.shift(arr, 1);  // [5, 1, 2, 3, 4]
      ArrayUtils.shift(arr, -1); // [2, 3, 4, 5, 1]
      
      • 1
      • 2
      • 3
    • 交换

      ArrayUtils.swap(arr, 0, 1); // [0, 1]
      
      • 1
    • 翻转

      ArrayUtils.reverse(arr); // [0, 1]
      
      • 1

    五、其它

    5.1 RandomUtils

    • 随机整数

      RandomUtils.nextInt(1, 100);
      RandomUtils.nextInt();
      RandomUtils.nextLong(1L, 1000000000L);
      
      • 1
      • 2
      • 3
    • 随机浮点数

      RandomUtils.nextDouble(1.0, 1000000000.0);
      RandomUtils.nextFloat(1.0f, 1000000000.0f);
      
      • 1
      • 2
    • 随机布尔值

      RandomUtils.nextBoolean();
      
      • 1

    5.2 SystemUtils

    • 获取环境变量

      SystemUtils.getEnvironmentVariable("PATH", "default_value");
      
      • 1
    • 获取 Hostname

      SystemUtils.getHostName();
      
      • 1
    • 获取 JavaHome

      SystemUtils.getJavaHome();
      
      • 1
    • 获取 UserHome

      SystemUtils.getUserHome();
      
      • 1
    • 获取操作系统版本

      SystemUtils.OS_VERSION
      
      • 1
    • 获取操作系统名字

      SystemUtils.OS_NAME
      
      • 1
    • 获取 java 版本

      SystemUtils.JAVA_VERSION
      
      • 1

    5.3 ExceptionUtils

    • 获取异常信息

      Throwable th = new IllegalArgumentException("Base");
      ExceptionUtils.getMessage(th); // "IllegalArgumentException: Base"
      
      • 1
      • 2
    • 获取错误的根原因

      private static class ExceptionWithoutCause extends Exception {
      	private static final long serialVersionUID = 1L;
      
          @SuppressWarnings("unused")
          public void getTargetException() {
              // noop
          }
      }
      
      Throwable withoutCause = new ExceptionWithoutCause();
      Throwable nested = new NestableException(withoutCause);
      ExceptionUtils.getRootCause(nested); 
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    • 获取栈帧消息

      String[] actual = ExceptionUtils.getStackFrames(new Throwable() {
                  private static final long serialVersionUID = 1L;
      
                  // provide static stack trace to make test stable
                  @Override
                  public void printStackTrace(final PrintWriter s) {
                      s.write("org.apache.commons.lang3.exception.ExceptionUtilsTest$1\n" +
                          "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)\n" +
                          "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
                          "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)\n" +
                          "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)\n");
                  }
              });
      
      assertArrayEquals(new String[]{
                  "org.apache.commons.lang3.exception.ExceptionUtilsTest$1",
                  "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)",
                  "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
                  "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)",
                  "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)"
          }, actual);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21

    参考

    1. https://commons.apache.org/proper/commons-lang/apidocs/
  • 相关阅读:
    AI+医疗:使用神经网络进行医学影像识别分析 ⛵
    JavaScript基础 | 内置函数知识梳理
    easyscholar配置秘钥连接Zotero-style,更方便的了解文献!
    【无标题】
    洛谷 P5357 【模板】AC 自动机(二次加强版)(AC自动机,fail树)
    springcloud11:Hystrix服务降级(断路器)
    【C数据存储详解】(2)——深度剖析 浮点型数据 在内存中的存取
    JAVA的异常处理
    小学生python游戏编程arcade----基本知识1
    实拆一个风扇
  • 原文地址:https://blog.csdn.net/qq_23091073/article/details/126743040