|
| 1 | +package com.blade.kit; |
| 2 | + |
| 3 | +import lombok.NoArgsConstructor; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | +import java.util.concurrent.ConcurrentMap; |
| 8 | + |
| 9 | +/** |
| 10 | + * Collection kit |
| 11 | + * |
| 12 | + * @author biezhi |
| 13 | + * @date 2017/12/15 |
| 14 | + */ |
| 15 | +@NoArgsConstructor |
| 16 | +public final class CollectionKit { |
| 17 | + |
| 18 | + /** |
| 19 | + * Determines whether an array is empty |
| 20 | + * |
| 21 | + * @param array array object |
| 22 | + * @param <T> array type |
| 23 | + * @return return array is empty |
| 24 | + */ |
| 25 | + public static <T> boolean isEmpty(T[] array) { |
| 26 | + return null == array || array.length == 0; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Determines whether an array is not empty |
| 31 | + * |
| 32 | + * @param array array object |
| 33 | + * @param <T> array type |
| 34 | + * @return return array is not empty |
| 35 | + */ |
| 36 | + public static <T> boolean isNotEmpty(T[] array) { |
| 37 | + return null != array && array.length > 0; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Determines whether an collection is empty |
| 42 | + * |
| 43 | + * @param collection collection object |
| 44 | + * @param <T> collection type |
| 45 | + * @return return collection is empty |
| 46 | + */ |
| 47 | + public static <T> boolean isEmpty(Collection<T> collection) { |
| 48 | + return null == collection || collection.size() == 0; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Determines whether an collection is not empty |
| 53 | + * |
| 54 | + * @param collection collection object |
| 55 | + * @param <T> collection type |
| 56 | + * @return return collection is not empty |
| 57 | + */ |
| 58 | + public static <T> boolean isNotEmpty(Collection<T> collection) { |
| 59 | + return null != collection && collection.size() > 0; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * New HashMap |
| 64 | + * |
| 65 | + * @param <K> HashMap Key type |
| 66 | + * @param <V> HashMap Value type |
| 67 | + * @return return HashMap |
| 68 | + */ |
| 69 | + public static <K, V> HashMap<K, V> newMap() { |
| 70 | + return new HashMap<>(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * New HashMap and initialCapacity |
| 75 | + * |
| 76 | + * @param initialCapacity initialCapacity |
| 77 | + * @param <K> HashMap Key type |
| 78 | + * @param <V> HashMap Value type |
| 79 | + * @return return HashMap |
| 80 | + */ |
| 81 | + public static <K, V> HashMap<K, V> newMap(int initialCapacity) { |
| 82 | + return new HashMap<>(initialCapacity); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * New ConcurrentMap |
| 87 | + * |
| 88 | + * @param <K> ConcurrentMap Key type |
| 89 | + * @param <V> ConcurrentMap Value type |
| 90 | + * @return return ConcurrentMap |
| 91 | + */ |
| 92 | + public static <K, V> ConcurrentMap<K, V> newConcurrentMap() { |
| 93 | + return new ConcurrentHashMap<>(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * New ConcurrentMap and initialCapacity |
| 98 | + * |
| 99 | + * @param initialCapacity initialCapacity |
| 100 | + * @param <K> ConcurrentMap Key type |
| 101 | + * @param <V> ConcurrentMap Value type |
| 102 | + * @return return ConcurrentMap |
| 103 | + */ |
| 104 | + public static <K, V> ConcurrentMap<K, V> newConcurrentMap(int initialCapacity) { |
| 105 | + return new ConcurrentHashMap<>(initialCapacity); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * New List and add values |
| 110 | + * |
| 111 | + * @param values list values |
| 112 | + * @param <T> list type |
| 113 | + * @return return array list |
| 114 | + */ |
| 115 | + public static <T> List<T> newLists(T... values) { |
| 116 | + return Arrays.asList(values); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * New Set and add values |
| 121 | + * |
| 122 | + * @param values set values |
| 123 | + * @param <T> set type |
| 124 | + * @return return HashSet |
| 125 | + */ |
| 126 | + public static <T> Set<T> newSets(T... values) { |
| 127 | + return new HashSet<>(Arrays.asList(values)); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments