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;
024
025/**
026 * This filter accepts {@code File}s that are hidden.
027 * <p>
028 * Example, showing how to print out a list of the current directory's
029 * <em>hidden</em> files:
030 * </p>
031 *
032 * <pre>
033 * FileSystemManager fsManager = VFS.getManager();
034 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
035 * FileObject[] files = dir.findFiles(new FileFilterSelector(HiddenFileFilter.HIDDEN));
036 * for (int i = 0; i &lt; files.length; i++) {
037 *     System.out.println(files[i]);
038 * }
039 * </pre>
040 *
041 * <p>
042 * Example, showing how to print out a list of the current directory's
043 * <em>visible</em> (i.e. not hidden) files:
044 * </p>
045 *
046 * <pre>
047 * FileSystemManager fsManager = VFS.getManager();
048 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
049 * FileObject[] files = dir.findFiles(new FileFilterSelector(HiddenFileFilter.VISIBLE));
050 * for (int i = 0; i &lt; files.length; i++) {
051 *     System.out.println(files[i]);
052 * }
053 * </pre>
054 *
055 * @author This code was originally ported from Apache Commons IO File Filter
056 * @see "https://commons.apache.org/proper/commons-io/"
057 * @since 2.4
058 */
059public class HiddenFileFilter implements FileFilter, Serializable {
060
061    /** Singleton instance of <em>hidden</em> filter. */
062    public static final FileFilter HIDDEN = new HiddenFileFilter();
063
064    /** Singleton instance of <em>visible</em> filter. */
065    public static final FileFilter VISIBLE = new NotFileFilter(HIDDEN);
066
067    private static final long serialVersionUID = 1L;
068
069    /**
070     * Restrictive constructor.
071     */
072    protected HiddenFileFilter() {
073    }
074
075    /**
076     * Checks to see if the file is hidden. Non-existing files won't be accepted.
077     *
078     * @param fileSelectInfo the File to check
079     * @return {@code true} if the file is <em>hidden</em>, otherwise {@code false}.
080     * @throws FileSystemException Thrown for file system errors.
081     */
082    @Override
083    public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
084        if (!fileSelectInfo.getFile().exists()) {
085            return false;
086        }
087        return fileSelectInfo.getFile().isHidden();
088    }
089
090}