CollectionTools#isNotEmpty 支持 guava 的 Table、Multimap、Multiset 和 RangeSet
This commit is contained in:
parent
d217e8b9ac
commit
e3ff5a2ab3
@ -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");
|
||||
}
|
||||
|
@ -34,14 +34,24 @@ import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.HashMultiset;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multiset;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.RangeSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.collect.TreeRangeSet;
|
||||
|
||||
public class CollectionToolsTests {
|
||||
@Test
|
||||
void testIsEmpty() {
|
||||
|
||||
// Collection
|
||||
List<String> list = new ArrayList<>();
|
||||
assertTrue(CollectionTools.isEmpty(list));
|
||||
assertFalse(CollectionTools.isNotEmpty(list));
|
||||
@ -50,6 +60,7 @@ public class CollectionToolsTests {
|
||||
assertFalse(CollectionTools.isEmpty(list));
|
||||
assertTrue(CollectionTools.isNotEmpty(list));
|
||||
|
||||
// Map
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
assertTrue(CollectionTools.isEmpty(map));
|
||||
assertFalse(CollectionTools.isNotEmpty(map));
|
||||
@ -57,6 +68,43 @@ public class CollectionToolsTests {
|
||||
map.put("2", 2);
|
||||
assertFalse(CollectionTools.isEmpty(map));
|
||||
assertTrue(CollectionTools.isNotEmpty(map));
|
||||
|
||||
// Table
|
||||
Table<String, String, Integer> table = HashBasedTable.create();
|
||||
assertTrue(CollectionTools.isEmpty(table));
|
||||
assertFalse(CollectionTools.isNotEmpty(table));
|
||||
|
||||
table.put("ABC", "d", 4);
|
||||
assertFalse(CollectionTools.isEmpty(table));
|
||||
assertTrue(CollectionTools.isNotEmpty(table));
|
||||
|
||||
// Multimap
|
||||
Multimap<String, String> multimap = HashMultimap.create();
|
||||
assertTrue(CollectionTools.isEmpty(multimap));
|
||||
assertFalse(CollectionTools.isNotEmpty(multimap));
|
||||
|
||||
multimap.put("ABC", "d");
|
||||
assertFalse(CollectionTools.isEmpty(multimap));
|
||||
assertTrue(CollectionTools.isNotEmpty(multimap));
|
||||
|
||||
// Multiset
|
||||
Multiset<String> multiset = HashMultiset.create();
|
||||
assertTrue(CollectionTools.isEmpty(multiset));
|
||||
assertFalse(CollectionTools.isNotEmpty(multiset));
|
||||
|
||||
multiset.add("ABC");
|
||||
assertFalse(CollectionTools.isEmpty(multiset));
|
||||
assertTrue(CollectionTools.isNotEmpty(multiset));
|
||||
|
||||
// RangeSet
|
||||
RangeSet<Integer> rangeSet = TreeRangeSet.create();
|
||||
assertTrue(CollectionTools.isEmpty(rangeSet));
|
||||
assertFalse(CollectionTools.isNotEmpty(rangeSet));
|
||||
|
||||
rangeSet.add(Range.closed(0, 100));
|
||||
rangeSet.add(Range.openClosed(100, 200));
|
||||
assertFalse(CollectionTools.isEmpty(rangeSet));
|
||||
assertTrue(CollectionTools.isNotEmpty(rangeSet));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user