001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2;
018
019import org.apache.commons.lang3.Range;
020
021/**
022 * A {@link FileSelector} that selects all files in a particular depth range.
023 */
024public class FileDepthSelector implements FileSelector {
025
026    private final Range<Integer> range;
027
028    /**
029     * Creates a selector with the same minimum and maximum depths of 0.
030     *
031     * @since 2.1
032     */
033    public FileDepthSelector() {
034        this(0, 0);
035    }
036
037    /**
038     * Creates a selector with the same minimum and maximum depths.
039     *
040     * @param minMaxDepth minimum and maximum depth
041     * @since 2.1
042     */
043    public FileDepthSelector(final int minMaxDepth) {
044        this(minMaxDepth, minMaxDepth);
045    }
046
047    /**
048     * Creates a selector with the given minimum and maximum depths.
049     *
050     * @param minDepth minimum depth
051     * @param maxDepth maximum depth
052     */
053    public FileDepthSelector(final int minDepth, final int maxDepth) {
054        range = Range.between(minDepth, maxDepth);
055    }
056
057    /**
058     * Determines if a file or folder should be selected.
059     *
060     * @param fileInfo The file selection information
061     * @return true if the file or folder should be included, false otherwise.
062     */
063    @Override
064    public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
065        return range.contains(fileInfo.getDepth());
066    }
067
068    /**
069     * Determines whether a folder should be traversed.
070     *
071     * @param fileInfo The file selection information
072     * @return true if the file or folder should be traversed, false otherwise.
073     */
074    @Override
075    public boolean traverseDescendents(final FileSelectInfo fileInfo) throws Exception {
076        return fileInfo.getDepth() < range.getMaximum();
077    }
078}