/*
 * @(#)FileLengthFilter.java
 *
 * Summary: Filters accepting only short or long files (not 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;

/**
 * Filters accepting only short or long files (not filenames).
 * <p/>
 * Use it like this to get a list of files shorter than 12
 * characters.
 * <p/>
 * FilenameFilter f = new FileLengthFilter( 12, false ); String[] filenames = new File ( &quot;MyDir&quot; ).list( f );
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.5 2009-02-28 - CommandLine split off in its own package.
 * @since 2003
 */
public final class FileLengthFilter implements FilenameFilter
    {
    // ------------------------------ CONSTANTS ------------------------------

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

    /**
     * convenience constant to remember that true means you want long files.
     */
    public static boolean LONG_FILES = true;

    /**
     * convenience constant to help you remember that false means accept short files.
     */
    public static boolean SHORT_FILES = false;

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

    /**
     * true if want long filenames, false if want short filenames.
     */
    private boolean wantLongFiles;

    /**
     * The minumum/maximum length of file we will let through our filter.
     */
    private long length;
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------

    /**
     * constructor
     *
     * @param length        The length of file you want to accept.
     * @param wantLongFiles true LONG_FILES if you want file longer or equal to the specified length. false SHORT_FILES
     *                      if you want filenames shorter or equal to the specified length.
     */
    public FileLengthFilter( long length, boolean wantLongFiles )
        {
        this.length = length;
        this.wantLongFiles = wantLongFiles;
        }

    /**
     * Select only files with appropriate length.
     *
     * @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 file = new File( dir, name );
        if ( file.isDirectory() )
            {
            return false;
            }
        long length = file.length();
        if ( length == this.length )
            {
            return true;
            }
        return ( length >= this.length ) == wantLongFiles;
        }

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

    /**
     * TEST harness
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        if ( DEBUGGING )
            {
            // get files 200 bytes or longer
            FilenameFilter f = new FileLengthFilter( 200, LONG_FILES );

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

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