jdk/src/share/classes/java/nio/file/DirectoryStreamFilters.java
changeset 2057 3acf8e5e2ca0
child 2072 80dfe4469bbd
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2007-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package java.nio.file;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.io.IOError;
       
    30 import sun.nio.fs.MimeType;
       
    31 
       
    32 /**
       
    33  * This class consists exclusively of static methods that construct or combine
       
    34  * filters.
       
    35  *
       
    36  * @since 1.7
       
    37  */
       
    38 
       
    39 public final class DirectoryStreamFilters {
       
    40     private DirectoryStreamFilters() { }
       
    41 
       
    42     /**
       
    43      * Constructs a directory stream filter that filters directory entries by
       
    44      * their  <a href="http://www.ietf.org/rfc/rfc2045.txt">MIME</a> content
       
    45      * type. The directory stream filter's {@link
       
    46      * java.nio.file.DirectoryStream.Filter#accept accept} method returns {@code
       
    47      * true} if the content type of the directory entry can be determined by
       
    48      * invoking the {@link Files#probeContentType probeContentType} method, and
       
    49      * the content type matches the given content type.
       
    50      *
       
    51      * <p> The {@code type} parameter is the value of a Multipurpose Internet
       
    52      * Mail Extension (MIME) content type as defined by <a
       
    53      * href="http://www.ietf.org/rfc/rfc2045.txt"><i>RFC&nbsp;2045: Multipurpose
       
    54      * Internet Mail Extensions (MIME) Part One: Format of Internet Message
       
    55      * Bodies</i></a>. It is parsable according to the grammar in the RFC. Any
       
    56      * space characters (<code>'&#92;u0020'</code>) surrounding its components are
       
    57      * ignored. The {@code type} parameter is parsed into its primary and subtype
       
    58      * components which are used to match the primary and subtype components of
       
    59      * each directory entry's content type. Parameters are not allowed. The
       
    60      * primary type matches if it has value {@code '*'} or is equal to the
       
    61      * primary type of the directory entry's content type without regard to
       
    62      * case. The subtype matches if has the value {@code '*'} or is equal to the
       
    63      * subtype of the directory entry's content type without regard to case. If
       
    64      * both the primary and subtype match then the filter's {@code accept} method
       
    65      * returns {@code true}. If the content type of a directory entry cannot be
       
    66      * determined then the entry is filtered.
       
    67      *
       
    68      * <p> The {@code accept} method of the resulting directory stream filter
       
    69      * throws {@link IOError} if the probing of the content type fails by
       
    70      * throwing an {@link IOException}. Security exceptions are also propogated
       
    71      * to the caller of the {@code accept} method.
       
    72      *
       
    73      * <p> <b>Usage Example:</b>
       
    74      * Suppose we require to list only the HTML files in a directory.
       
    75      * <pre>
       
    76      *     DirectoryStream.Filter&lt;FileRef&gt; filter =
       
    77      *         DirectoryStreamFilters.newContentTypeFilter("text/html");
       
    78      * </pre>
       
    79      *
       
    80      * @param   type
       
    81      *          The content type
       
    82      *
       
    83      * @return  A new directory stream filter
       
    84      *
       
    85      * @throws  IllegalArgumentException
       
    86      *          If the {@code type} parameter cannot be parsed as a MIME type
       
    87      *          or it has parameters
       
    88      */
       
    89     public static <T extends FileRef> DirectoryStream.Filter<T>
       
    90         newContentTypeFilter(String type)
       
    91     {
       
    92         final MimeType matchType = MimeType.parse(type);
       
    93         if (matchType.hasParameters())
       
    94             throw new IllegalArgumentException("Parameters not allowed");
       
    95         return new DirectoryStream.Filter<T>() {
       
    96             @Override
       
    97             public boolean accept(T entry) {
       
    98                 String fileType;
       
    99                 try {
       
   100                     fileType = Files.probeContentType(entry);
       
   101                 } catch (IOException x) {
       
   102                     throw new IOError(x);
       
   103                 }
       
   104                 if (fileType != null) {
       
   105                     return matchType.match(fileType);
       
   106                 }
       
   107                 return false;
       
   108             }
       
   109         };
       
   110     }
       
   111 
       
   112     /**
       
   113      * Returns a directory stream filter that {@link DirectoryStream.Filter#accept
       
   114      * accepts} a directory entry if the entry is accepted by all of the given
       
   115      * filters.
       
   116      *
       
   117      * <p> This method returns a filter that invokes, in iterator order, the
       
   118      * {@code accept} method of each of the filters. If {@code false} is returned
       
   119      * by any of the filters then the directory entry is filtered. If the
       
   120      * directory entry is not filtered then the resulting filter accepts the
       
   121      * entry. If the iterator returns zero elements then the resulting filter
       
   122      * accepts all directory entries.
       
   123      *
       
   124      * <p> <b>Usage Example:</b>
       
   125      * <pre>
       
   126      *     List&lt;DirectoryStream.Filter&lt;? super Path&gt;&gt; filters = ...
       
   127      *     DirectoryStream.Filter&lt;Path&gt; filter = DirectoryStreamFilters.allOf(filters);
       
   128      * </pre>
       
   129      *
       
   130      * @param   filters
       
   131      *          The sequence of filters
       
   132      *
       
   133      * @return  The resulting filter
       
   134      */
       
   135     public static <T> DirectoryStream.Filter<T>
       
   136         allOf(final Iterable<? extends DirectoryStream.Filter<? super T>> filters)
       
   137     {
       
   138         if (filters == null)
       
   139             throw new NullPointerException("'filters' is null");
       
   140         return new DirectoryStream.Filter<T>() {
       
   141             @Override
       
   142             public boolean accept(T entry) {
       
   143                 for (DirectoryStream.Filter<? super T> filter: filters) {
       
   144                     if (!filter.accept(entry))
       
   145                         return false;
       
   146                 }
       
   147                 return true;
       
   148             }
       
   149         };
       
   150     }
       
   151 
       
   152     /**
       
   153      * Returns a directory stream filter that {@link DirectoryStream.Filter#accept
       
   154      * accepts} a directory entry if the entry is accepted by one or more of
       
   155      * the given filters.
       
   156      *
       
   157      * <p> This method returns a filter that invokes, in iteration order, the
       
   158      * {@code accept} method of each of filter. If {@code true} is returned by
       
   159      * any of the filters then the directory entry is accepted. If none of the
       
   160      * filters accepts the directory entry then it is filtered. If the iterator
       
   161      * returns zero elements then the resulting filter filters all directory
       
   162      * entries.
       
   163      *
       
   164      * @param   filters
       
   165      *          The sequence of filters
       
   166      *
       
   167      * @return  The resulting filter
       
   168      */
       
   169     public static <T> DirectoryStream.Filter<T>
       
   170         anyOf(final Iterable<? extends DirectoryStream.Filter<? super T>> filters)
       
   171     {
       
   172         if (filters == null)
       
   173             throw new NullPointerException("'filters' is null");
       
   174         return new DirectoryStream.Filter<T>() {
       
   175             @Override
       
   176             public boolean accept(T entry) {
       
   177                 for (DirectoryStream.Filter<? super T> filter: filters) {
       
   178                     if (filter.accept(entry))
       
   179                         return true;
       
   180                 }
       
   181                 return false;
       
   182             }
       
   183         };
       
   184     }
       
   185 
       
   186     /**
       
   187      * Returns a directory stream filter that is the <em>complement</em> of the
       
   188      * given filter. The resulting filter {@link
       
   189      * java.nio.file.DirectoryStream.Filter#accept accepts} a directory entry
       
   190      * if filtered by the given filter, and filters any entries that are accepted
       
   191      * by the given filter.
       
   192      *
       
   193      * @param   filter
       
   194      *          The given filter
       
   195      *
       
   196      * @return  The resulting filter that is the complement of the given filter
       
   197      */
       
   198     public static <T> DirectoryStream.Filter<T>
       
   199         complementOf(final DirectoryStream.Filter<T> filter)
       
   200     {
       
   201         if (filter == null)
       
   202             throw new NullPointerException("'filter' is null");
       
   203         return new DirectoryStream.Filter<T>() {
       
   204             @Override
       
   205             public boolean accept(T entry) {
       
   206                 return !filter.accept(entry);
       
   207             }
       
   208         };
       
   209     }
       
   210 }