|
2 | 2 |
|
3 | 3 | import javax.annotation.Nonnull; |
4 | 4 | import javax.annotation.Nullable; |
5 | | -import java.util.*; |
| 5 | +import java.util.AbstractList; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.SortedSet; |
| 13 | +import java.util.TreeSet; |
6 | 14 |
|
7 | 15 | /** |
8 | 16 | * Utility for handling {@link java.util.List} types. |
@@ -268,6 +276,93 @@ public static <T> int sortedInsertIndex(@Nullable Comparator<T> comparator, @Non |
268 | 276 | return i; |
269 | 277 | } |
270 | 278 |
|
| 279 | + /** |
| 280 | + * @param list |
| 281 | + * List to insert into. |
| 282 | + * @param item |
| 283 | + * Item to insert. |
| 284 | + * @param <T> |
| 285 | + * Item type. |
| 286 | + * |
| 287 | + * @return {@code true} when inserted successfully. |
| 288 | + */ |
| 289 | + public static <T extends Comparable<T>> boolean sortedInsert(@Nonnull List<T> list, @Nonnull T item) { |
| 290 | + return sortedInsert(null, list, item); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * @param comparator |
| 295 | + * Optional comparator for sorting, must be specified if {@code T} is not {@link Comparable}. |
| 296 | + * @param list |
| 297 | + * List to insert into. |
| 298 | + * @param item |
| 299 | + * Item to insert. |
| 300 | + * @param <T> |
| 301 | + * Item type. |
| 302 | + * |
| 303 | + * @return {@code true} when inserted successfully. |
| 304 | + */ |
| 305 | + public static <T> boolean sortedInsert(@Nullable Comparator<T> comparator, @Nonnull List<T> list, @Nonnull T item) { |
| 306 | + int index = sortedInsertIndex(comparator, list, item); |
| 307 | + if (index >= 0 && index <= list.size()) { |
| 308 | + list.add(index, item); |
| 309 | + return true; |
| 310 | + } |
| 311 | + return false; |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * @param collection |
| 316 | + * Some collection. |
| 317 | + * @param <T> |
| 318 | + * Item type. |
| 319 | + * @param <C> |
| 320 | + * Collection type. |
| 321 | + * |
| 322 | + * @return Sorted list of the items in the collection. |
| 323 | + */ |
| 324 | + @Nonnull |
| 325 | + public static <T extends Comparable<T>, C extends Collection<T>> List<T> sorted(@Nonnull C collection) { |
| 326 | + // Empty, nothing to sort |
| 327 | + if (collection.isEmpty()) |
| 328 | + return Collections.emptyList(); |
| 329 | + |
| 330 | + // Already sorted |
| 331 | + if (collection instanceof SortedSet) |
| 332 | + return new ArrayList<>(collection); |
| 333 | + |
| 334 | + // Wrap implicitly sorted collection |
| 335 | + return new ArrayList<>(new TreeSet<>(collection)); |
| 336 | + } |
| 337 | + |
| 338 | + /** |
| 339 | + * @param comparator |
| 340 | + * Comparator for sorting. |
| 341 | + * @param collection |
| 342 | + * Some collection. |
| 343 | + * @param <T> |
| 344 | + * Item type. |
| 345 | + * @param <C> |
| 346 | + * Collection type. |
| 347 | + * |
| 348 | + * @return Sorted list of the items in the collection. |
| 349 | + */ |
| 350 | + @Nonnull |
| 351 | + public static <T, C extends Collection<T>> List<T> sorted(@Nonnull Comparator<T> comparator, @Nonnull C collection) { |
| 352 | + // Empty, nothing to sort |
| 353 | + if (collection.isEmpty()) |
| 354 | + return Collections.emptyList(); |
| 355 | + |
| 356 | + // Already sorted |
| 357 | + if (collection instanceof SortedSet) |
| 358 | + return new ArrayList<>(collection); |
| 359 | + |
| 360 | + // Wrap implicitly sorted collection |
| 361 | + TreeSet<T> sortedSet = new TreeSet<>(comparator); |
| 362 | + sortedSet.addAll(collection); |
| 363 | + return new ArrayList<>(sortedSet); |
| 364 | + } |
| 365 | + |
271 | 366 | /** |
272 | 367 | * @param listA |
273 | 368 | * Some list of comparable items. Assumed to be in sorted order. |
|
0 commit comments