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 Path} 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 PathEqualsFileFilter extends AbstractFileFilter {
034
035    private final Path path;
036
037    /**
038     * Constructs a new instance for the given {@link Path}.
039     *
040     * @param file The file to match.
041     */
042    public PathEqualsFileFilter(final Path file) {
043        this.path = file;
044    }
045
046    @Override
047    public boolean accept(final File file) {
048        return Objects.equals(this.path, file.toPath());
049    }
050
051    @Override
052    public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
053        return toFileVisitResult(Objects.equals(this.path, path));
054    }
055}