alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

JMeter example source code file (JMeterFileFilter.java)

This example JMeter source code file (JMeterFileFilter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - JMeter tags/keywords

io, jmeter, jmeter, jmeterfilefilter, jmeterfilefilter, override, override, should, string, string, util

The JMeter JMeterFileFilter.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.jmeter.gui;

import java.io.File;
import java.util.Arrays;

/**
 * A file filter which allows files to be filtered based on a list of allowed
 * extensions.
 *
 * Optionally returns directories.
 *
 */
public class JMeterFileFilter extends javax.swing.filechooser.FileFilter implements java.io.FileFilter {
    /** The list of extensions allowed by this filter. */
    private final String[] exts;

    private final boolean allowDirs; // Should we allow directories?

    /**
     * Create a new JMeter file filter which allows the specified extensions. If
     * the array of extensions contains no elements, any file will be allowed.
     *
     * This constructor will also return all directories
     *
     * @param extensions
     *            non-null array of allowed file extensions
     */
    public JMeterFileFilter(String[] extensions) {
        this(extensions,true);
    }

    /**
     * Create a new JMeter file filter which allows the specified extensions. If
     * the array of extensions contains no elements, any file will be allowed.
     *
     * @param extensions non-null array of allowed file extensions
     * @param allow should directories be returned ?
     */
    public JMeterFileFilter(String[] extensions, boolean allow) {
        exts = extensions;
        allowDirs = allow;
    }

    /**
     * Determine if the specified file is allowed by this filter. The file will
     * be allowed if it is a directory, or if the end of the filename matches
     * one of the extensions allowed by this filter. The filename is converted
     * to lower-case before making the comparison.
     *
     * @param f
     *            the File being tested
     *
     * @return true if the file should be allowed, false otherwise
     */
    @Override
    public boolean accept(File f) {
        return (allowDirs && f.isDirectory()) || accept(f.getName().toLowerCase());
        // TODO - why lower case? OK to use the default Locale?
    }

    /**
     * Determine if the specified filename is allowed by this filter. The file
     * will be allowed if the end of the filename matches one of the extensions
     * allowed by this filter. The comparison is case-sensitive. If no
     * extensions were provided for this filter, the file will always be
     * allowed.
     *
     * @param filename
     *            the filename to test
     * @return true if the file should be allowed, false otherwise
     */
    public boolean accept(String filename) {
        if (exts.length == 0) {
            return true;
        }

        for (int i = 0; i < exts.length; i++) {
            if (filename.endsWith(exts[i])) {
                return true;
            }
        }

        return false;
    }

    /**
     * Get a description for this filter.
     *
     * @return a description for this filter
     */
    @Override
    public String getDescription() {
        return "JMeter " + Arrays.asList(exts).toString();
    }
}

Other JMeter examples (source code examples)

Here is a short list of links related to this JMeter JMeterFileFilter.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.