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.filter;
018
019import java.io.Serializable;
020
021import org.apache.commons.vfs2.FileFilter;
022import org.apache.commons.vfs2.FileSelectInfo;
023import org.apache.commons.vfs2.FileSystemException;
024import org.apache.commons.vfs2.FileType;
025
026/**
027 * This filter accepts {@code File}s that are directories.
028 * <p>
029 * For example, here is how to print out a list of the current directory's sub
030 * directories:
031 * </p>
032 *
033 * <pre>
034 * FileSystemManager fsManager = VFS.getManager();
035 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
036 * FileObject[] files = dir.findFiles(new FileFilterSelector(DirectoryFileFilter.DIRECTORY));
037 * for (int i = 0; i &lt; files.length; i++) {
038 *     System.out.println(files[i]);
039 * }
040 * </pre>
041 *
042 * @author This code was originally ported from Apache Commons IO File Filter
043 * @see "https://commons.apache.org/proper/commons-io/"
044 * @since 2.4
045 */
046public class DirectoryFileFilter implements FileFilter, Serializable {
047
048    /**
049     * Singleton instance of directory filter.
050     */
051    public static final FileFilter DIRECTORY = new DirectoryFileFilter();
052
053    private static final long serialVersionUID = 1L;
054
055    /**
056     * Restrictive constructor.
057     */
058    protected DirectoryFileFilter() {
059    }
060
061    /**
062     * Checks to see if the file is a directory.
063     *
064     * @param fileSelectInfo the File to check
065     * @return {@code true} if the file is a directory
066     * @throws FileSystemException Thrown for file system errors.
067     */
068    @Override
069    public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
070        return fileSelectInfo.getFile().getType() == FileType.FOLDER;
071    }
072
073}