/*
 * @(#)FileListFilter.java
 *
 * Summary: Selects files that match a one of a list of filenames.
 *
 * 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;
import java.util.HashSet;

/**
 * Selects files that match a one of a list of filenames.
 * <p/>
 * You provide an array of specific filenames that you want. Does not work with
 * Directories. See DirListFilter.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.5 2009-02-28 - CommandLine split off in its own package.
 * @since 2003-06-06
 */
public final class FileListFilter implements FilenameFilter
    {
    // ------------------------------ CONSTANTS ------------------------------

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

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

    /**
     * Collection of filenames describing files you do want, in lower case
     */
    private HashSet<String> want;
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------

    /**
     * constructor
     *
     * @param want Array of filenames without directory names you want. No wildcards. when old Javas die we will convert
     *             this to String...
     */
    public FileListFilter( String... want )
        {
        // we need them in lower case. We don't want to disturb the caller's
        // array.
        // we don't lower case the caller's array.

        this.want =
                new HashSet<String>( Math.max( ( int ) ( want.length / .75f ) + 1,
                        16 ) );
        if ( want == null )
            {
            return;
            }
        for ( int i = 0; i < want.length; i++ )
            {
            this.want.add( want[ i ].toLowerCase() );
            }
        }

    /**
     * Select only files with that pass muster
     *
     * @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.
     */
    public boolean accept( File dir, String name )
        {
        File f = new File( dir, name );
        if ( !f.isFile() )
            {
            return false;
            }
        return want.contains( name.toLowerCase() );
        }

    // --------------------------- 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 FileListFilter( new String[] {
                    "filter.use", "Just.bat", "notthere.txt" } );

            String[] filenames = new File( "." ).list( f );

            for ( int i = 0; i < filenames.length; i++ )
                {
                System.out.println( filenames[ i ] );
                }
            }
        }
    }// end FileListFilter
