    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.filefilter;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.nio.file.FileVisitResult;
    022import java.nio.file.Files;
    023import java.nio.file.Path;
    024import java.nio.file.attribute.BasicFileAttributes;
    025
    026/**
    027 * This filter accepts {@link File}s that are files (not directories).
    028 * <p>
    029 * For example, here is how to print out a list of the real files
    030 * within the current directory:
    031 * </p>
    032 * <h2>Using Classic IO</h2>
    033 * <pre>
    034 * File dir = FileUtils.current();
    035 * String[] files = dir.list(FileFileFilter.INSTANCE);
    036 * for (String file : files) {
    037 *     System.out.println(file);
    038 * }
    039 * </pre>
    040 *
    041 * <h2>Using NIO</h2>
    042 * <pre>
    043 * final Path dir = PathUtils.current();
    044 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(FileFileFilter.INSTANCE);
    045 * //
    046 * // Walk one directory
    047 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
    048 * System.out.println(visitor.getPathCounters());
    049 * System.out.println(visitor.getFileList());
    050 * //
    051 * visitor.getPathCounters().reset();
    052 * //
    053 * // Walk directory tree
    054 * Files.<strong>walkFileTree</strong>(dir, visitor);
    055 * System.out.println(visitor.getPathCounters());
    056 * System.out.println(visitor.getDirList());
    057 * System.out.println(visitor.getFileList());
    058 * </pre>
    059 * <h2>Deprecating Serialization</h2>
    060 * <p>
    061 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    062 * </p>
    063 *
    064 * @since 1.3
    065 * @see FileFilterUtils#fileFileFilter()
    066 */
    067public class FileFileFilter extends AbstractFileFilter implements Serializable {
    068
    069    /**
    070     * Singleton instance of file filter.
    071     *
    072     * @since 2.9.0
    073     */
    074    public static final IOFileFilter INSTANCE = new FileFileFilter();
    075
    076    /**
    077     * Singleton instance of file filter.
    078     *
    079     * @deprecated Use {@link #INSTANCE}.
    080     */
    081    @Deprecated
    082    public static final IOFileFilter FILE = INSTANCE;
    083
    084    private static final long serialVersionUID = 5345244090827540862L;
    085
    086    /**
    087     * Restrictive constructor.
    088     */
    089    protected FileFileFilter() {
    090    }
    091
    092    /**
    093     * Tests to see if the file is a file.
    094     *
    095     * @param file  the File to check.
    096     * @return true if the file is a file.
    097     */
    098    @Override
    099    public boolean accept(final File file) {
    100        return isFile(file);
    101    }
    102
    103    /**
    104     * Tests to see if the file is a file.
    105     *
    106     * @param file  the File to check.
    107     * @param attributes the path's basic attributes (may be null).
    108     * @return true if the file is a file.
    109     * @since 2.9.0
    110     */
    111    @Override
    112    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    113        return toFileVisitResult(file != null && Files.isRegularFile(file));
    114    }
    115
    116}