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$
 34  
  * @see FileFilterUtils#or(IOFileFilter...)
 35  
  */
 36  
 public class OrFileFilter
 37  
         extends AbstractFileFilter
 38  
         implements ConditionalFileFilter, Serializable {
 39  
 
 40  
     private static final long serialVersionUID = 5767770777065432721L;
 41  
     /** The list of file filters. */
 42  
     private final List<IOFileFilter> fileFilters;
 43  
 
 44  
     /**
 45  
      * Constructs a new instance of <code>OrFileFilter</code>.
 46  
      *
 47  
      * @since 1.1
 48  
      */
 49  28
     public OrFileFilter() {
 50  28
         this.fileFilters = new ArrayList<>();
 51  28
     }
 52  
 
 53  
     /**
 54  
      * Constructs a new instance of <code>OrFileFilter</code>
 55  
      * with the specified filters.
 56  
      *
 57  
      * @param fileFilters  the file filters for this filter, copied, null ignored
 58  
      * @since 1.1
 59  
      */
 60  94
     public OrFileFilter(final List<IOFileFilter> fileFilters) {
 61  94
         if (fileFilters == null) {
 62  2
             this.fileFilters = new ArrayList<>();
 63  
         } else {
 64  92
             this.fileFilters = new ArrayList<>(fileFilters);
 65  
         }
 66  94
     }
 67  
 
 68  
     /**
 69  
      * Constructs a new file filter that ORs 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  18
     public OrFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
 76  18
         if (filter1 == null || filter2 == null) {
 77  2
             throw new IllegalArgumentException("The filters must not be null");
 78  
         }
 79  16
         this.fileFilters = new ArrayList<>(2);
 80  16
         addFileFilter(filter1);
 81  16
         addFileFilter(filter2);
 82  16
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     @Override
 88  
     public void addFileFilter(final IOFileFilter ioFileFilter) {
 89  132
         this.fileFilters.add(ioFileFilter);
 90  132
     }
 91  
 
 92  
     /**
 93  
      * {@inheritDoc}
 94  
      */
 95  
     @Override
 96  
     public List<IOFileFilter> getFileFilters() {
 97  34
         return Collections.unmodifiableList(this.fileFilters);
 98  
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     @Override
 104  
     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
 105  12
         return this.fileFilters.remove(ioFileFilter);
 106  
     }
 107  
 
 108  
     /**
 109  
      * {@inheritDoc}
 110  
      */
 111  
     @Override
 112  
     public void setFileFilters(final List<IOFileFilter> fileFilters) {
 113  2
         this.fileFilters.clear();
 114  2
         this.fileFilters.addAll(fileFilters);
 115  2
     }
 116  
 
 117  
     /**
 118  
      * {@inheritDoc}
 119  
      */
 120  
     @Override
 121  
     public boolean accept(final File file) {
 122  2347
         for (final IOFileFilter fileFilter : fileFilters) {
 123  4314
             if (fileFilter.accept(file)) {
 124  815
                 return true;
 125  
             }
 126  3499
         }
 127  1532
         return false;
 128  
     }
 129  
 
 130  
     /**
 131  
      * {@inheritDoc}
 132  
      */
 133  
     @Override
 134  
     public boolean accept(final File file, final String name) {
 135  44
         for (final IOFileFilter fileFilter : fileFilters) {
 136  64
             if (fileFilter.accept(file, name)) {
 137  34
                 return true;
 138  
             }
 139  30
         }
 140  10
         return false;
 141  
     }
 142  
 
 143  
     /**
 144  
      * Provide a String representation of this file filter.
 145  
      *
 146  
      * @return a String representation
 147  
      */
 148  
     @Override
 149  
     public String toString() {
 150  38
         final StringBuilder buffer = new StringBuilder();
 151  38
         buffer.append(super.toString());
 152  38
         buffer.append("(");
 153  38
         if (fileFilters != null) {
 154  96
             for (int i = 0; i < fileFilters.size(); i++) {
 155  58
                 if (i > 0) {
 156  30
                     buffer.append(",");
 157  
                 }
 158  58
                 final Object filter = fileFilters.get(i);
 159  58
                 buffer.append(filter == null ? "null" : filter.toString());
 160  
             }
 161  
         }
 162  38
         buffer.append(")");
 163  38
         return buffer.toString();
 164  
     }
 165  
 
 166  
 }