001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 * Creates an unmodifiable list from the provided collection. 058 * If the collection is null, returns an empty unmodifiable list. 059 * 060 * @param collection the collection to create an unmodifiable list from 061 * @param <T> the type of elements in the list 062 * @return an unmodifiable list containing the elements from the provided collection, 063 * or an empty list if the collection is null 064 */ 065 public static <T> List<T> unmodifiableList(Collection<? extends T> collection) { 066 final List<T> result; 067 if (collection == null) { 068 result = Collections.emptyList(); 069 } 070 else { 071 result = List.copyOf(collection); 072 } 073 return result; 074 } 075 076 /** 077 * Returns an unmodifiable view of a List containing elements of a specific type. 078 * 079 * @param items The List of items to make unmodifiable. 080 * @param elementType The Class object representing the type of elements in the list. 081 * @param <S> The generic type of elements in the input Collection. 082 * @param <T> The type of elements in the resulting unmodifiable List. 083 * @return An unmodifiable List containing elements of the specified type. 084 */ 085 public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) { 086 return items.stream() 087 .map(elementType::cast) 088 .toList(); 089 } 090 091 /** 092 * Creates a copy of array. 093 * 094 * @param array Array to create a copy of 095 * @param length length of array 096 * @param <T> The type of array 097 * @return copy of array 098 */ 099 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}