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 */
017
018package org.apache.commons.io.filefilter;
019
020import java.io.File;
021import java.nio.file.FileVisitResult;
022import java.nio.file.Path;
023import java.nio.file.attribute.BasicFileAttributes;
024import java.util.Objects;
025
026/**
027 * Accepts only an exact {@link File} object match. You can use this filter to visit the start directory when walking a
028 * file tree with
029 * {@link java.nio.file.Files#walkFileTree(java.nio.file.Path, java.util.Set, int, java.nio.file.FileVisitor)}.
030 *
031 * @since 2.9.0
032 */
033public class FileEqualsFileFilter extends AbstractFileFilter {
034
035    private final File file;
036    private final Path path;
037
038    /**
039     * Constructs a new instance for the given {@link File}.
040     *
041     * @param file The file to match.
042     */
043    public FileEqualsFileFilter(final File file) {
044        this.file = Objects.requireNonNull(file, "file");
045        this.path = file.toPath();
046    }
047
048    @Override
049    public boolean accept(final File file) {
050        return Objects.equals(this.file, file);
051    }
052
053    @Override
054    public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
055        return toFileVisitResult(Objects.equals(this.path, path), path);
056    }
057}