Coverage Report - org.apache.commons.io.filefilter.OrFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
OrFileFilter
100%
43/43
86%
19/22
2.4
 
 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 OR logic across a list of
 27  
  * file filters. This filter returns {@code true} if any 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 true}.
 31  
  *
 32  
  * @since 1.0
 33  
  * @version $Id: OrFileFilter.java 1415850 2012-11-30 20:51:39Z ggregory $
 34  
  * @see FileFilterUtils#or(IOFileFilter...)
 35  
  */
 36  
 public class OrFileFilter
 37  
         extends AbstractFileFilter
 38  
         implements ConditionalFileFilter, Serializable {
 39  
 
 40  
     /** The list of file filters. */
 41  
     private final List<IOFileFilter> fileFilters;
 42  
 
 43  
     /**
 44  
      * Constructs a new instance of <code>OrFileFilter</code>.
 45  
      *
 46  
      * @since 1.1
 47  
      */
 48  28
     public OrFileFilter() {
 49  28
         this.fileFilters = new ArrayList<IOFileFilter>();
 50  28
     }
 51  
 
 52  
     /**
 53  
      * Constructs a new instance of <code>OrFileFilter</code>
 54  
      * with the specified filters.
 55  
      *
 56  
      * @param fileFilters  the file filters for this filter, copied, null ignored
 57  
      * @since 1.1
 58  
      */
 59  94
     public OrFileFilter(final List<IOFileFilter> fileFilters) {
 60  94
         if (fileFilters == null) {
 61  2
             this.fileFilters = new ArrayList<IOFileFilter>();
 62  
         } else {
 63  92
             this.fileFilters = new ArrayList<IOFileFilter>(fileFilters);
 64  
         }
 65  94
     }
 66  
 
 67  
     /**
 68  
      * Constructs a new file filter that ORs the result of two other filters.
 69  
      * 
 70  
      * @param filter1  the first filter, must not be null
 71  
      * @param filter2  the second filter, must not be null
 72  
      * @throws IllegalArgumentException if either filter is null
 73  
      */
 74  18
     public OrFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
 75  18
         if (filter1 == null || filter2 == null) {
 76  2
             throw new IllegalArgumentException("The filters must not be null");
 77  
         }
 78  16
         this.fileFilters = new ArrayList<IOFileFilter>(2);
 79  16
         addFileFilter(filter1);
 80  16
         addFileFilter(filter2);
 81  16
     }
 82  
 
 83  
     /**
 84  
      * {@inheritDoc}
 85  
      */
 86  
     public void addFileFilter(final IOFileFilter ioFileFilter) {
 87  132
         this.fileFilters.add(ioFileFilter);
 88  132
     }
 89  
 
 90  
     /**
 91  
      * {@inheritDoc}
 92  
      */
 93  
     public List<IOFileFilter> getFileFilters() {
 94  34
         return Collections.unmodifiableList(this.fileFilters);
 95  
     }
 96  
 
 97  
     /**
 98  
      * {@inheritDoc}
 99  
      */
 100  
     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
 101  12
         return this.fileFilters.remove(ioFileFilter);
 102  
     }
 103  
 
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     public void setFileFilters(final List<IOFileFilter> fileFilters) {
 108  2
         this.fileFilters.clear();
 109  2
         this.fileFilters.addAll(fileFilters);
 110  2
     }
 111  
 
 112  
     /**
 113  
      * {@inheritDoc}
 114  
      */
 115  
     @Override
 116  
     public boolean accept(final File file) {
 117  2200
         for (final IOFileFilter fileFilter : fileFilters) {
 118  4024
             if (fileFilter.accept(file)) {
 119  792
                 return true;
 120  
             }
 121  3232
         }
 122  1408
         return false;
 123  
     }
 124  
 
 125  
     /**
 126  
      * {@inheritDoc}
 127  
      */
 128  
     @Override
 129  
     public boolean accept(final File file, final String name) {
 130  44
         for (final IOFileFilter fileFilter : fileFilters) {
 131  64
             if (fileFilter.accept(file, name)) {
 132  34
                 return true;
 133  
             }
 134  30
         }
 135  10
         return false;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Provide a String representaion of this file filter.
 140  
      *
 141  
      * @return a String representaion
 142  
      */
 143  
     @Override
 144  
     public String toString() {
 145  38
         final StringBuilder buffer = new StringBuilder();
 146  38
         buffer.append(super.toString());
 147  38
         buffer.append("(");
 148  38
         if (fileFilters != null) {
 149  96
             for (int i = 0; i < fileFilters.size(); i++) {
 150  58
                 if (i > 0) {
 151  30
                     buffer.append(",");
 152  
                 }
 153  58
                 final Object filter = fileFilters.get(i);
 154  58
                 buffer.append(filter == null ? "null" : filter.toString());
 155  
             }
 156  
         }
 157  38
         buffer.append(")");
 158  38
         return buffer.toString();
 159  
     }
 160  
 
 161  
 }