You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AndFileFilter.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2013 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j.util;
  14. import java.io.File;
  15. import java.io.FileFilter;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. /**
  21. * This filter providing conditional AND logic across a list of
  22. * file filters. This filter returns <code>true</code> if all filters in the
  23. * list return <code>true</code>. Otherwise, it returns <code>false</code>.
  24. * Checking of the file filter list stops when the first filter returns
  25. * <code>false</code>.
  26. *
  27. * @author Decebal Suiu
  28. */
  29. public class AndFileFilter implements FileFilter {
  30. /** The list of file filters. */
  31. private List<FileFilter> fileFilters;
  32. public AndFileFilter() {
  33. this.fileFilters = new ArrayList<FileFilter>();
  34. }
  35. public AndFileFilter(List<FileFilter> fileFilters) {
  36. this.fileFilters = new ArrayList<FileFilter>(fileFilters);
  37. }
  38. public void addFileFilter(FileFilter fileFilter) {
  39. fileFilters.add(fileFilter);
  40. }
  41. public List<FileFilter> getFileFilters() {
  42. return Collections.unmodifiableList(fileFilters);
  43. }
  44. public boolean removeFileFilter(FileFilter fileFilter) {
  45. return fileFilters.remove(fileFilter);
  46. }
  47. public void setFileFilters(List<FileFilter> fileFilters) {
  48. this.fileFilters = new ArrayList<FileFilter>(fileFilters);
  49. }
  50. @Override
  51. public boolean accept(File file) {
  52. if (this.fileFilters.size() == 0) {
  53. return false;
  54. }
  55. for (Iterator<FileFilter> iter = this.fileFilters.iterator(); iter.hasNext();) {
  56. FileFilter fileFilter = (FileFilter) iter.next();
  57. if (!fileFilter.accept(file)) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. }