View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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       * Creates an unmodifiable list from the provided collection.
58       * If the collection is null, returns an empty unmodifiable list.
59       *
60       * @param collection the collection to create an unmodifiable list from
61       * @param <T> the type of elements in the list
62       * @return an unmodifiable list containing the elements from the provided collection,
63       *         or an empty list if the collection is null
64       */
65      public static <T> List<T> unmodifiableList(Collection<? extends T> collection) {
66          final List<T> result;
67          if (collection == null) {
68              result = Collections.emptyList();
69          }
70          else {
71              result = List.copyOf(collection);
72          }
73          return result;
74      }
75  
76      /**
77       * Returns an unmodifiable view of a List containing elements of a specific type.
78       *
79       * @param items The List of items to make unmodifiable.
80       * @param elementType The Class object representing the type of elements in the list.
81       * @param <S> The generic type of elements in the input Collection.
82       * @param <T> The type of elements in the resulting unmodifiable List.
83       * @return An unmodifiable List containing elements of the specified type.
84       */
85      public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) {
86          return items.stream()
87                  .map(elementType::cast)
88                  .toList();
89      }
90  
91      /**
92       * Creates a copy of array.
93       *
94       * @param array Array to create a copy of
95       * @param length length of array
96       * @param <T> The type of array
97       * @return copy of array
98       */
99      public static <T> T[] copyOfArray(T[] array, int length) {
100         return Arrays.copyOf(array, length);
101     }
102 
103     /**
104      * Creates a copy of Map.
105      *
106      * @param map map to create a copy of
107      * @param <K> the type of keys in the map
108      * @param <V> the type of values in the map
109      * @return an immutable copy of the input map
110      */
111     public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) {
112         return Map.copyOf(map);
113     }
114 
115     /**
116      * Returns an immutable set containing only the specified object.
117      *
118      * @param obj the type of object in the set
119      * @param <T> the type of object
120      * @return immutable set
121      */
122     public static <T> Set<T> singleton(T obj) {
123         return Collections.singleton(obj);
124     }
125 }