001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.Set;
027
028/**
029 * <div>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 * </div>
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 list based on the provided collection.
046     *
047     * @param collection the collection to create an unmodifiable list from
048     * @param <T> the type of elements in the set
049     * @return an unmodifiable list containing the elements from the provided collection
050     */
051    public static <T> List<T> unmodifiableList(List<T> collection) {
052        return Collections.unmodifiableList(collection);
053    }
054
055    /**
056     * Creates an unmodifiable list from the provided collection.
057     * If the collection is null, returns an empty unmodifiable list.
058     *
059     * @param collection the collection to create an unmodifiable list from
060     * @param <T> the type of elements in the list
061     * @return an unmodifiable list containing the elements from the provided collection,
062     *         or an empty list if the collection is null
063     */
064    public static <T> List<T> unmodifiableList(Collection<? extends T> collection) {
065        final List<T> result;
066        if (collection == null) {
067            result = Collections.emptyList();
068        }
069        else {
070            result = List.copyOf(collection);
071        }
072        return result;
073    }
074
075    /**
076     * Returns an unmodifiable view of a List containing elements of a specific type.
077     *
078     * @param items The List of items to make unmodifiable.
079     * @param elementType The Class object representing the type of elements in the list.
080     * @param <S> The generic type of elements in the input Collection.
081     * @param <T> The type of elements in the resulting unmodifiable List.
082     * @return An unmodifiable List containing elements of the specified type.
083     */
084    public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) {
085        return items.stream()
086                .map(elementType::cast)
087                .toList();
088    }
089
090    /**
091     * Creates a copy of array.
092     *
093     * @param array Array to create a copy of
094     * @param length length of array
095     * @param <T> The type of array
096     * @return copy of array
097     */
098    public static <T> T[] copyOfArray(T[] array, int length) {
099        return Arrays.copyOf(array, length);
100    }
101
102    /**
103     * Returns an immutable set containing only the specified object.
104     *
105     * @param obj the type of object in the set
106     * @param <T> the type of object
107     * @return immutable set
108     */
109    public static <T> Set<T> singleton(T obj) {
110        return Collections.singleton(obj);
111    }
112}