@SuppressWarnings("unchecked")
    public static <T> Set<T> unmodifiableSet(Set<T> set) {
        if (isUnmodifiableType(set.getClass())) {
            return set;
        }
        switch (set.size()) {
            case 0:
                return Collections.emptySet();
            case 1:
                return Collections.singleton(set.iterator().next());
            default:
                return Collections.unmodifiableSet((Set<T>) (
                        set instanceof LinkedHashSet ? ((LinkedHashSet<T>) set).clone() :
                        set instanceof TreeSet ? ((TreeSet<T>) set).clone() :
                        set instanceof HashSet ? ((HashSet<T>) set).clone() :
                        new LinkedHashSet<>(set)));
        }
    }
Return an unmodifiable copy of a set, or the same set if its already an unmodifiable type.

@param set the set
@param <T> element type
@return unmodifiable copy of a set, or the same set if its already an unmodifiable type
querydsl-core/src/main/java/com/querydsl/core/util/CollectionUtils.java