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.Set; 027 028/** 029 * <div>Note: it simply wraps the existing JDK methods to provide a workaround 030 * for Pitest survival on mutation for removal of immutable wrapping, 031 * see #13127 for more details. 032 * </div> 033 * 034 */ 035public final class UnmodifiableCollectionUtil { 036 037 /** 038 * Private constructor for UnmodifiableCollectionUtil. 039 * 040 */ 041 private UnmodifiableCollectionUtil() { 042 } 043 044 /** 045 * Creates an unmodifiable list based on the provided collection. 046 * This does NOT handle null — if called with null it will throw NPE. 047 * This is intentional: see 048 * <a href="https://github.com/hcoles/pitest/issues/1462">pitest issue #1462</a>. 049 * 050 * @param collection the collection to create an unmodifiable list from 051 * @param <T> the type of elements in the set 052 * @return an unmodifiable list containing the elements from the provided collection 053 */ 054 public static <T> List<T> unmodifiableList(List<T> collection) { 055 return Collections.unmodifiableList(collection); 056 } 057 058 /** 059 * Creates an unmodifiable list from the provided collection. 060 * If the collection is null, returns an empty unmodifiable list. 061 * 062 * @param collection the collection to create an unmodifiable list from 063 * @param <T> the type of elements in the list 064 * @return an unmodifiable list containing the elements from the provided collection, 065 * or an empty list if the collection is null 066 */ 067 public static <T> List<T> unmodifiableList(Collection<? extends T> collection) { 068 final List<T> result; 069 if (collection == null) { 070 result = List.of(); 071 } 072 else { 073 result = List.copyOf(collection); 074 } 075 return result; 076 } 077 078 /** 079 * Returns an unmodifiable view of a List containing elements of a specific type. 080 * 081 * @param items The List of items to make unmodifiable. 082 * @param elementType The Class object representing the type of elements in the list. 083 * @param <S> The generic type of elements in the input Collection. 084 * @param <T> The type of elements in the resulting unmodifiable List. 085 * @return An unmodifiable List containing elements of the specified type. 086 */ 087 public static <S, T> List<T> unmodifiableList(Collection<S> items, Class<T> elementType) { 088 return items.stream() 089 .map(elementType::cast) 090 .toList(); 091 } 092 093 /** 094 * Creates a copy of array. 095 * 096 * @param array Array to create a copy of 097 * @param length length of array 098 * @param <T> The type of array 099 * @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 Set.of(obj); 114 } 115 116}