KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > vfs > helpers > FilterVirtualFileVisitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.virtual.plugins.vfs.helpers;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.jboss.virtual.VirtualFile;
29 import org.jboss.virtual.VirtualFileFilter;
30 import org.jboss.virtual.VirtualFileFilterWithAttributes;
31 import org.jboss.virtual.VisitorAttributes;
32
33 /**
34  * A visitor based on a virtual file filter
35  *
36  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
37  * @version $Revision: 1.1 $
38  */

39 public class FilterVirtualFileVisitor extends AbstractVirtualFileVisitor
40 {
41    /** The filter */
42    private final VirtualFileFilter filter;
43    
44    /** What is matched */
45    private List JavaDoc<VirtualFile> matched;
46
47    /**
48     * Check the attributes
49     *
50     * @param filter the filter
51     * @param attributes the attributes
52     * @return the attributes
53     * @throws IllegalArgumentException for a null filter
54     */

55    private static VisitorAttributes checkAttributes(VirtualFileFilter filter, VisitorAttributes attributes)
56    {
57       if (filter == null)
58          throw new IllegalArgumentException JavaDoc("Null filter");
59
60       // Specified
61
if (attributes != null)
62          return attributes;
63       
64       // From the filter
65
if (filter instanceof VirtualFileFilterWithAttributes)
66          return ((VirtualFileFilterWithAttributes) filter).getAttributes();
67       
68       // It will use the default
69
return null;
70    }
71    
72    /**
73     * Create a new FilterVirtualFileVisitor with default attributes
74     *
75     * @param filter the filter
76     * @throws IllegalArgumentException if the filter is null
77     */

78    public FilterVirtualFileVisitor(VirtualFileFilter filter)
79    {
80       this(filter, null);
81    }
82    
83    /**
84     * Create a new FilterVirtualFileVisitor.
85     *
86     * @param filter the filter
87     * @param attributes the attributes, uses the default if null
88     * @throws IllegalArgumentException if the filter is null
89     */

90    public FilterVirtualFileVisitor(VirtualFileFilter filter, VisitorAttributes attributes)
91    {
92       super(checkAttributes(filter, attributes));
93       this.filter = filter;
94    }
95
96    /**
97     * Get the matched files
98     *
99     * @return the matched files
100     */

101    public List JavaDoc<VirtualFile> getMatched()
102    {
103       if (matched == null)
104          return Collections.emptyList();
105       else
106          return matched;
107    }
108    
109    public void visit(VirtualFile virtualFile)
110    {
111       if (filter.accepts(virtualFile))
112       {
113          if (matched == null)
114             matched = new ArrayList JavaDoc<VirtualFile>();
115          matched.add(virtualFile);
116       }
117    }
118 }
119
Popular Tags