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: AndFileFilter.java 1415850 2012-11-30 20:51:39Z ggregory $
 34  
  *
 35  
  * @see FileFilterUtils#and(IOFileFilter...)
 36  
  */
 37  
 public class AndFileFilter
 38  
         extends AbstractFileFilter
 39  
         implements ConditionalFileFilter, Serializable {
 40  
 
 41  
     /** The list of file filters. */
 42  
     private final List<IOFileFilter> fileFilters;
 43  
 
 44  
     /**
 45  
      * Constructs a new instance of <code>AndFileFilter</code>.
 46  
      *
 47  
      * @since 1.1
 48  
      */
 49  26
     public AndFileFilter() {
 50  26
         this.fileFilters = new ArrayList<IOFileFilter>();
 51  26
     }
 52  
 
 53  
     /**
 54  
      * Constructs a new instance of <code>AndFileFilter</code>
 55  
      * with the specified list of filters.
 56  
      *
 57  
      * @param fileFilters  a List of IOFileFilter instances, copied, null ignored
 58  
      * @since 1.1
 59  
      */
 60  162
     public AndFileFilter(final List<IOFileFilter> fileFilters) {
 61  162
         if (fileFilters == null) {
 62  2
             this.fileFilters = new ArrayList<IOFileFilter>();
 63  
         } else {
 64  160
             this.fileFilters = new ArrayList<IOFileFilter>(fileFilters);
 65  
         }
 66  162
     }
 67  
 
 68  
     /**
 69  
      * Constructs a new file filter that ANDs the result of two other filters.
 70  
      *
 71  
      * @param filter1  the first filter, must not be null
 72  
      * @param filter2  the second filter, must not be null
 73  
      * @throws IllegalArgumentException if either filter is null
 74  
      */
 75  54
     public AndFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
 76  54
         if (filter1 == null || filter2 == null) {
 77  4
             throw new IllegalArgumentException("The filters must not be null");
 78  
         }
 79  50
         this.fileFilters = new ArrayList<IOFileFilter>(2);
 80  50
         addFileFilter(filter1);
 81  50
         addFileFilter(filter2);
 82  50
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public void addFileFilter(final IOFileFilter ioFileFilter) {
 88  200
         this.fileFilters.add(ioFileFilter);
 89  200
     }
 90  
 
 91  
     /**
 92  
      * {@inheritDoc}
 93  
      */
 94  
     public List<IOFileFilter> getFileFilters() {
 95  32
         return Collections.unmodifiableList(this.fileFilters);
 96  
     }
 97  
 
 98  
     /**
 99  
      * {@inheritDoc}
 100  
      */
 101  
     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
 102  8
         return this.fileFilters.remove(ioFileFilter);
 103  
     }
 104  
 
 105  
     /**
 106  
      * {@inheritDoc}
 107  
      */
 108  
     public void setFileFilters(final List<IOFileFilter> fileFilters) {
 109  0
         this.fileFilters.clear();
 110  0
         this.fileFilters.addAll(fileFilters);
 111  0
     }
 112  
 
 113  
     /**
 114  
      * {@inheritDoc}
 115  
      */
 116  
     @Override
 117  
     public boolean accept(final File file) {
 118  2474
         if (this.fileFilters.isEmpty()) {
 119  6
             return false;
 120  
         }
 121  2468
         for (final IOFileFilter fileFilter : fileFilters) {
 122  3846
             if (!fileFilter.accept(file)) {
 123  1842
                 return false;
 124  
             }
 125  2004
         }
 126  626
         return true;
 127  
     }
 128  
 
 129  
     /**
 130  
      * {@inheritDoc}
 131  
      */
 132  
     @Override
 133  
     public boolean accept(final File file, final String name) {
 134  110
         if (this.fileFilters.isEmpty()) {
 135  2
             return false;
 136  
         }
 137  108
         for (final IOFileFilter fileFilter : fileFilters) {
 138  176
             if (!fileFilter.accept(file, name)) {
 139  80
                 return false;
 140  
             }
 141  96
         }
 142  28
         return true;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Provide a String representaion of this file filter.
 147  
      *
 148  
      * @return a String representaion
 149  
      */
 150  
     @Override
 151  
     public String toString() {
 152  106
         final StringBuilder buffer = new StringBuilder();
 153  106
         buffer.append(super.toString());
 154  106
         buffer.append("(");
 155  106
         if (fileFilters != null) {
 156  310
             for (int i = 0; i < fileFilters.size(); i++) {
 157  204
                 if (i > 0) {
 158  104
                     buffer.append(",");
 159  
                 }
 160  204
                 final Object filter = fileFilters.get(i);
 161  204
                 buffer.append(filter == null ? "null" : filter.toString());
 162  
             }
 163  
         }
 164  106
         buffer.append(")");
 165  106
         return buffer.toString();
 166  
     }
 167  
 
 168  
 }