KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > file > FilenamePrefixFilter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, 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.util.file;
23
24 import java.io.File JavaDoc;
25 import java.io.FilenameFilter JavaDoc;
26
27 /**
28  * A <em>prefix</em> based filename filter.
29  *
30  * @version <tt>$Revision: 1958 $</tt>
31  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
32  */

33 public class FilenamePrefixFilter
34    implements FilenameFilter JavaDoc
35 {
36    /** The prefix which files must have to be accepted. */
37    protected final String JavaDoc prefix;
38
39    /** Flag to signal that we want to ignore the case. */
40    protected final boolean ignoreCase;
41
42    /**
43     * Construct a <tt>FilenamePrefixFilter</tt>.
44     *
45     * @param prefix The prefix which files must have to be accepted.
46     * @param ignoreCase <tt>True</tt> if the filter should be case-insensitive.
47     */

48    public FilenamePrefixFilter(final String JavaDoc prefix,
49                                final boolean ignoreCase)
50    {
51       this.ignoreCase = ignoreCase;
52       this.prefix = (ignoreCase ? prefix.toLowerCase() : prefix);
53    }
54
55    /**
56     * Construct a case sensitive <tt>FilenamePrefixFilter</tt>.
57     *
58     * @param prefix The prefix which files must have to be accepted.
59     */

60    public FilenamePrefixFilter(final String JavaDoc prefix) {
61       this(prefix, false);
62    }
63
64    /**
65     * Check if a file is acceptible.
66     *
67     * @param dir The directory the file resides in.
68     * @param name The name of the file.
69     * @return <tt>true</tt> if the file is acceptable.
70     */

71    public boolean accept(final File JavaDoc dir, final String JavaDoc name) {
72       if (ignoreCase) {
73          return name.toLowerCase().startsWith(prefix);
74       }
75       else {
76          return name.startsWith(prefix);
77       }
78    }
79 }
80
Popular Tags