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.

OrFileFilter.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2013 Decebal Suiu
  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 OR logic across a list of
  25. * file filters. This filter returns <code>true</code> if one filter in the
  26. * list return <code>true</code>. Otherwise, it returns <code>false</code>.
  27. * Checking of the file filter list stops when the first filter returns
  28. * <code>true</code>.
  29. *
  30. * @author Decebal Suiu
  31. */
  32. public class OrFileFilter implements FileFilter {
  33. /** The list of file filters. */
  34. private List<FileFilter> fileFilters;
  35. public OrFileFilter() {
  36. this(new ArrayList<FileFilter>());
  37. }
  38. public OrFileFilter(FileFilter... fileFilters) {
  39. this(Arrays.asList(fileFilters));
  40. }
  41. public OrFileFilter(List<FileFilter> fileFilters) {
  42. this.fileFilters = new ArrayList<>(fileFilters);
  43. }
  44. public OrFileFilter addFileFilter(FileFilter fileFilter) {
  45. fileFilters.add(fileFilter);
  46. return this;
  47. }
  48. public List<FileFilter> getFileFilters() {
  49. return Collections.unmodifiableList(fileFilters);
  50. }
  51. public boolean removeFileFilter(FileFilter fileFilter) {
  52. return fileFilters.remove(fileFilter);
  53. }
  54. public void setFileFilters(List<FileFilter> fileFilters) {
  55. this.fileFilters = new ArrayList<>(fileFilters);
  56. }
  57. @Override
  58. public boolean accept(File file) {
  59. if (this.fileFilters.size() == 0) {
  60. return true;
  61. }
  62. for (FileFilter fileFilter : this.fileFilters) {
  63. if (fileFilter.accept(file)) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. }