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.vfs2;
018
019import org.apache.commons.vfs2.util.Messages;
020
021/**
022 * A {@link org.apache.commons.vfs2.FileSelector} that selects all children of the given fileObject.
023 * <p>
024 * This is to mimic the {@link java.io.FileFilter} interface.
025 */
026public class FileFilterSelector extends FileDepthSelector {
027    /**
028     * The FileFilter.
029     */
030    private final FileFilter fileFilter;
031
032    public FileFilterSelector() {
033        this(null);
034    }
035
036    public FileFilterSelector(final FileFilter fileFilter) {
037        super(1, 1);
038        this.fileFilter = fileFilter;
039    }
040
041    /**
042     * Determines if a file or folder should be selected.
043     *
044     * @param fileInfo The file selection information.
045     * @return true if the file or folder should be included, false otherwise.
046     */
047    @Override
048    public boolean includeFile(final FileSelectInfo fileInfo) {
049        if (!super.includeFile(fileInfo)) {
050            return false;
051        }
052
053        return accept(fileInfo);
054    }
055
056    /**
057     * Determines whether the file should be selected.
058     *
059     * @param fileInfo The file selection information.
060     * @return true if the file should be selected, false otherwise.
061     */
062    public boolean accept(final FileSelectInfo fileInfo) {
063        if (fileFilter != null) {
064            return fileFilter.accept(fileInfo);
065        }
066
067        throw new IllegalArgumentException(Messages.getString("vfs.selectors/filefilter.missing.error"));
068    }
069}