Coverage Summary for Class: PatternFilenameFilter (com.google.common.io)

Class Class, % Method, % Line, %
PatternFilenameFilter 0% (0/1) 0% (0/3) 0% (0/4)


1 /* 2  * Copyright (C) 2006 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.io; 16  17 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.GwtIncompatible; 19 import com.google.common.base.Preconditions; 20 import java.io.File; 21 import java.io.FilenameFilter; 22 import java.util.regex.Pattern; 23 import java.util.regex.PatternSyntaxException; 24 import javax.annotation.CheckForNull; 25  26 /** 27  * File name filter that only accepts files matching a regular expression. This class is thread-safe 28  * and immutable. 29  * 30  * @author Apple Chow 31  * @since 1.0 32  */ 33 @Beta 34 @GwtIncompatible 35 @ElementTypesAreNonnullByDefault 36 public final class PatternFilenameFilter implements FilenameFilter { 37  38  private final Pattern pattern; 39  40  /** 41  * Constructs a pattern file name filter object. 42  * 43  * @param patternStr the pattern string on which to filter file names 44  * @throws PatternSyntaxException if pattern compilation fails (runtime) 45  */ 46  public PatternFilenameFilter(String patternStr) { 47  this(Pattern.compile(patternStr)); 48  } 49  50  /** 51  * Constructs a pattern file name filter object. 52  * 53  * @param pattern the pattern on which to filter file names 54  */ 55  public PatternFilenameFilter(Pattern pattern) { 56  this.pattern = Preconditions.checkNotNull(pattern); 57  } 58  59  /* 60  * Our implementation works fine with a null `dir`. However, there's nothing in the documentation 61  * of the supertype that suggests that implementations are expected to tolerate null. That said, I 62  * see calls in Google code that pass a null `dir` to a FilenameFilter.... So let's declare the 63  * parameter as non-nullable (since passing null to a FilenameFilter is unsafe in general), but if 64  * someone still manages to pass null, let's continue to have the method work. 65  * 66  * (OK, I haven't done that yet, but I will when I follow through on the TODO below after updating 67  * callers.) 68  * 69  * (PatternFilenameFilter is of course one of those classes that shouldn't be a publicly visible 70  * class to begin with but rather something returned from a static factory method whose declared 71  * return type is plain FilenameFilter. If we made such a change, then the annotation we choose 72  * here would have no significance to end users, who would be forced to conform to the signature 73  * used in FilenameFilter.) 74  */ 75  @Override 76  @SuppressWarnings("nullness") 77  public boolean accept( 78  // TODO(b/147136275): Remove @CheckForNull, and remove suppression. 79  @CheckForNull File dir, String fileName) { 80  return pattern.matcher(fileName).matches(); 81  } 82 }