KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openidex > search > SimpleSearchInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openidex.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.DataFolder;
29
30 /**
31  *
32  * @author Marian Petras
33  */

34 final class SimpleSearchInfo implements SearchInfo {
35
36     /**
37      * Empty search info object.
38      * Its method {@link SearchInfo#canSearch canSearch()}
39      * always returns <code>true</code>. Its iterator
40      * (returned by method
41      * {@link SearchInfo#objectsToSearch objectsToSearch()}) has no elements.
42      */

43     static final SearchInfo EMPTY_SEARCH_INFO
44         = new SearchInfo() {
45             public boolean canSearch() {
46                 return true;
47             }
48             public java.util.Iterator JavaDoc objectsToSearch() {
49                 return Collections.EMPTY_LIST.iterator();
50             }
51         };
52         
53     /** */
54     private final DataFolder rootFolder;
55     /** */
56     private final boolean recursive;
57     /** */
58     private final FileObjectFilter[] filters;
59     
60     /**
61      * Creates a new instance of SimpleSearchInfo
62      *
63      * @param folder <!-- PENDING -->
64      * @param filters <!-- PENDING, accepts null -->
65      * @exception java.lang.IllegalArgumentException
66      * if the <code>folder</code> argument is <code>null</code>
67      */

68     SimpleSearchInfo(DataFolder folder,
69                      boolean recursive,
70                      FileObjectFilter[] filters) {
71         if (folder == null) {
72             throw new IllegalArgumentException JavaDoc();
73         }
74         
75         if ((filters != null) && (filters.length == 0)) {
76             filters = null;
77         }
78         this.rootFolder = folder;
79         this.recursive = recursive;
80         this.filters = filters;
81     }
82
83     /**
84      */

85     public boolean canSearch() {
86         return (filters != null)
87                ? checkFolderAgainstFilters(rootFolder.getPrimaryFile())
88                : true;
89     }
90
91     /**
92      */

93     public Iterator JavaDoc objectsToSearch() {
94         return new SimpleSearchIterator(rootFolder,
95                                         recursive,
96                                         filters != null ? Arrays.asList(filters)
97                                                         : null);
98     }
99     
100     /**
101      */

102     private boolean checkFolderAgainstFilters(final FileObject folder) {
103         assert folder.isFolder();
104         
105         for (int i = 0; i < filters.length; i++) {
106             if (filters[i].traverseFolder(folder)
107                     == FileObjectFilter.DO_NOT_TRAVERSE) {
108                 return false;
109             }
110         }
111         return true;
112     }
113     
114 }
115
Popular Tags