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.Set;
27  
28  /**
29   * <div>Note: it simply wraps the existing JDK methods to provide a workaround
30   * for Pitest survival on mutation for removal of immutable wrapping,
31   * see #13127 for more details.
32   * </div>
33   *
34   */
35  public final class UnmodifiableCollectionUtil {
36  
37      /**
38       * Private constructor for UnmodifiableCollectionUtil.
39       *
40       */
41      private UnmodifiableCollectionUtil() {
42      }
43  
44      /**
45       * Creates an unmodifiable list based on the provided collection.
46       * This does NOT handle null — if called with null it will throw NPE.
47       * This is intentional: see
48       * <a href="https://github.com/hcoles/pitest/issues/1462">pitest issue #1462</a>.
49       *
50       * @param collection the collection to create an unmodifiable list from
51       * @param <T> the type of elements in the set
52       * @return an unmodifiable list containing the elements from the provided collection
53       */
54      public static <T> List<T> unmodifiableList(List<T> collection) {
55          return Collections.unmodifiableList(collection);
56      }
57  
58      /**
59       * Creates an unmodifiable list from the provided collection.
60       * If the collection is null, returns an empty unmodifiable list.
61       *
62       * @param collection the collection to create an unmodifiable list from
63       * @param <T> the type of elements in the list
64       * @return an unmodifiable list containing the elements from the provided collection,
65       *         or an empty list if the collection is null
66       */
67      public static <T> List<T> unmodifiableList(Collection<? extends T> collection) {
68          final List<T> result;
69          if (collection == null) {
70              result = Collections.emptyList();
71          }
72          else {
73              result = List.copyOf(collection);
74          }
75          return result;
76      }
77  
78      /**
79       * Returns an unmodifiable view of a List containing elements of a specific type.
80       *
81       * @param items The List of items to make unmodifiable.
82       * @param elementType The Class object representing the type of elements in the list.
83       * @param <S> The generic type of elements in the input Collection.
84       * @param <T> The type of elements in the resulting unmodifiable List.
85       * @return An unmodifiable List containing elements of the specified type.
86       */
87      public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) {
88          return items.stream()
89                  .map(elementType::cast)
90                  .toList();
91      }
92  
93      /**
94       * Creates a copy of array.
95       *
96       * @param array Array to create a copy of
97       * @param length length of array
98       * @param <T> The type of array
99       * @return copy of array
100      */
101     public static <T> T[] copyOfArray(T[] array, int length) {
102         return Arrays.copyOf(array, length);
103     }
104 
105     /**
106      * Returns an immutable set containing only the specified object.
107      *
108      * @param obj the type of object in the set
109      * @param <T> the type of object
110      * @return immutable set
111      */
112     public static <T> Set<T> singleton(T obj) {
113         return Collections.singleton(obj);
114     }
115 }