org.javers.common.collections.Sets 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.common.validation.Validate;
import org.javers.core.metamodel.property.MissingProperty;
import java.util.*;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Collector;
import static java.util.Arrays.asList;
import static java.util.Collections.EMPTY_SET;
/**
* @author Maciej Zasada
*/
public class Sets {
private Sets() {
}
public static Set wrapNull(Object set) {
if (set == null || set == MissingProperty.INSTANCE) {
return Collections.emptySet();
}
return (Set) set;
}
/**
* null args are allowed
*/
public static Set intersection(Set first, Set second) {
if (first == null || second == null) {
return EMPTY_SET;
}
Set intersection = new HashSet<>();
for (E e : first) {
if (second.contains(e)) {
intersection.add(e);
}
}
return intersection;
}
public static Set xor(Set first, Set second) {
Set xor = difference(first, second);
xor.addAll(difference(second, first));
return xor;
}
/**
* null args are allowed
*
* @return mutable set
*/
public static Set difference(Set first, Set second) {
if (first == null || first.size() == 0) {
return new HashSet<>();
}
if (second == null || second.size() == 0) {
return first;
}
Set difference = new HashSet<>(first);
difference.removeAll(second);
return difference;
}
/**
* null args are allowed
*/
public static Collection difference(Set first, Set second, Function hasher) {
if (first == null || first.size() == 0) {
return Collections.emptyList();
}
if (second == null || second.size() == 0) {
return first;
}
Map map = new HashMap();
first.stream().forEach(e -> map.put(hasher.apply(e), e));
second.stream().forEach(e -> map.remove(hasher.apply(e)));
return Collections.unmodifiableCollection(map.values());
}
public static Set asSet(E... elements) {
return asSet(asList(elements));
}
public static Set asSet(Collection elements) {
if (elements == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(new HashSet<>(elements));
}
/**
* @return ImmutableSet
*/
public static Set transform(Set input, Function transformation) {
Validate.argumentsAreNotNull(input, transformation);
return input.stream().map(transformation::apply).collect(toImmutableSet());
}
private static Set nullSafe(Set set) {
if (set == null) {
return EMPTY_SET;
}
return set;
}
/**
* @return index -> value
*/
public static Map asMap(Set input) {
if (input == null){
return null;
}
Map result = new HashMap<>();
int i = 0;
for (T element : input) {
result.put(i, element);
i++;
}
return result;
}
public static Collector, Set> toImmutableSet() {
return Collector.of(HashSet::new, Set::add, (left, right) -> {
left.addAll(right);
return left;
}, Collections::unmodifiableSet);
}
}