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

com.moon.core.util.SetUtil Maven / Gradle / Ivy

package com.moon.core.util;

import com.moon.core.lang.ThrowUtil;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author moonsky
 */
public final class SetUtil extends CollectUtil {

    private SetUtil() { ThrowUtil.noInstanceError(); }

    public static Set empty() {return Collections.EMPTY_SET;}

    /*
     * ---------------------------------------------------------------------------------
     * of hash set
     * ---------------------------------------------------------------------------------
     */

    public static  HashSet newSet() { return new HashSet<>(); }

    public static  HashSet newSet(int initCapacity) { return new HashSet<>(initCapacity); }

    public static  HashSet newSet(T... values) { return addAll(newSet(values.length), values); }

    public static  HashSet newSet(Iterable iterable) {
        return iterable == null ? newSet() : (iterable instanceof Collection ? new HashSet((Collection) iterable) : addAll(
            newSet(),
            iterable));
    }

    public static  HashSet newSet(Iterator iterator) { return addAll(newSet(), iterator); }

    public static  HashSet newHashSet() { return new HashSet<>(); }

    public static  HashSet newHashSet(int initCapacity) { return new HashSet<>(initCapacity); }

    public static  HashSet newHashSet(T value) { return add(newHashSet(), value); }

    public static  HashSet newHashSet(T value1, T value2) { return add(newHashSet(value1), value2); }

    public static  HashSet newHashSet(T value1, T value2, T value3) {
        return add(newHashSet(value1, value2), value3);
    }

    public static  HashSet newHashSet(T... values) { return addAll(newHashSet(values.length), values); }

    public static  HashSet newHashSet(Collection collect) {
        return collect == null ? newHashSet() : new HashSet<>(collect);
    }

    public static  HashSet newHashSet(Iterable iterable) {
        return iterable == null ? newHashSet() : (iterable instanceof Collection ? new HashSet((Collection) iterable) : addAll(
            newHashSet(),
            iterable));
    }

    public static  HashSet newHashSet(Iterator iterator) { return addAll(newHashSet(), iterator); }

    /*
     * ---------------------------------------------------------------------------------
     * of linked hash set
     * ---------------------------------------------------------------------------------
     */

    public static  LinkedHashSet newLinkedHashSet() { return new LinkedHashSet<>(); }

    public static  LinkedHashSet newLinkedHashSet(int initCapacity) { return new LinkedHashSet<>(initCapacity); }

    public static  LinkedHashSet newLinkedHashSet(T value) { return add(newLinkedHashSet(), value); }

    public static  LinkedHashSet newLinkedHashSet(T value1, T value2) {
        return add(newLinkedHashSet(value1), value2);
    }

    public static  LinkedHashSet newLinkedHashSet(T value1, T value2, T value3) {
        return add(newLinkedHashSet(value1, value2), value3);
    }

    public static  LinkedHashSet newLinkedHashSet(T... values) {
        return addAll(newLinkedHashSet(values.length), values);
    }

    public static  LinkedHashSet newLinkedHashSet(Collection collect) {
        return collect == null ? newLinkedHashSet() : new LinkedHashSet<>(collect);
    }

    public static  LinkedHashSet newLinkedHashSet(Iterable iterable) {
        return iterable == null ? newLinkedHashSet() : (iterable instanceof Collection ? new LinkedHashSet((Collection) iterable) : addAll(
            newLinkedHashSet(),
            iterable));
    }

    public static  LinkedHashSet newLinkedHashSet(Iterator iterator) {
        return addAll(newLinkedHashSet(), iterator);
    }

    /*
     * ---------------------------------------------------------------------------------
     * of tree set
     * ---------------------------------------------------------------------------------
     */

    public static  TreeSet newTreeSet() { return new TreeSet<>(); }

    public static  TreeSet newTreeSet(T value) { return add(newTreeSet(), value); }

    public static  TreeSet newTreeSet(T value1, T value2) { return add(newTreeSet(value1), value2); }

    public static  TreeSet newTreeSet(T value1, T value2, T value3) {
        return add(newTreeSet(value1, value2), value3);
    }

    public static  TreeSet newTreeSet(T... values) { return addAll(newTreeSet(), values); }

    public static  TreeSet newTreeSet(Collection collect) {
        return collect == null ? newTreeSet() : new TreeSet<>(collect);
    }

    public static  TreeSet newTreeSet(Iterable iterable) {
        return iterable == null ? newTreeSet() : (iterable instanceof Collection ? new TreeSet((Collection) iterable) : addAll(
            newTreeSet(),
            iterable));
    }

    public static  TreeSet newTreeSet(SortedSet sortedSet) { return new TreeSet<>(sortedSet); }

    public static  TreeSet newTreeSet(Comparator comparator) { return new TreeSet<>(comparator); }

    public static  TreeSet newTreeSet(Comparator comparator, Iterable iterable) {
        return addAll(newTreeSet(comparator), iterable);
    }

    public static  TreeSet newTreeSet(Comparator comparator, T... values) {
        return addAll(newTreeSet(comparator), values);
    }

    public static  TreeSet newTreeSet(Iterator iterator) { return addAll(newTreeSet(), iterator); }

    public static  Set mapAsSet(Collection src, Function mapper) {
        Collection collect = map(src, mapper);
        return collect instanceof Set ? (Set) collect : newSet(collect);
    }

    /*
     * ---------------------------------------------------------------------------------
     * keepers
     * ---------------------------------------------------------------------------------
     */

    /**
     * 如果集合是空集合(null 或 size() == 0)这返回 null
     *
     * @param set set
     * @param  set 元素类型
     *
     * @return null set if is an empty set or null
     */
    public static  Set nullIfEmpty(Set set) { return isEmpty(set) ? null : set; }

    /**
     * 如果 valuesSet 是 null 则创建一个新的 ArrayList 返回
     *
     * @param set set
     * @param  set 元素类型
     *
     * @return empty set if null
     */
    public static  Set emptyIfNull(Set set) { return isEmpty(set) ? empty() : set; }

    /**
     * 确保返回集合不为 null
     *
     * @param set set
     * @param  set 元素类型
     *
     * @return empty Set if null
     */
    public static  Set newIfNull(Set set) { return newIfNull(set, SetUtil::newSet); }

    /**
     * 通常用于确保返回集合不为 null
     *
     * @param set     set
     * @param creator set 构建器
     * @param      set 元素类型
     *
     * @return empty Set if null
     */
    public static  Set newIfNull(
        Set set, Supplier> creator
    ) { return set == null ? creator.get() : set; }

    /**
     * 确保返回集合是可操作的{@code set},用于规避{@link Collections#emptySet()}
     *
     * @param set
     * @param 
     *
     * @return
     */
    public static  Set newIfEmpty(Set set) { return newIfEmpty(set, SetUtil::newSet); }

    /**
     * 通常用于确保返回集合可操作的{@code set}
     *
     * @param set     set
     * @param creator set 构建器
     * @param      set 元素类型
     *
     * @return empty Set if null
     */
    public static  Set newIfEmpty(Set set, Supplier> creator) {
        return isEmpty(set) ? creator.get() : set;
    }

    /**
     * 连接多个集合,返回新集合
     *
     * @param set  基础集合
     * @param sets 待连接集合
     * @param   集合数据类型
     *
     * @return 连接后的集合
     */
    public static  Set concat(Set set, Set... sets) { return (Set) concat0(set, sets); }

    public static  T requireGet(Set set, int index) { return get(set, index); }

    public static  T nullableGet(Set set, int index) {
        int size = set.size(), idx = 0;
        if (size <= index || index < 0) {
            return null;
        }
        for (T item : set) {
            if (idx++ == index) {
                return item;
            }
        }
        return null;
    }

    public static  T get(Set set, int index) {
        int size = set.size(), idx = 0;
        if (size <= index || index < 0) {
            throw new IndexOutOfBoundsException(String.valueOf(index));
        }
        for (T item : set) {
            if (idx++ == index) {
                return item;
            }
        }
        throw new IndexOutOfBoundsException(String.valueOf(index));
    }

    public static  T getByObject(Object set, int index) { return get((Set) set, index); }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy