/*
 * @(#)EndsWithFilter.java
 *
 * Summary: Select files that end with a particular string.
 *
 * Copyright: (c) 2003-2010 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.5+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  2.5 2009-02-28 - CommandLine split off in its own package.
 */
package com.mindprod.filter;

import java.io.File;
import java.io.FilenameFilter;

/**
 * Select files that end with a particular string.
 * <p/>
 * Filters files whose file names end with a given string, case insensitive. Does not accept directories. See
 * DirListFilter. see also FileListFilter and ClamFilter.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.5 2009-02-28 - CommandLine split off in its own package.
 * @since 2003
 */
public final class EndsWithFilter implements FilenameFilter// note Sun's
        // spelling, not
        // FileNameFilter

    {
    // ------------------------------ CONSTANTS ------------------------------

    /**
     * true if debugging. Enables debugging harness.
     */
    private static final boolean DEBUGGING = false;

    // ------------------------------ FIELDS ------------------------------

    /**
     * we filter to accept only files starting with this string.
     */
    private final String endsWith;
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------

    /**
     * constructor
     *
     * @param endsWith string file must end with. Case Insensitive.
     */
    @SuppressWarnings( { "WeakerAccess", "SameParameterValue" } )
    public EndsWithFilter( String endsWith )
        {
        this.endsWith = endsWith.toLowerCase();
        }

    /**
     * Select only Files ending with our string.
     *
     * @param dir  the directory in which the file was found.
     * @param name the name of the file.
     *
     * @return true if and only if the name should be included in the file list; false otherwise.
     */
    @SuppressWarnings( { "SimplifiableIfStatement" } )
    public boolean accept( File dir, String name )
        {
        File f = new File( dir, name );
        if ( f.isDirectory() )
            {
            return false;
            }
        return name.toLowerCase().endsWith( endsWith );
        }

    // --------------------------- main() method ---------------------------

    /**
     * TEST harness.
     *
     * @param args not used.
     */
    public static void main( String[] args )
        {
        if ( DEBUGGING )
            {
            // find all just the files listed, case insensitive.
            FilenameFilter f = new EndsWithFilter( "filter.class" );

            String[] filenames = new File( "C:\\com\\mindprod\\filter" )
                    .list( f );

            for ( String filename : filenames )
                {
                System.out.println( filename );
                }
            }
        }
    }// end EndsWithFilter
