001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.IOException;
021
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.yetus.audience.InterfaceStability;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.fs.PathFilter;
028
029import edu.umd.cs.findbugs.annotations.CheckForNull;
030
031/**
032 * Typical base class for file status filter.  Works more efficiently when
033 * filtering file statuses, otherwise implementation will need to lookup filestatus
034 * for the path which will be expensive.
035 */
036@InterfaceAudience.Private
037@InterfaceStability.Evolving
038public abstract class AbstractFileStatusFilter implements PathFilter, FileStatusFilter {
039
040  /**
041   * Filters out a path.  Can be given an optional directory hint to avoid
042   * filestatus lookup.
043   *
044   * @param p       A filesystem path
045   * @param isDir   An optional boolean indicating whether the path is a directory or not
046   * @return        true if the path is accepted, false if the path is filtered out
047   */
048  protected abstract boolean accept(Path p, @CheckForNull Boolean isDir);
049
050  @Override
051  public boolean accept(FileStatus f) {
052    return accept(f.getPath(), f.isDirectory());
053  }
054
055  @Override
056  public boolean accept(Path p) {
057    return accept(p, null);
058  }
059
060  protected boolean isFile(FileSystem fs, @CheckForNull Boolean isDir, Path p) throws IOException {
061    return !isDirectory(fs, isDir, p);
062  }
063
064  protected boolean isDirectory(FileSystem fs, @CheckForNull Boolean isDir, Path p) throws IOException {
065    return isDir != null ? isDir : fs.isDirectory(p);
066  }
067}