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 produces a logical NOT of the filters specified. 027 * 028 * @author This code was originally ported from Apache Commons IO File Filter 029 * @see "https://commons.apache.org/proper/commons-io/" 030 * @since 2.4 031 */ 032public class NotFileFilter implements FileFilter, Serializable { 033 034 private static final long serialVersionUID = 1L; 035 036 /** The filter. */ 037 private final FileFilter filter; 038 039 /** 040 * Constructs a new file filter that NOTs the result of another filter. 041 * 042 * @param filter the filter, must not be null 043 */ 044 public NotFileFilter(final FileFilter filter) { 045 if (filter == null) { 046 throw new IllegalArgumentException("The filter must not be null"); 047 } 048 this.filter = filter; 049 } 050 051 /** 052 * Returns the logical NOT of the underlying filter's return value for the same 053 * File. 054 * 055 * @param fileSelectInfo the File to check 056 * @return {@code true} if the filter returns {@code false} 057 * @throws FileSystemException Thrown for file system errors. 058 */ 059 @Override 060 public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException { 061 return !filter.accept(fileSelectInfo); 062 } 063 064 /** 065 * Provide a String representation of this file filter. 066 * 067 * @return a String representation 068 */ 069 @Override 070 public String toString() { 071 return super.toString() + "(" + filter + ")"; 072 } 073 074}