Skip to content

Commit 9749cc2

Browse files
committed
Add additional sort utils for lists
1 parent 3d00d15 commit 9749cc2

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/main/java/software/coley/collections/Lists.java

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
import javax.annotation.Nonnull;
44
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;
614

715
/**
816
* Utility for handling {@link java.util.List} types.
@@ -268,6 +276,93 @@ public static <T> int sortedInsertIndex(@Nullable Comparator<T> comparator, @Non
268276
return i;
269277
}
270278

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+
271366
/**
272367
* @param listA
273368
* Some list of comparable items. Assumed to be in sorted order.

src/test/java/software/coley/collections/ListsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import java.util.ArrayList;
56
import java.util.Comparator;
67
import java.util.List;
78

@@ -97,6 +98,16 @@ void sortedInsertIndex() {
9798
assertEquals(5, Lists.sortedInsertIndex(strings, "g"));
9899
}
99100

101+
@Test
102+
void sortedInsert() {
103+
List<String> strings = new ArrayList<>(asList("a", "b", "c", /* d */ "e", "f"));
104+
assertTrue(Lists.sortedInsert(strings, " "));
105+
assertEquals(0, strings.indexOf(" "));
106+
assertTrue(Lists.sortedInsert(strings, "a"));
107+
assertEquals(1, strings.indexOf("a"));
108+
assertEquals(2, strings.lastIndexOf("a"));
109+
}
110+
100111
@Test
101112
void compare() {
102113
assertEquals(0, Lists.compare(asList("a", "b"), asList("a", "b")));

0 commit comments

Comments
 (0)