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 import org.apache.commons.lang3.Range; 20 21 /** 22 * A {@link FileSelector} that selects all files in a particular depth range. 23 */ 24 public class FileDepthSelector implements FileSelector { 25 26 private final Range<Integer> range; 27 28 /** 29 * Creates a selector with the given minimum and maximum depths. 30 * 31 * @param minDepth minimum depth 32 * @param maxDepth maximum depth 33 */ 34 public FileDepthSelector(final int minDepth, final int maxDepth) { 35 this.range = Range.between(minDepth, maxDepth); 36 } 37 38 /** 39 * Creates a selector with the same minimum and maximum depths. 40 * 41 * @param minMaxDepth minimum and maximum depth 42 * @since 2.1 43 */ 44 public FileDepthSelector(final int minMaxDepth) { 45 this(minMaxDepth, minMaxDepth); 46 } 47 48 /** 49 * Creates a selector with the same minimum and maximum depths of 0. 50 * 51 * @since 2.1 52 */ 53 public FileDepthSelector() { 54 this(0, 0); 55 } 56 57 /** 58 * Determines if a file or folder should be selected. 59 * 60 * @param fileInfo The file selection information 61 * @return true if the file or folder should be included, false otherwise. 62 */ 63 @Override 64 public boolean includeFile(final FileSelectInfo fileInfo) throws Exception { 65 return range.contains(fileInfo.getDepth()); 66 } 67 68 /** 69 * Determines whether a folder should be traversed. 70 * 71 * @param fileInfo The file selection information 72 * @return true if the file or folder should be traversed, false otherwise. 73 */ 74 @Override 75 public boolean traverseDescendents(final FileSelectInfo fileInfo) throws Exception { 76 return fileInfo.getDepth() < range.getMaximum(); 77 } 78 }