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 {@code 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 = new File(".");
034 * String[] files = dir.list(CanWriteFileFilter.CAN_WRITE);
035 * for (String file : files) {
036 *     System.out.println(file);
037 * }
038 * </pre>
039 *
040 * <p>
041 * Example, showing how to print out a list of the current directory's <i>un-writable</i> files:
042 *
043 * <pre>
044 * File dir = new File(".");
045 * String[] files = dir.list(CanWriteFileFilter.CANNOT_WRITE);
046 * for (String file : files) {
047 *     System.out.println(file);
048 * }
049 * </pre>
050 *
051 * <p>
052 * <b>N.B.</b> For read-only files, use {@code CanReadFileFilter.READ_ONLY}.
053 *
054 * @since 1.3
055 */
056public class CanWriteFileFilter extends AbstractFileFilter implements Serializable {
057
058    /** Singleton instance of <i>writable</i> filter */
059    public static final IOFileFilter CAN_WRITE = new CanWriteFileFilter();
060
061    /** Singleton instance of not <i>writable</i> filter */
062    public static final IOFileFilter CANNOT_WRITE = CAN_WRITE.negate();
063
064    private static final long serialVersionUID = 5132005214688990379L;
065
066    /**
067     * Restrictive constructor.
068     */
069    protected CanWriteFileFilter() {
070    }
071
072    /**
073     * Checks to see if the file can be written to.
074     *
075     * @param file the File to check
076     * @return {@code true} if the file can be written to, otherwise {@code false}.
077     */
078    @Override
079    public boolean accept(final File file) {
080        return file.canWrite();
081    }
082
083    /**
084     * Checks to see if the file can be written to.
085     * @param file the File to check
086     *
087     * @return {@code true} if the file can be written to, otherwise {@code false}.
088     * @since 2.9.0
089     */
090    @Override
091    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
092        return toFileVisitResult(Files.isWritable(file), file);
093    }
094
095}