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 *      https://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.io.filefilter;
018
019import java.io.File;
020import java.io.Serializable;
021import java.nio.file.FileVisitResult;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.nio.file.attribute.BasicFileAttributes;
025
026/**
027 * This filter accepts {@link File}s that are writable.
028 * <p>
029 * Example, showing how to print out a list of the current directory's <em>writable</em> files:
030 * </p>
031 * <h2>Using Classic IO</h2>
032 * <pre>
033 * File dir = FileUtils.current();
034 * String[] files = dir.list(CanWriteFileFilter.CAN_WRITE);
035 * for (String file : files) {
036 *     System.out.println(file);
037 * }
038 * </pre>
039 * <p>
040 * Example, showing how to print out a list of the current directory's <em>un-writable</em> files:
041 * </p>
042 * <pre>
043 * File dir = FileUtils.current();
044 * String[] files = dir.list(CanWriteFileFilter.CANNOT_WRITE);
045 * for (String file : files) {
046 *     System.out.println(file);
047 * }
048 * </pre>
049 * <p>
050 * <strong>N.B.</strong> For read-only files, use {@code CanReadFileFilter.READ_ONLY}.
051 * </p>
052 * <h2>Deprecating Serialization</h2>
053 * <p>
054 * <em>Serialization is deprecated and will be removed in 3.0.</em>
055 * </p>
056 *
057 * @see Files#isWritable(Path)
058 * @since 1.3
059 */
060public class CanWriteFileFilter extends AbstractFileFilter implements Serializable {
061
062    /** Singleton instance of <em>writable</em> filter */
063    public static final IOFileFilter CAN_WRITE = new CanWriteFileFilter();
064
065    /** Singleton instance of not <em>writable</em> filter */
066    public static final IOFileFilter CANNOT_WRITE = CAN_WRITE.negate();
067
068    private static final long serialVersionUID = 5132005214688990379L;
069
070    /**
071     * Restrictive constructor.
072     */
073    protected CanWriteFileFilter() {
074    }
075
076    /**
077     * Tests to see if the file can be written to.
078     *
079     * @param file the File to check.
080     * @return {@code true} if the file can be written to, otherwise {@code false}.
081     */
082    @Override
083    public boolean accept(final File file) {
084        return file != null && file.canWrite();
085    }
086
087    /**
088     * Tests to see if the file can be written to.
089     *
090     * @param file the File to check.
091     * @param attributes the path's basic attributes (may be null).
092     * @return {@code true} if the file can be written to, otherwise {@code false}.
093     * @since 2.9.0
094     */
095    @Override
096    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
097        return toFileVisitResult(file != null && Files.isWritable(file));
098    }
099
100}