001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.Collection;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028import java.util.stream.Collectors;
029
030/**
031 * <div>Note: it simply wraps the existing JDK methods to provide a workaround
032 * for Pitest survival on mutation for removal of immutable wrapping,
033 * see #13127 for more details.
034 * </div>
035 *
036 */
037public final class UnmodifiableCollectionUtil {
038
039    /**
040     * Private constructor for UnmodifiableCollectionUtil.
041     *
042     */
043    private UnmodifiableCollectionUtil() {
044    }
045
046    /**
047     * Creates an unmodifiable list based on the provided collection.
048     *
049     * @param collection the collection to create an unmodifiable list from
050     * @param <T> the type of elements in the set
051     * @return an unmodifiable list containing the elements from the provided collection
052     */
053    public static <T> List<T> unmodifiableList(List<T> collection) {
054        return Collections.unmodifiableList(collection);
055    }
056
057    /**
058     * Returns an unmodifiable view of a List containing elements of a specific type.
059     *
060     * @param items The List of items to make unmodifiable.
061     * @param elementType The Class object representing the type of elements in the list.
062     * @param <S> The generic type of elements in the input Collection.
063     * @param <T> The type of elements in the resulting unmodifiable List.
064     * @return An unmodifiable List containing elements of the specified type.
065     */
066    public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) {
067        return items.stream()
068                .map(elementType::cast)
069                .collect(Collectors.toUnmodifiableList());
070    }
071
072    /**
073     * Creates a copy of array.
074     *
075     * @param array Array to create a copy of
076     * @param length length of array
077     * @param <T> The type of array
078     * @return copy of array
079     */
080    public static <T> T[] copyOfArray(T[] array, int length) {
081        return Arrays.copyOf(array, length);
082    }
083
084    /**
085     * Creates a copy of Map.
086     *
087     * @param map map to create a copy of
088     * @param <K> the type of keys in the map
089     * @param <V> the type of values in the map
090     * @return an immutable copy of the input map
091     */
092    public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) {
093        return Map.copyOf(map);
094    }
095
096    /**
097     * Returns an immutable set containing only the specified object.
098     *
099     * @param obj the type of object in the set
100     * @param <T> the type of object
101     * @return immutable set
102     */
103    public static <T> Set<T> singleton(T obj) {
104        return Collections.singleton(obj);
105    }
106}