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

spring.turbo.util.collection.SetFactories Maven / Gradle / Ivy

package spring.turbo.util.collection;

import org.springframework.lang.Nullable;
import spring.turbo.util.Asserts;

import java.util.*;

/**
 * {@link Set} 创建工具
 *
 * @author 应卓
 * @see ListFactories
 * @see StreamFactories
 * @since 1.0.9
 */
public final class SetFactories {

    /**
     * 私有构造方法
     */
    private SetFactories() {
        super();
    }

    @SafeVarargs
    public static  Set newUnmodifiableSet(T... elements) {
        Asserts.notNull(elements);
        Asserts.noNullElements(elements);
        return Collections.unmodifiableSet(newHashSet(elements));
    }

    @SafeVarargs
    public static  HashSet newHashSet(T... elements) {
        Asserts.notNull(elements);
        Asserts.noNullElements(elements);
        final var set = new HashSet();
        Collections.addAll(set, elements);
        return set;
    }

    @SafeVarargs
    public static  LinkedHashSet newLinkedHashSet(T... elements) {
        Asserts.notNull(elements);
        Asserts.noNullElements(elements);
        final var set = new LinkedHashSet();
        Collections.addAll(set, elements);
        return set;
    }

    @SafeVarargs
    public static > TreeSet newTreeSet(@Nullable T... elements) {
        final var set = new TreeSet(Comparator.naturalOrder());
        if (elements != null) {
            Collections.addAll(set, elements);
        }
        return set;
    }

    @SafeVarargs
    public static  TreeSet newTreeSet(Comparator comparator, T... elements) {
        Asserts.notNull(comparator);
        Asserts.notNull(elements);
        Asserts.noNullElements(elements);
        final var set = new TreeSet(comparator);
        Collections.addAll(set, elements);
        return set;
    }

    @SafeVarargs
    public static  HashSet nullSafeNewHashSet(@Nullable T... elements) {
        final var set = new HashSet();
        CollectionUtils.nullSafeAddAll(set, elements);
        return set;
    }

    @SafeVarargs
    public static  LinkedHashSet nullSafeNewLinkedHashSet(@Nullable T... elements) {
        final var set = new LinkedHashSet();
        CollectionUtils.nullSafeAddAll(set, elements);
        return set;
    }

    @SafeVarargs
    public static > TreeSet nullSafeNewTreeSet(@Nullable T... elements) {
        final var set = new TreeSet(Comparator.naturalOrder());
        CollectionUtils.nullSafeAddAll(set, elements);
        return set;
    }

    @SafeVarargs
    public static  TreeSet nullSafeNewTreeSet(Comparator comparator, @Nullable T... elements) {
        Asserts.notNull(comparator);
        final var set = new TreeSet(comparator);
        CollectionUtils.nullSafeAddAll(set, elements);
        return set;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy