001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.utils;
021
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028/**
029 * <p>Note: it simply wraps the existing JDK methods to provide a workaround
030 * for Pitest survival on mutation for removal of immutable wrapping,
031 * see #13127 for more details.
032 * </p>
033 *
034 */
035public final class UnmodifiableCollectionUtil {
036
037    /**
038     * Private constructor for UnmodifiableCollectionUtil.
039     *
040     */
041    private UnmodifiableCollectionUtil() {
042    }
043
044    /**
045     * Creates an unmodifiable set based on the provided collection.
046     *
047     * @param collection the collection to create an unmodifiable set from
048     * @param <T> the type of elements in the set
049     * @return an unmodifiable set containing the elements from the provided collection
050     */
051    public static <T> Set<T> unmodifiableSet(Set<T> collection) {
052        return Collections.unmodifiableSet(collection);
053    }
054
055    /**
056     * Creates an unmodifiable list based on the provided collection.
057     *
058     * @param collection the collection to create an unmodifiable list from
059     * @param <T> the type of elements in the set
060     * @return an unmodifiable list containing the elements from the provided collection
061     */
062    public static <T> List<T> unmodifiableList(List<T> collection) {
063        return Collections.unmodifiableList(collection);
064    }
065
066    /**
067     * Creates a copy of array.
068     *
069     * @param array Array to create a copy of
070     * @param length length of array
071     * @param <T> The type of array
072     * @return copy of array
073     */
074    public static <T> T[] copyOfArray(T[] array, int length) {
075        return Arrays.copyOf(array, length);
076    }
077
078    /**
079     * Creates a copy of Map.
080     *
081     * @param map map to create a copy of
082     * @param <K> the type of keys in the map
083     * @param <V> the type of values in the map
084     * @return an immutable copy of the input map
085     */
086    public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) {
087        return Map.copyOf(map);
088    }
089
090    /**
091     * Returns an immutable set containing only the specified object.
092     *
093     * @param obj the type of object in the set
094     * @param <T> the type of object
095     * @return immutable set
096     */
097    public static <T> Set<T> singleton(T obj) {
098        return Collections.singleton(obj);
099    }
100}