public static <T> Set<T> addSorted(Set<T> set, T element) {
        final int size = set.size();
        if (size == 0) {
            return Collections.singleton(element);
        } else if (isUnmodifiableType(set.getClass())) {
            if (size == 1) {
                final T val = set.iterator().next();
                set = new LinkedHashSet<>();
                set.add(val);
            } else {
                set = new LinkedHashSet<>(set);
            }
        }
        set.add(element);
        return 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