From: Decebal Suiu Date: Thu, 24 Dec 2015 07:23:40 +0000 (+0200) Subject: Add NameFileFilter and OrFileFilter X-Git-Tag: release-0.12.0~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=730be40b1073bfd523be2669d1e2fec710e948d4;p=pf4j.git Add NameFileFilter and OrFileFilter --- diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/util/NameFileFilter.java b/pf4j/src/main/java/ro/fortsoft/pf4j/util/NameFileFilter.java new file mode 100644 index 0000000..9071882 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/util/NameFileFilter.java @@ -0,0 +1,40 @@ +/* + * Copyright 2015 Decebal Suiu + * + * Licensed 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 ro.fortsoft.pf4j.util; + +import java.io.File; +import java.io.FileFilter; + +/** + * Filter accepts any file with this name. The case of the filename is ignored. + * + * @author Decebal Suiu + */ +public class NameFileFilter implements FileFilter { + + private String name; + + public NameFileFilter(String name) { + this.name = name; + } + + @Override + public boolean accept(File file) { + // perform a case insensitive check. + return file.getName().equalsIgnoreCase(name); + } + +} diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/util/OrFileFilter.java b/pf4j/src/main/java/ro/fortsoft/pf4j/util/OrFileFilter.java new file mode 100644 index 0000000..33e3e83 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/util/OrFileFilter.java @@ -0,0 +1,84 @@ +/* + * Copyright 2013 Decebal Suiu + * + * Licensed 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 ro.fortsoft.pf4j.util; + +import java.io.File; +import java.io.FileFilter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * This filter providing conditional OR logic across a list of + * file filters. This filter returns true if one filter in the + * list return true. Otherwise, it returns false. + * Checking of the file filter list stops when the first filter returns + * true. + * + * @author Decebal Suiu + */ +public class OrFileFilter implements FileFilter { + + /** The list of file filters. */ + private List fileFilters; + + public OrFileFilter() { + this(new ArrayList()); + } + + public OrFileFilter(FileFilter... fileFilters) { + this(Arrays.asList(fileFilters)); + } + + public OrFileFilter(List fileFilters) { + this.fileFilters = new ArrayList<>(fileFilters); + } + + public OrFileFilter addFileFilter(FileFilter fileFilter) { + fileFilters.add(fileFilter); + + return this; + } + + public List getFileFilters() { + return Collections.unmodifiableList(fileFilters); + } + + public boolean removeFileFilter(FileFilter fileFilter) { + return fileFilters.remove(fileFilter); + } + + public void setFileFilters(List fileFilters) { + this.fileFilters = new ArrayList<>(fileFilters); + } + + @Override + public boolean accept(File file) { + if (this.fileFilters.size() == 0) { + return true; + } + + for (FileFilter fileFilter : this.fileFilters) { + if (fileFilter.accept(file)) { + return true; + } + } + + return false; + } + +}