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

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