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
019/**
020 * A {@link FileSelector} that selects all files in a particular depth range.
021 */
022public class FileDepthSelector implements FileSelector {
023    /**
024     * The minimum depth
025     */
026    private final int minDepth;
027
028    /**
029     * The maximum depth
030     */
031    private final int maxDepth;
032
033    /**
034     * Creates a selector with the given minimum and maximum depths.
035     *
036     * @param minDepth minimum depth
037     * @param maxDepth maximum depth
038     */
039    public FileDepthSelector(final int minDepth, final int maxDepth) {
040        this.minDepth = minDepth;
041        this.maxDepth = maxDepth;
042    }
043
044    /**
045     * Creates a selector with the same minimum and maximum depths.
046     *
047     * @param minMaxDepth minimum and maximum depth
048     * @since 2.1
049     */
050    public FileDepthSelector(final int minMaxDepth) {
051        this(minMaxDepth, minMaxDepth);
052    }
053
054    /**
055     * Creates a selector with the same minimum and maximum depths of 0.
056     *
057     * @since 2.1
058     */
059    public FileDepthSelector() {
060        this(0, 0);
061    }
062
063    /**
064     * Determines if a file or folder should be selected.
065     *
066     * @param fileInfo The file selection information
067     * @return true if the file or folder should be included, false otherwise.
068     */
069    @Override
070    public boolean includeFile(final FileSelectInfo fileInfo) {
071        final int depth = fileInfo.getDepth();
072        return minDepth <= depth && depth <= maxDepth;
073    }
074
075    /**
076     * Determines whether a folder should be traversed.
077     *
078     * @param fileInfo The file selection information
079     * @return true if the file or folder should be traversed, false otherwise.
080     */
081    @Override
082    public boolean traverseDescendents(final FileSelectInfo fileInfo) {
083        return fileInfo.getDepth() < maxDepth;
084    }
085}