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.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 can be written to.
028 * <p>
029 * Example, showing how to print out a list of the current directory's <i>writable</i> 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 <i>un-writable</i> 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 * <b>N.B.</b> 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 * @since 1.3
058 */
059public class CanWriteFileFilter extends AbstractFileFilter implements Serializable {
060
061    /** Singleton instance of <i>writable</i> filter */
062    public static final IOFileFilter CAN_WRITE = new CanWriteFileFilter();
063
064    /** Singleton instance of not <i>writable</i> filter */
065    public static final IOFileFilter CANNOT_WRITE = CAN_WRITE.negate();
066
067    private static final long serialVersionUID = 5132005214688990379L;
068
069    /**
070     * Restrictive constructor.
071     */
072    protected CanWriteFileFilter() {
073    }
074
075    /**
076     * Checks to see if the file can be written to.
077     *
078     * @param file the File to check
079     * @return {@code true} if the file can be written to, otherwise {@code false}.
080     */
081    @Override
082    public boolean accept(final File file) {
083        return file != null && file.canWrite();
084    }
085
086    /**
087     * Checks to see if the file can be written to.
088     * @param file the File to check
089     *
090     * @return {@code true} if the file can be written to, otherwise {@code false}.
091     * @since 2.9.0
092     */
093    @Override
094    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
095        return toFileVisitResult(file != null && Files.isWritable(file));
096    }
097
098}