/** * Factory method to create an unmodifiable set. * * @param <E> the element type * @param set the set to decorate, must not be null * @return a new unmodifiable set * @throws NullPointerException if set is null * @since 4.0 */ public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) { if (set instanceof Unmodifiable) { @SuppressWarnings("unchecked") // safe to upcast final Set<E> tmpSet = (Set<E>) set; return tmpSet; } return new UnmodifiableSet<>(set); }