|
7 | 7 | import org.jetbrains.annotations.NotNull; |
8 | 8 | import org.jetbrains.annotations.Nullable; |
9 | 9 |
|
10 | | -import java.util.Arrays; |
11 | | -import java.util.Comparator; |
12 | | -import java.util.Objects; |
| 10 | +import java.util.*; |
13 | 11 | import java.util.function.*; |
14 | 12 |
|
15 | 13 | /** |
@@ -244,6 +242,48 @@ public static <T, F> void fill(@NotNull T[] array, @Nullable F argument, @NotNul |
244 | 242 | return result; |
245 | 243 | } |
246 | 244 |
|
| 245 | + /** |
| 246 | + * Combine two arrays to one single array with uniq elements. |
| 247 | + * |
| 248 | + * @param <T> the base array's component type. |
| 249 | + * @param <E> the added array's component type. |
| 250 | + * @param base the base array. |
| 251 | + * @param added the additional array. |
| 252 | + * @return the combined array with uniq elements. |
| 253 | + */ |
| 254 | + public static <T, E extends T> @NotNull T[] combineUniq(@NotNull T[] base, @Nullable E[] added) { |
| 255 | + return combineUniq(base, added, getComponentType(base)); |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * Combine two arrays to one single array with uniq elements. |
| 260 | + * |
| 261 | + * @param <T> the base array's component type. |
| 262 | + * @param <E> the added array's component type. |
| 263 | + * @param base the base array. |
| 264 | + * @param added the additional array. |
| 265 | + * @param type the base array's component type. |
| 266 | + * @return the combined array with uniq elements. |
| 267 | + */ |
| 268 | + public static <T, E extends T> @NotNull T[] combineUniq( |
| 269 | + @Nullable T[] base, |
| 270 | + @Nullable E[] added, |
| 271 | + @NotNull Class<T> type |
| 272 | + ) { |
| 273 | + |
| 274 | + if (base == null) { |
| 275 | + return added == null ? create(type, 0) : added; |
| 276 | + } else if (added == null || added.length < 1) { |
| 277 | + return base; |
| 278 | + } |
| 279 | + |
| 280 | + var result = new HashSet<T>(base.length + added.length); |
| 281 | + result.addAll(Arrays.asList(base)); |
| 282 | + result.addAll(Arrays.asList(added)); |
| 283 | + |
| 284 | + return result.toArray(create(type, result.size())); |
| 285 | + } |
| 286 | + |
247 | 287 | /** |
248 | 288 | * Check the array on contains the value. |
249 | 289 | * |
|
0 commit comments