KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > MaximumFileFilter


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

21
22 package org.opensubsystems.core.util;
23
24 import java.io.File JavaDoc;
25 import java.io.FilenameFilter JavaDoc;
26 import java.util.Date JavaDoc;
27
28 /**
29  * File filter implementation to retrieve at most predefined number of files
30  * which are older than specified data
31  *
32  * @version $Id: MaximumFileFilter.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
33  * @author Miro Halas
34  * @code.reviewer Miro Halas
35  * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
36  */

37 public class MaximumFileFilter implements FilenameFilter JavaDoc
38 {
39    // Attributes ///////////////////////////////////////////////////////////////
40

41    /**
42     * Current number of files in list
43     */

44    protected int m_currentFileCount = 0;
45    
46    /**
47     * Maximum number of files in output list
48     */

49    
50    protected int m_iMaximum = 0;
51    
52    /**
53     * Last modification time of file have to be before this date, if it is null,
54     * modification date is not checked
55     */

56    protected Date JavaDoc m_dtOlderThan;
57    
58    // Constructors /////////////////////////////////////////////////////////////
59

60    /**
61     * Constructor with maximum count of file in the output list.
62     *
63     * @param iMax - maximum number of files in output list. If it is 0 length of
64     * @param dtOlderThen - file last modification time heve to by before this
65     * date, if it is null modification time is not checked
66     * output list is unlimited.
67     */

68    
69    public MaximumFileFilter(
70       int iMax,
71       Date JavaDoc dtOlderThen
72    )
73    {
74       m_iMaximum = iMax;
75       m_dtOlderThan = dtOlderThen;
76    }
77    
78    /**
79     * {@inheritDoc}
80     */

81    public boolean accept(
82       File JavaDoc dir,
83       String JavaDoc name
84    )
85    {
86       File JavaDoc currentFile = new File JavaDoc(dir, name);
87       
88       if (currentFile.isDirectory())
89       {
90          return false;
91       }
92       if ((m_dtOlderThan != null) && (currentFile.lastModified() > m_dtOlderThan.getTime()))
93       {
94          return false;
95       }
96       if ((m_iMaximum == 0) || (m_currentFileCount <= m_iMaximum))
97       {
98          m_currentFileCount++;
99       }
100       return m_currentFileCount <= m_iMaximum;
101    }
102 }
103
104
Popular Tags