File filter: retrieve at most predefined number of files which are older than specified data : FilenameFilter « File « Java Tutorial






/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 * 
 * Project: OpenSubsystems
 * 
 * $Id: MaximumFileFilter.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */


import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;

/**
 * File filter implementation to retrieve at most predefined number of files
 * which are older than specified data
 * 
 * @version $Id: MaximumFileFilter.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
 * @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
 */
public class MaximumFileFilter implements FilenameFilter
{
   // Attributes ///////////////////////////////////////////////////////////////
   
   /**
    * Current number of files in list
    */
   protected int m_currentFileCount = 0;
   
   /**
    * Maximum number of files in output list
    */
   
   protected int m_iMaximum = 0;
   
   /**
    * Last modification time of file have to be before this date, if it is null, 
    * modification date is not checked
    */
   protected Date m_dtOlderThan;
   
   // Constructors /////////////////////////////////////////////////////////////
   
   /**
    * Constructor with maximum count of file in the output list.
    *  
    * @param iMax - maximum number of files in output list. If it is 0 length of 
    * @param dtOlderThen  - file last modification time heve to by before this 
    *                       date, if it is null modification time is not checked
    *                       output list is unlimited.
    */
   
   public MaximumFileFilter(
      int  iMax,
      Date dtOlderThen
   )
   {
      m_iMaximum = iMax;
      m_dtOlderThan = dtOlderThen; 
   }
   
   /**
    * {@inheritDoc}
    */
   public boolean accept(
      File   dir, 
      String name
   )
   {
      File currentFile = new File(dir, name);
      
      if (currentFile.isDirectory())
      {
         return false;
      }
      if ((m_dtOlderThan != null) && (currentFile.lastModified() > m_dtOlderThan.getTime()))
      {
         return false;
      }
      if ((m_iMaximum == 0) || (m_currentFileCount <= m_iMaximum))
      {
         m_currentFileCount++;
      }
      return  m_currentFileCount <= m_iMaximum;
   }
}








11.71.FilenameFilter
11.71.1.Combine FilenameFilter and Pattern match to create a dir command
11.71.2.A prefix based filename filter
11.71.3.Filter files by name
11.71.4.strip Extension name
11.71.5.A filesystem filter.
11.71.6.File filter: retrieve at most predefined number of files which are older than specified data