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.Beta;
018import com.google.common.annotations.GwtIncompatible;
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@Beta
033@GwtIncompatible
034@ElementTypesAreNonnullByDefault
035public final class PatternFilenameFilter implements FilenameFilter {
036
037  private final Pattern pattern;
038
039  /**
040   * Constructs a pattern file name filter object.
041   *
042   * @param patternStr the pattern string on which to filter file names
043   * @throws PatternSyntaxException if pattern compilation fails (runtime)
044   */
045  public PatternFilenameFilter(String patternStr) {
046    this(Pattern.compile(patternStr));
047  }
048
049  /**
050   * Constructs a pattern file name filter object.
051   *
052   * @param pattern the pattern on which to filter file names
053   */
054  public PatternFilenameFilter(Pattern pattern) {
055    this.pattern = Preconditions.checkNotNull(pattern);
056  }
057
058  /*
059   * Our implementation works fine with a null `dir`. However, there's nothing in the documentation
060   * of the supertype that suggests that implementations are expected to tolerate null. That said, I
061   * see calls in Google code that pass a null `dir` to a FilenameFilter.... So let's declare the
062   * parameter as non-nullable (since passing null to a FilenameFilter is unsafe in general), but if
063   * someone still manages to pass null, let's continue to have the method work.
064   *
065   * (PatternFilenameFilter is of course one of those classes that shouldn't be a publicly visible
066   * class to begin with but rather something returned from a static factory method whose declared
067   * return type is plain FilenameFilter. If we made such a change, then the annotation we choose
068   * here would have no significance to end users, who would be forced to conform to the signature
069   * used in FilenameFilter.)
070   */
071  @Override
072  public boolean accept(File dir, String fileName) {
073    return pattern.matcher(fileName).matches();
074  }
075}