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.api; 021 022import java.io.File; 023import java.util.Objects; 024 025/** 026 * Stateless helpers for API classes that historically contain implementation code. 027 * These helpers can be removed together with that implementation in a future API redesign. 028 */ 029final class ApiUtil { 030 031 /** Private constructor for ApiUtil. */ 032 private ApiUtil() { 033 } 034 035 /** 036 * Returns whether the file extension matches what we are meant to process. 037 * 038 * @param file 039 * the file to be checked. 040 * @param fileExtensions 041 * file extensions normalized by 042 * {@link AbstractFileSetCheck#setFileExtensions(String...)}; 043 * null or empty arrays match all files. 044 * @return whether there is a match. 045 */ 046 /* package */ static boolean matchesFileExtension(File file, String... fileExtensions) { 047 boolean result = false; 048 if (fileExtensions == null || fileExtensions.length == 0) { 049 result = true; 050 } 051 else { 052 // A failed setter can leave a null after an otherwise matching extension. 053 for (final String fileExtension : fileExtensions) { 054 Objects.requireNonNull(fileExtension); 055 } 056 057 final String fileName = file.getName(); 058 for (final String fileExtension : fileExtensions) { 059 if (fileName.endsWith(fileExtension)) { 060 result = true; 061 break; 062 } 063 } 064 } 065 066 return result; 067 } 068 069 /** 070 * Returns the length of a String prefix with tabs expanded. 071 * Each tab is counted as the number of characters is takes to jump to the next tab stop. 072 * 073 * @param inputString 074 * the input String 075 * @param toIdx 076 * index in string (exclusive) where the calculation stops 077 * @param tabWidth 078 * the distance between tab stop position. 079 * @return the length of string.substring(0, toIdx) with tabs expanded. 080 */ 081 /* package */ static int lengthExpandedTabs(String inputString, 082 int toIdx, 083 int tabWidth) { 084 int len = 0; 085 for (int idx = 0; idx < toIdx; idx++) { 086 if (inputString.codePointAt(idx) == '\t') { 087 len = (len / tabWidth + 1) * tabWidth; 088 } 089 else { 090 len++; 091 } 092 } 093 return len; 094 } 095 096}