    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 *      http://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 {@code File}s that are symbolic links.
    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 = new File(".");
    035 * String[] files = dir.list(SymbolicLinkFileFilter.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 = Paths.get("");
    044 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(SymbolicLinkFileFilter.FILE);
    045 * //
    046 * // Walk one dir
    047 * Files.<b>walkFileTree</b>(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 dir tree
    054 * Files.<b>walkFileTree</b>(dir, visitor);
    055 * System.out.println(visitor.getPathCounters());
    056 * System.out.println(visitor.getDirList());
    057 * System.out.println(visitor.getFileList());
    058 * </pre>
    059 *
    060 * @since 2.11.0
    061 * @see FileFilterUtils#fileFileFilter()
    062 */
    063public class SymbolicLinkFileFilter extends AbstractFileFilter implements Serializable {
    064
    065    /**
    066     * Singleton instance of file filter.
    067     */
    068    public static final SymbolicLinkFileFilter INSTANCE = new SymbolicLinkFileFilter();
    069
    070    private static final long serialVersionUID = 1L;
    071
    072    /**
    073     * Restrictive constructor.
    074     */
    075    protected SymbolicLinkFileFilter() {
    076    }
    077
    078    /**
    079     * Checks to see if the file is a file.
    080     *
    081     * @param file  the File to check
    082     * @return true if the file is a file
    083     */
    084    @Override
    085    public boolean accept(final File file) {
    086        return file.isFile();
    087    }
    088
    089    /**
    090     * Checks to see if the file is a file.
    091     * @param file  the File to check
    092     *
    093     * @return true if the file is a file
    094     */
    095    @Override
    096    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    097        return toFileVisitResult(Files.isSymbolicLink(file), file);
    098    }
    099
    100}