org.javers.common.collections.Lists Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.common.collections;
import org.javers.core.metamodel.property.MissingProperty;
import java.util.*;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import static java.util.Collections.EMPTY_LIST;
import static java.util.Collections.unmodifiableList;
import static org.javers.common.validation.Validate.argumentsAreNotNull;
public class Lists {
public static List wrapNull(Object list) {
if (list == null || list == MissingProperty.INSTANCE) {
return Collections.emptyList();
}
return (List)list;
}
public static List add(List list, T element) {
List l = new ArrayList<>(list);
l.add(element);
return unmodifiableList(l);
}
public static List immutableListOf(T... elements) {
if (elements == null) {
return Collections.emptyList();
}
return unmodifiableList(java.util.Arrays.asList(elements));
}
public static List immutableListOf(Collection elements) {
if (elements == null || elements.size() == 0) {
return Collections.emptyList();
}
return unmodifiableList(new ArrayList<>(elements));
}
public static List immutableCopyOf(List elements) {
if (elements == null || elements.size() == 0) {
return Collections.emptyList();
}
return unmodifiableList(new ArrayList<>(elements));
}
public static List asList(E... elements) {
return (List) Arrays.asList(elements);
}
public static List immutableListOf(List elements, E element) {
List list = new ArrayList<>(elements);
list.add(element);
return unmodifiableList(list);
}
/**
* @return index -> value
*/
public static Map asMap(List input) {
if (input == null) {
return null;
}
Map result = new HashMap<>();
int i = 0;
for (T element : input) {
result.put(i, element);
i++;
}
return result;
}
/**
* returns new list with elements from input that satisfies given filter condition
*/
public static List positiveFilter(List input, Predicate filter) {
argumentsAreNotNull(input, filter);
return input.stream().filter(filter).collect(Collectors.toList());
}
/**
* returns new list with elements from input that doesn't satisfies given filter condition
*/
public static List negativeFilter(List input, final Predicate filter) {
argumentsAreNotNull(input, filter);
return input.stream().filter(element -> !filter.test(element)).collect(Collectors.toList());
}
public static List transform(Collection input, Function transformation) {
argumentsAreNotNull(input, transformation);
return input.stream().map(transformation::apply).collect(Collectors.toList());
}
public static List difference(List first, List second) {
if (first == null) {
return EMPTY_LIST;
}
if (second == null) {
return first;
}
List difference = new ArrayList<>(first);
difference.removeAll(second);
return difference;
}
public static Collector, List> toImmutableList() {
return Collector.of(ArrayList::new, List::add, (left, right) -> {
left.addAll(right);
return left;
}, Collections::unmodifiableList);
}
}