KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > JEFileFilter


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: JEFileFilter.java,v 1.18 2006/10/30 21:14:20 bostic Exp $
7  */

8
9 package com.sleepycat.je.log;
10
11 import java.io.File JavaDoc;
12 import java.io.FilenameFilter JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14
15 /**
16  * JEFileFilters are used for listing je files.
17  */

18 class JEFileFilter implements FilenameFilter JavaDoc {
19     String JavaDoc[] suffix;
20     long minFileNumber = 0;
21     long maxFileNumber = -1;
22
23     JEFileFilter(String JavaDoc[] suffix) {
24         this.suffix = suffix;
25     }
26
27     /**
28      * @param maxFileNumber this filter will only return
29      * files that are numbers <= maxFileNumber.
30      */

31     JEFileFilter(String JavaDoc[] suffix, long maxFileNumber) {
32         this.suffix = suffix;
33         this.maxFileNumber = maxFileNumber;
34     }
35
36     /**
37      * @param minFileNumber this filter will only return files that are >=
38      * minFileNumber.
39      * @param maxFileNumber this filter will only return
40      * files that are numbers <= maxFileNumber.
41      */

42     JEFileFilter(String JavaDoc[] suffix, long minFileNumber, long maxFileNumber) {
43         this.suffix = suffix;
44         this.minFileNumber = minFileNumber;
45         this.maxFileNumber = maxFileNumber;
46     }
47
48     private boolean matches(String JavaDoc fileSuffix) {
49     for (int i = 0; i < suffix.length; i++) {
50         if (fileSuffix.equalsIgnoreCase(suffix[i])) {
51         return true;
52         }
53     }
54     return false;
55     }
56
57     /**
58      * A JE file has to be of the format nnnnnnnn.suffix.
59      */

60     public boolean accept(File JavaDoc dir, String JavaDoc name) {
61         boolean ok = false;
62         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, ".");
63         /* There should be two parts. */
64     int nTokens = tokenizer.countTokens();
65         if (nTokens == 2 || nTokens == 3) {
66         boolean hasVersion = (nTokens == 3);
67             String JavaDoc fileNumber = tokenizer.nextToken();
68             String JavaDoc fileSuffix = "." + tokenizer.nextToken();
69         String JavaDoc fileVersion = (hasVersion ? tokenizer.nextToken() : null);
70
71             /* Check the length and the suffix. */
72             if ((fileNumber.length() == 8) &&
73         matches(fileSuffix)) {
74                 //(fileSuffix.equalsIgnoreCase(suffix))) {
75

76                 /* The first part should be a number. */
77                 try {
78                     long fileNum = Long.parseLong(fileNumber, 16);
79                     if (fileNum < minFileNumber) {
80                         ok = false;
81                     } else if ((fileNum <= maxFileNumber) ||
82                                (maxFileNumber == -1)) {
83                     ok = true;
84                     }
85                 } catch (NumberFormatException JavaDoc e) {
86                     ok = false;
87                 }
88         if (hasVersion) {
89             try {
90             Integer.parseInt(fileVersion);
91             ok = true;
92             } catch (NumberFormatException JavaDoc e) {
93             ok = false;
94             }
95         }
96             }
97         }
98
99         return ok;
100     }
101 }
102
103
Popular Tags