org.elasticsearch.common.util.set.Sets Maven / Gradle / Ivy
The newest version!
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.util.set;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public final class Sets {
private Sets() {
}
public static HashSet newHashSet(Iterator iterator) {
Objects.requireNonNull(iterator);
HashSet set = new HashSet<>();
while (iterator.hasNext()) {
set.add(iterator.next());
}
return set;
}
public static HashSet newHashSet(Iterable iterable) {
Objects.requireNonNull(iterable);
return iterable instanceof Collection ? new HashSet<>((Collection)iterable) : newHashSet(iterable.iterator());
}
public static HashSet newHashSet(T... elements) {
Objects.requireNonNull(elements);
HashSet set = new HashSet<>(elements.length);
Collections.addAll(set, elements);
return set;
}
public static Set newConcurrentHashSet() {
return Collections.newSetFromMap(new ConcurrentHashMap<>());
}
public static boolean haveEmptyIntersection(Set left, Set right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return left.stream().noneMatch(right::contains);
}
/**
* The relative complement, or difference, of the specified left and right set. Namely, the resulting set contains all the elements that
* are in the left set but not in the right set. Neither input is mutated by this operation, an entirely new set is returned.
*
* @param left the left set
* @param right the right set
* @param the type of the elements of the sets
* @return the relative complement of the left set with respect to the right set
*/
public static Set difference(Set left, Set right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return left.stream().filter(k -> !right.contains(k)).collect(Collectors.toSet());
}
/**
* The relative complement, or difference, of the specified left and right set, returned as a sorted set. Namely, the resulting set
* contains all the elements that are in the left set but not in the right set, and the set is sorted using the natural ordering of
* element type. Neither input is mutated by this operation, an entirely new set is returned.
*
* @param left the left set
* @param right the right set
* @param the type of the elements of the sets
* @return the sorted relative complement of the left set with respect to the right set
*/
public static SortedSet sortedDifference(Set left, Set right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return left.stream().filter(k -> !right.contains(k)).collect(new SortedSetCollector<>());
}
private static class SortedSetCollector implements Collector, SortedSet> {
@Override
public Supplier> supplier() {
return TreeSet::new;
}
@Override
public BiConsumer, T> accumulator() {
return (s, e) -> s.add(e);
}
@Override
public BinaryOperator> combiner() {
return (s, t) -> {
s.addAll(t);
return s;
};
}
@Override
public Function, SortedSet> finisher() {
return Function.identity();
}
static final Set CHARACTERISTICS =
Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
@Override
public Set characteristics() {
return CHARACTERISTICS;
}
}
public static Set union(Set left, Set right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
Set union = new HashSet<>(left);
union.addAll(right);
return union;
}
public static Set intersection(Set set1, Set set2) {
Objects.requireNonNull(set1);
Objects.requireNonNull(set2);
final Set left;
final Set right;
if (set1.size() < set2.size()) {
left = set1;
right = set2;
} else {
left = set2;
right = set1;
}
return left.stream().filter(right::contains).collect(Collectors.toSet());
}
}