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 1642757 2014-12-01 21:09:30Z sebb $
 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<IOFileFilter>();
 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<IOFileFilter>();
 65  
         } else {
 66  80
             this.fileFilters = new ArrayList<IOFileFilter>(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<IOFileFilter>(2);
 82  25
         addFileFilter(filter1);
 83  25
         addFileFilter(filter2);
 84  25
     }
 85  
 
 86  
     /**
 87  
      * {@inheritDoc}
 88  
      */
 89  
     public void addFileFilter(final IOFileFilter ioFileFilter) {
 90  100
         this.fileFilters.add(ioFileFilter);
 91  100
     }
 92  
 
 93  
     /**
 94  
      * {@inheritDoc}
 95  
      */
 96  
     public List<IOFileFilter> getFileFilters() {
 97  16
         return Collections.unmodifiableList(this.fileFilters);
 98  
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
 104  4
         return this.fileFilters.remove(ioFileFilter);
 105  
     }
 106  
 
 107  
     /**
 108  
      * {@inheritDoc}
 109  
      */
 110  
     public void setFileFilters(final List<IOFileFilter> fileFilters) {
 111  0
         this.fileFilters.clear();
 112  0
         this.fileFilters.addAll(fileFilters);
 113  0
     }
 114  
 
 115  
     /**
 116  
      * {@inheritDoc}
 117  
      */
 118  
     @Override
 119  
     public boolean accept(final File file) {
 120  1315
         if (this.fileFilters.isEmpty()) {
 121  3
             return false;
 122  
         }
 123  1312
         for (final IOFileFilter fileFilter : fileFilters) {
 124  2040
             if (!fileFilter.accept(file)) {
 125  990
                 return false;
 126  
             }
 127  1050
         }
 128  322
         return true;
 129  
     }
 130  
 
 131  
     /**
 132  
      * {@inheritDoc}
 133  
      */
 134  
     @Override
 135  
     public boolean accept(final File file, final String name) {
 136  55
         if (this.fileFilters.isEmpty()) {
 137  1
             return false;
 138  
         }
 139  54
         for (final IOFileFilter fileFilter : fileFilters) {
 140  88
             if (!fileFilter.accept(file, name)) {
 141  40
                 return false;
 142  
             }
 143  48
         }
 144  14
         return true;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Provide a String representaion of this file filter.
 149  
      *
 150  
      * @return a String representaion
 151  
      */
 152  
     @Override
 153  
     public String toString() {
 154  53
         final StringBuilder buffer = new StringBuilder();
 155  53
         buffer.append(super.toString());
 156  53
         buffer.append("(");
 157  53
         if (fileFilters != null) {
 158  155
             for (int i = 0; i < fileFilters.size(); i++) {
 159  102
                 if (i > 0) {
 160  52
                     buffer.append(",");
 161  
                 }
 162  102
                 final Object filter = fileFilters.get(i);
 163  102
                 buffer.append(filter == null ? "null" : filter.toString());
 164  
             }
 165  
         }
 166  53
         buffer.append(")");
 167  53
         return buffer.toString();
 168  
     }
 169  
 
 170  
 }