/*
 * @(#)StartsWithFilter.java
 *
 * Summary: Filters files whose names start with a given string, case insensitive.
 *
 * Copyright: (c) 2007-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;

/**
 * Filters files whose names start with a given string, case insensitive.
 * <p/>
 * Does not accept directories. See
 * DirListFilter. see also FileListFilter and ClamFilter.
 * <p/>
 * @noinspection WeakerAccess
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.5 2009-02-28 - CommandLine split off in its own package.
 * @since 2007-03-20
 */
public final class StartsWithFilter 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 startsWith;
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------

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

    /**
     * Select only Files starting 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 final boolean accept( File dir, String name )
        {
        File f = new File( dir, name );
        if ( f.isDirectory() )
            {
            return false;
            }
        return name.toLowerCase().startsWith( startsWith );
        }

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

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

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

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