/*
 * @(#)ClamFilter.java
 *
 * Summary: Select files that begin/end with a given 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 begin/end with a given string.
 * <p/>
 * filter out all but files/directories that begin and end with given string, a primitive sort of wildcard feature. Case
 * insensitive. If you just want extensions, the newer ExtensionListFilter is better. see FileListFilter if you just
 * have a list of specific files.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.5 2009-02-28 - CommandLine split off in its own package.
 * @since 2003
 */
public final class ClamFilter implements FilenameFilter
    {
    // ------------------------------ CONSTANTS ------------------------------

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

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

    /**
     * Filename to pass must start with this string.
     */
    private String mustEndWith;

    /**
     * filename to pass must start with this string.
     */
    private String mustStartWith;
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------

    /**
     * constructor
     *
     * @param mustStartWith string the filename must start with
     * @param mustEndWith   string the filename must end with, usually of form ".html" including the dot.
     */
    public ClamFilter( String mustStartWith, String mustEndWith )
        {
        this.mustStartWith = mustStartWith.toLowerCase();
        this.mustEndWith = mustEndWith.toLowerCase();
        }

    /**
     * Select only files with with appropriate beginning and end.
     *
     * @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 )
        {
        if ( !( new File( dir, name ).isFile() ) )
            {
            return false;
            }
        name = name.toLowerCase();
        return name.startsWith( mustStartWith ) && name.endsWith( mustEndWith );
        }

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

    /**
     * TEST harness
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        if ( DEBUGGING )
            {
            // find all cl*.java files, case insensitive.
            FilenameFilter f = new ClamFilter( "cl", ".java" );

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

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