Java File Name Get getFilenameFilter(final String pFilter, final boolean pExclude)

Here you can find the source of getFilenameFilter(final String pFilter, final boolean pExclude)

Description

Returns the file filter object.

License

Open Source License

Parameter

Parameter Description
pFilter The filter.
pExclude The exclude flag.

Declaration

public static final FilenameFilter getFilenameFilter(final String pFilter, final boolean pExclude) 

Method Source Code

//package com.java2s;
/**/*from w  w  w . ja  v  a2s .co  m*/
 * (C) Copyright 2007, Deft Labs.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.FilenameFilter;

public class Main {
    /**
     * Returns the file filter object. This checks the cache based on the
     * filter value.
     * @param pFilter The filter.
     * @param pExclude The exclude flag.
     */
    public static final FilenameFilter getFilenameFilter(final String pFilter, final boolean pExclude) {
        return getFilenameFilter(pFilter, pExclude, false);
    }

    /**
     * Returns the file filter object. This checks the cache based on the
     * filter value. This method can be refactored :-) In a hurry now,.
     * @param pFilter The filter.
     * @param pExclude The exclude flag.
     * @param pEndsWith  Make sure the suffix is there (in the filter).
     */
    public static final FilenameFilter getFilenameFilter(final String pFilter, final boolean pExclude,
            final boolean pEndsWith) {
        FilenameFilter filter;
        if (pExclude) {

            if (pEndsWith) {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return !pName.endsWith(pFilter);
                    }
                };
            } else {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return (pName.indexOf(pFilter) == -1) ? true : false;
                    }
                };
            }
        } else {
            if (pEndsWith) {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return pName.endsWith(pFilter);
                    }
                };
            } else {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return (pName.indexOf(pFilter) != -1) ? true : false;
                    }
                };
            }
        }

        return filter;
    }
}

Related

  1. getFileName(String url, String basePath)
  2. getFileNameAndExt(String s)
  3. getFileNameArrayFromDirectory(String directoryLocation)
  4. getFileNameFilter(final String endsWith)
  5. getFilenameFilter(final String extension)
  6. getFilenameForClassName(String className)
  7. getFileNameFromCanonical(String file)
  8. getFileNameFromFilePath(String filePath)
  9. getFileNameFromFilePath(String filepath)