001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.Map; 027import java.util.Set; 028 029/** 030 * <div>Note: it simply wraps the existing JDK methods to provide a workaround 031 * for Pitest survival on mutation for removal of immutable wrapping, 032 * see #13127 for more details. 033 * </div> 034 * 035 */ 036public final class UnmodifiableCollectionUtil { 037 038 /** 039 * Private constructor for UnmodifiableCollectionUtil. 040 * 041 */ 042 private UnmodifiableCollectionUtil() { 043 } 044 045 /** 046 * Creates an unmodifiable list based on the provided collection. 047 * 048 * @param collection the collection to create an unmodifiable list from 049 * @param <T> the type of elements in the set 050 * @return an unmodifiable list containing the elements from the provided collection 051 */ 052 public static <T> List<T> unmodifiableList(List<T> collection) { 053 return Collections.unmodifiableList(collection); 054 } 055 056 /** 057 * Returns an unmodifiable view of a List containing elements of a specific type. 058 * 059 * @param items The List of items to make unmodifiable. 060 * @param elementType The Class object representing the type of elements in the list. 061 * @param <S> The generic type of elements in the input Collection. 062 * @param <T> The type of elements in the resulting unmodifiable List. 063 * @return An unmodifiable List containing elements of the specified type. 064 */ 065 public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) { 066 return items.stream() 067 .map(elementType::cast) 068 .toList(); 069 } 070 071 /** 072 * Creates a copy of array. 073 * 074 * @param array Array to create a copy of 075 * @param length length of array 076 * @param <T> The type of array 077 * @return copy of array 078 */ 079 public static <T> T[] copyOfArray(T[] array, int length) { 080 return Arrays.copyOf(array, length); 081 } 082 083 /** 084 * Creates a copy of Map. 085 * 086 * @param map map to create a copy of 087 * @param <K> the type of keys in the map 088 * @param <V> the type of values in the map 089 * @return an immutable copy of the input map 090 */ 091 public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) { 092 return Map.copyOf(map); 093 } 094 095 /** 096 * Returns an immutable set containing only the specified object. 097 * 098 * @param obj the type of object in the set 099 * @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}