1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.utils;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 /**
30 * <div>Note: it simply wraps the existing JDK methods to provide a workaround
31 * for Pitest survival on mutation for removal of immutable wrapping,
32 * see #13127 for more details.
33 * </div>
34 *
35 */
36 public final class UnmodifiableCollectionUtil {
37
38 /**
39 * Private constructor for UnmodifiableCollectionUtil.
40 *
41 */
42 private UnmodifiableCollectionUtil() {
43 }
44
45 /**
46 * Creates an unmodifiable list based on the provided collection.
47 *
48 * @param collection the collection to create an unmodifiable list from
49 * @param <T> the type of elements in the set
50 * @return an unmodifiable list containing the elements from the provided collection
51 */
52 public static <T> List<T> unmodifiableList(List<T> collection) {
53 return Collections.unmodifiableList(collection);
54 }
55
56 /**
57 * Returns an unmodifiable view of a List containing elements of a specific type.
58 *
59 * @param items The List of items to make unmodifiable.
60 * @param elementType The Class object representing the type of elements in the list.
61 * @param <S> The generic type of elements in the input Collection.
62 * @param <T> The type of elements in the resulting unmodifiable List.
63 * @return An unmodifiable List containing elements of the specified type.
64 */
65 public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) {
66 return items.stream()
67 .map(elementType::cast)
68 .toList();
69 }
70
71 /**
72 * Creates a copy of array.
73 *
74 * @param array Array to create a copy of
75 * @param length length of array
76 * @param <T> The type of array
77 * @return copy of array
78 */
79 public static <T> T[] copyOfArray(T[] array, int length) {
80 return Arrays.copyOf(array, length);
81 }
82
83 /**
84 * Creates a copy of Map.
85 *
86 * @param map map to create a copy of
87 * @param <K> the type of keys in the map
88 * @param <V> the type of values in the map
89 * @return an immutable copy of the input map
90 */
91 public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) {
92 return Map.copyOf(map);
93 }
94
95 /**
96 * Returns an immutable set containing only the specified object.
97 *
98 * @param obj the type of object in the set
99 * @param <T> the type of object
100 * @return immutable set
101 */
102 public static <T> Set<T> singleton(T obj) {
103 return Collections.singleton(obj);
104 }
105 }