001/*
002 * Copyright (C) 2006 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.io;
016
017import com.google.common.annotations.GwtIncompatible;
018import com.google.common.annotations.J2ktIncompatible;
019import com.google.common.base.Preconditions;
020import java.io.File;
021import java.io.FilenameFilter;
022import java.util.regex.Pattern;
023import java.util.regex.PatternSyntaxException;
024
025/**
026 * File name filter that only accepts files matching a regular expression. This class is thread-safe
027 * and immutable.
028 *
029 * @author Apple Chow
030 * @since 1.0
031 */
032@J2ktIncompatible
033@GwtIncompatible
034public final class PatternFilenameFilter implements FilenameFilter {
035
036  private final Pattern pattern;
037
038  /**
039   * Constructs a pattern file name filter object.
040   *
041   * @param patternStr the pattern string on which to filter file names
042   * @throws PatternSyntaxException if pattern compilation fails (runtime)
043   */
044  public PatternFilenameFilter(String patternStr) {
045    this(Pattern.compile(patternStr));
046  }
047
048  /**
049   * Constructs a pattern file name filter object.
050   *
051   * @param pattern the pattern on which to filter file names
052   */
053  public PatternFilenameFilter(Pattern pattern) {
054    this.pattern = Preconditions.checkNotNull(pattern);
055  }
056
057  /*
058   * Our implementation works fine with a null `dir`. However, there's nothing in the documentation
059   * of the supertype that suggests that implementations are expected to tolerate null. That said, I
060   * see calls in Google code that pass a null `dir` to a FilenameFilter.... So let's declare the
061   * parameter as non-nullable (since passing null to a FilenameFilter is unsafe in general), but if
062   * someone still manages to pass null, let's continue to have the method work.
063   *
064   * (PatternFilenameFilter is of course one of those classes that shouldn't be a publicly visible
065   * class to begin with but rather something returned from a static factory method whose declared
066   * return type is plain FilenameFilter. If we made such a change, then the annotation we choose
067   * here would have no significance to end users, who would be forced to conform to the signature
068   * used in FilenameFilter.)
069   */
070  @Override
071  public boolean accept(File dir, String fileName) {
072    return pattern.matcher(fileName).matches();
073  }
074}