Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.ewv.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class SetToSortedList {
// These functions are actually not specific to EwV, but I have to put them somewhere.

// Provide a null-safe comparator if you don't want trouble!
// This works neatly with overloaded Comparators, but you don't have to
public static <T> List<T> toSortedList(Set<T> set, Comparator<T> comparator) {
if (set == null) return Collections.<T>emptyList();
return set.stream()
.sorted(comparator) // What will this match?
.collect(Collectors.toList());
}

// Some convenience wrappers, if needed.
// But, as you see, it's not hard to wrap up your own comparator.
// This only converts the top level to nullsFirst, the lower levels might be unsafe or NullsLast
public static <T> List<T> toNullsFirstList(Set<T> set, Comparator<T> comparator) {
return toSortedList(set, Comparator.nullsFirst(comparator));
}

// This only converts the top level to nullsLast, the lower levels might be unsafe or nullsFirst
public static <T> List<T> toNullsLastList(Set<T> set, Comparator<T> comparator) {
return toSortedList(set, Comparator.nullsLast(comparator));
}

}