All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.common.util.set.Sets Maven / Gradle / Ivy

/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.common.util.set;

import java.util.Arrays;
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());
    }

    @SafeVarargs
    @SuppressWarnings("varargs")
    public static  HashSet newHashSet(T... elements) {
        Objects.requireNonNull(elements);
        return new HashSet<>(Arrays.asList(elements));
    }

    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);
    }

    public static  boolean haveNonEmptyIntersection(Set left, Set right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        return left.stream().anyMatch(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) == false).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(final Set left, final Set right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        return left.stream().filter(k -> right.contains(k) == false).collect(toSortedSet());
    }

    /**
     * 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. The resulting set is unmodifiable.
     *
     * @param left  the left set
     * @param right the right set
     * @param    the type of the elements of the sets
     * @return the unmodifiable sorted relative complement of the left set with respect to the right set
     */
    public static  SortedSet unmodifiableSortedDifference(final Set left, final Set right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        return left.stream().filter(k -> right.contains(k) == false).collect(toUnmodifiableSortedSet());
    }

    /**
     * Returns a {@link Collector} that accumulates the input elements into a sorted set.
     *
     * @param  the type of the input elements
     * @return a sorted set
     */
    public static  Collector, SortedSet> toSortedSet() {
        return new SortedSetCollector<>();
    }

    /**
     * Returns a {@link Collector} that accumulates the input elements into a sorted set and finishes the resulting set into an
     * unmodifiable set. The resulting read-only view through the unmodifiable set is a sorted set.
     *
     * @param  the type of the input elements
     * @return an unmodifiable set where the underlying set is sorted
     */
    public static  Collector, SortedSet> toUnmodifiableSortedSet() {
        return new UnmodifiableSortedSetCollector<>();
    }

    abstract static class AbstractSortedSetCollector implements Collector, SortedSet> {

        @Override
        public Supplier> supplier() {
            return TreeSet::new;
        }

        @Override
        public BiConsumer, T> accumulator() {
            return SortedSet::add;
        }

        @Override
        public BinaryOperator> combiner() {
            return (s, t) -> {
                s.addAll(t);
                return s;
            };
        }

        public abstract Function, SortedSet> finisher();

        public abstract Set characteristics();

    }

    private static class SortedSetCollector extends AbstractSortedSetCollector {

        @Override
        public Function, SortedSet> finisher() {
            return Function.identity();
        }

        static final Set CHARACTERISTICS = Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));

        @Override
        public Set characteristics() {
            return CHARACTERISTICS;
        }

    }

    private static class UnmodifiableSortedSetCollector extends AbstractSortedSetCollector {

        @Override
        public Function, SortedSet> finisher() {
            return Collections::unmodifiableSortedSet;
        }

        @Override
        public Set characteristics() {
            return Collections.emptySet();
        }

    }

    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());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy