View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  /**
20   * A {@link FileSelector} that selects all files in a particular depth range.
21   */
22  public class FileDepthSelector implements FileSelector
23  {
24      /**
25       * The minimum depth
26       */
27      private final int minDepth;
28  
29      /**
30       * The maximum depth
31       */
32      private final int maxDepth;
33  
34      /**
35       * Creates a selector with the given minimum and maximum depths.
36       *
37       * @param minDepth
38       *            minimum depth
39       * @param maxDepth
40       *            maximum depth
41       */
42      public FileDepthSelector(final int minDepth, final int maxDepth)
43      {
44          this.minDepth = minDepth;
45          this.maxDepth = maxDepth;
46      }
47  
48      /**
49       * Creates a selector with the same minimum and maximum depths.
50       *
51       * @param minMaxDepth
52       *            minimum and maximum depth
53       * @since 2.1
54       */
55      public FileDepthSelector(final int minMaxDepth)
56      {
57          this(minMaxDepth, minMaxDepth);
58      }
59  
60      /**
61       * Creates a selector with the same minimum and maximum depths of 0.
62       *
63       * @since 2.1
64       */
65      public FileDepthSelector()
66      {
67          this(0, 0);
68      }
69  
70      /**
71       * Determines if a file or folder should be selected.
72       *
73       * @param fileInfo
74       *            The file selection information
75       * @return true if the file or folder should be included, false otherwise.
76       */
77      @Override
78      public boolean includeFile(final FileSelectInfo fileInfo)
79      {
80          final int depth = fileInfo.getDepth();
81          return minDepth <= depth && depth <= maxDepth;
82      }
83  
84      /**
85       * Determines whether a folder should be traversed.
86       *
87       * @param fileInfo
88       *            The file selection information
89       * @return true if the file or folder should be traversed, false otherwise.
90       */
91      @Override
92      public boolean traverseDescendents(final FileSelectInfo fileInfo)
93      {
94          return fileInfo.getDepth() < maxDepth;
95      }
96  }