View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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  import java.util.stream.Collectors;
29  
30  /**
31   * <div>Note: it simply wraps the existing JDK methods to provide a workaround
32   * for Pitest survival on mutation for removal of immutable wrapping,
33   * see #13127 for more details.
34   * </div>
35   *
36   */
37  public final class UnmodifiableCollectionUtil {
38  
39      /**
40       * Private constructor for UnmodifiableCollectionUtil.
41       *
42       */
43      private UnmodifiableCollectionUtil() {
44      }
45  
46      /**
47       * Creates an unmodifiable list based on the provided collection.
48       *
49       * @param collection the collection to create an unmodifiable list from
50       * @param <T> the type of elements in the set
51       * @return an unmodifiable list containing the elements from the provided collection
52       */
53      public static <T> List<T> unmodifiableList(List<T> collection) {
54          return Collections.unmodifiableList(collection);
55      }
56  
57      /**
58       * Returns an unmodifiable view of a List containing elements of a specific type.
59       *
60       * @param items The List of items to make unmodifiable.
61       * @param elementType The Class object representing the type of elements in the list.
62       * @param <S> The generic type of elements in the input Collection.
63       * @param <T> The type of elements in the resulting unmodifiable List.
64       * @return An unmodifiable List containing elements of the specified type.
65       */
66      public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) {
67          return items.stream()
68                  .map(elementType::cast)
69                  .collect(Collectors.toUnmodifiableList());
70      }
71  
72      /**
73       * Creates a copy of array.
74       *
75       * @param array Array to create a copy of
76       * @param length length of array
77       * @param <T> The type of array
78       * @return copy of array
79       */
80      public static <T> T[] copyOfArray(T[] array, int length) {
81          return Arrays.copyOf(array, length);
82      }
83  
84      /**
85       * Creates a copy of Map.
86       *
87       * @param map map to create a copy of
88       * @param <K> the type of keys in the map
89       * @param <V> the type of values in the map
90       * @return an immutable copy of the input map
91       */
92      public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) {
93          return Map.copyOf(map);
94      }
95  
96      /**
97       * Returns an immutable set containing only the specified object.
98       *
99       * @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 }