Coverage Report - org.apache.commons.io.filefilter.AndFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
AndFileFilter
93%
44/47
92%
24/26
2,8
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io.filefilter;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.Serializable;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collections;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * A {@link java.io.FileFilter} providing conditional AND logic across a list of
 27  
  * file filters. This filter returns {@code true} if all filters in the
 28  
  * list return {@code true}. Otherwise, it returns {@code false}.
 29  
  * Checking of the file filter list stops when the first filter returns
 30  
  * {@code false}.
 31  
  *
 32  
  * @since 1.0
 33  
  * @version $Id$
 34  
  *
 35  
  * @see FileFilterUtils#and(IOFileFilter...)
 36  
  */
 37  
 public class AndFileFilter
 38  
         extends AbstractFileFilter
 39  
         implements ConditionalFileFilter, Serializable {
 40  
 
 41  
     private static final long serialVersionUID = 7215974688563965257L;
 42  
 
 43  
     /** The list of file filters. */
 44  
     private final List<IOFileFilter> fileFilters;
 45  
 
 46  
     /**
 47  
      * Constructs a new instance of <code>AndFileFilter</code>.
 48  
      *
 49  
      * @since 1.1
 50  
      */
 51  13
     public AndFileFilter() {
 52  13
         this.fileFilters = new ArrayList<>();
 53  13
     }
 54  
 
 55  
     /**
 56  
      * Constructs a new instance of <code>AndFileFilter</code>
 57  
      * with the specified list of filters.
 58  
      *
 59  
      * @param fileFilters  a List of IOFileFilter instances, copied, null ignored
 60  
      * @since 1.1
 61  
      */
 62  81
     public AndFileFilter(final List<IOFileFilter> fileFilters) {
 63  81
         if (fileFilters == null) {
 64  1
             this.fileFilters = new ArrayList<>();
 65  
         } else {
 66  80
             this.fileFilters = new ArrayList<>(fileFilters);
 67  
         }
 68  81
     }
 69  
 
 70  
     /**
 71  
      * Constructs a new file filter that ANDs the result of two other filters.
 72  
      *
 73  
      * @param filter1  the first filter, must not be null
 74  
      * @param filter2  the second filter, must not be null
 75  
      * @throws IllegalArgumentException if either filter is null
 76  
      */
 77  27
     public AndFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
 78  27
         if (filter1 == null || filter2 == null) {
 79  2
             throw new IllegalArgumentException("The filters must not be null");
 80  
         }
 81  25
         this.fileFilters = new ArrayList<>(2);
 82  25
         addFileFilter(filter1);
 83  25
         addFileFilter(filter2);
 84  25
     }
 85  
 
 86  
     /**
 87  
      * {@inheritDoc}
 88  
      */
 89  
     @Override
 90  
     public void addFileFilter(final IOFileFilter ioFileFilter) {
 91  100
         this.fileFilters.add(ioFileFilter);
 92  100
     }
 93  
 
 94  
     /**
 95  
      * {@inheritDoc}
 96  
      */
 97  
     @Override
 98  
     public List<IOFileFilter> getFileFilters() {
 99  16
         return Collections.unmodifiableList(this.fileFilters);
 100  
     }
 101  
 
 102  
     /**
 103  
      * {@inheritDoc}
 104  
      */
 105  
     @Override
 106  
     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
 107  4
         return this.fileFilters.remove(ioFileFilter);
 108  
     }
 109  
 
 110  
     /**
 111  
      * {@inheritDoc}
 112  
      */
 113  
     @Override
 114  
     public void setFileFilters(final List<IOFileFilter> fileFilters) {
 115  0
         this.fileFilters.clear();
 116  0
         this.fileFilters.addAll(fileFilters);
 117  0
     }
 118  
 
 119  
     /**
 120  
      * {@inheritDoc}
 121  
      */
 122  
     @Override
 123  
     public boolean accept(final File file) {
 124  1333
         if (this.fileFilters.isEmpty()) {
 125  3
             return false;
 126  
         }
 127  1330
         for (final IOFileFilter fileFilter : fileFilters) {
 128  2067
             if (!fileFilter.accept(file)) {
 129  1005
                 return false;
 130  
             }
 131  1062
         }
 132  325
         return true;
 133  
     }
 134  
 
 135  
     /**
 136  
      * {@inheritDoc}
 137  
      */
 138  
     @Override
 139  
     public boolean accept(final File file, final String name) {
 140  55
         if (this.fileFilters.isEmpty()) {
 141  1
             return false;
 142  
         }
 143  54
         for (final IOFileFilter fileFilter : fileFilters) {
 144  88
             if (!fileFilter.accept(file, name)) {
 145  40
                 return false;
 146  
             }
 147  48
         }
 148  14
         return true;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Provide a String representation of this file filter.
 153  
      *
 154  
      * @return a String representation
 155  
      */
 156  
     @Override
 157  
     public String toString() {
 158  53
         final StringBuilder buffer = new StringBuilder();
 159  53
         buffer.append(super.toString());
 160  53
         buffer.append("(");
 161  53
         if (fileFilters != null) {
 162  155
             for (int i = 0; i < fileFilters.size(); i++) {
 163  102
                 if (i > 0) {
 164  52
                     buffer.append(",");
 165  
                 }
 166  102
                 final Object filter = fileFilters.get(i);
 167  102
                 buffer.append(filter == null ? "null" : filter.toString());
 168  
             }
 169  
         }
 170  53
         buffer.append(")");
 171  53
         return buffer.toString();
 172  
     }
 173  
 
 174  
 }