CollectionTools#isNotEmpty 支持 guava 的 Table、Multimap、Multiset 和 RangeSet

This commit is contained in:
2025-02-14 16:57:45 +08:00
parent d217e8b9ac
commit e3ff5a2ab3
2 changed files with 108 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,11 @@ import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.RangeSet;
import com.google.common.collect.Table;
/**
* 集合工具类
*
@@ -33,9 +38,9 @@ import javax.annotation.Nullable;
*/
public class CollectionTools {
// TODO [添加] 新增其它集合类型,如 guava 的扩展集合等
// isEmpty
// ================================
// #region - isEmpty
// ================================
public static boolean isEmpty(@Nullable Collection<?> collection) {
return collection == null || collection.isEmpty();
@@ -45,7 +50,29 @@ public class CollectionTools {
return map == null || map.isEmpty();
}
// isNotEmpty
public static boolean isEmpty(@Nullable Table<?, ?, ?> table) {
return table == null || table.isEmpty();
}
public static boolean isEmpty(@Nullable Multimap<?, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isEmpty(@Nullable Multiset<?> set) {
return set == null || set.isEmpty();
}
public static boolean isEmpty(@Nullable RangeSet<?> set) {
return set == null || set.isEmpty();
}
// ================================
// #endregion - isEmpty
// ================================
// ================================
// #region - isNotEmpty
// ================================
public static boolean isNotEmpty(@Nullable Collection<?> collection) {
return collection != null && !collection.isEmpty();
@@ -55,6 +82,30 @@ public class CollectionTools {
return map != null && !map.isEmpty();
}
public static boolean isNotEmpty(@Nullable Table<?, ?, ?> table) {
return table != null && !table.isEmpty();
}
public static boolean isNotEmpty(@Nullable Multimap<?, ?> map) {
return map != null && !map.isEmpty();
}
public static boolean isNotEmpty(@Nullable Multiset<?> set) {
return set != null && !set.isEmpty();
}
public static boolean isNotEmpty(@Nullable RangeSet<?> set) {
return set != null && !set.isEmpty();
}
// ================================
// #endregion - isNotEmpty
// ================================
// ================================
// #region - nullToEmpty
// ================================
@Nonnull
public static <T> List<T> nullToEmptyList(@Nullable List<T> list) {
return list == null ? Collections.emptyList() : list;
@@ -70,6 +121,10 @@ public class CollectionTools {
return map == null ? Collections.emptyMap() : map;
}
// ================================
// #endregion - nullToEmpty
// ================================
private CollectionTools() {
throw new IllegalStateException("Utility class");
}