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 edu.umd.cs.findbugs.annotations.CheckForNull;
021import java.io.IOException;
022import org.apache.hadoop.fs.FileStatus;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.fs.PathFilter;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028
029/**
030 * Typical base class for file status filter. Works more efficiently when filtering file statuses,
031 * otherwise implementation will need to lookup filestatus for the path which will be expensive.
032 */
033@InterfaceAudience.Private
034@InterfaceStability.Evolving
035public abstract class AbstractFileStatusFilter implements PathFilter, FileStatusFilter {
036
037  /**
038   * Filters out a path. Can be given an optional directory hint to avoid filestatus lookup.
039   * @param p     A filesystem path
040   * @param isDir An optional boolean indicating whether the path is a directory or not
041   * @return true if the path is accepted, false if the path is filtered out
042   */
043  protected abstract boolean accept(Path p, @CheckForNull Boolean isDir);
044
045  @Override
046  public boolean accept(FileStatus f) {
047    return accept(f.getPath(), f.isDirectory());
048  }
049
050  @Override
051  public boolean accept(Path p) {
052    return accept(p, null);
053  }
054
055  protected boolean isFile(FileSystem fs, @CheckForNull Boolean isDir, Path p) throws IOException {
056    return !isDirectory(fs, isDir, p);
057  }
058
059  protected boolean isDirectory(FileSystem fs, @CheckForNull Boolean isDir, Path p)
060    throws IOException {
061    return isDir != null ? isDir : fs.isDirectory(p);
062  }
063}