1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation.
  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.apache.commons.io.filefilter;
  17. import java.io.File;
  18. import java.util.List;
  19. /**
  20. * Filters files based on the suffix (what the filename ends with).
  21. * This is used in retrieving all the files of a particular type.
  22. * <p>
  23. * For example, to retrieve and print all <code>*.java</code> files
  24. * in the current directory:
  25. *
  26. * <pre>
  27. * File dir = new File(".");
  28. * String[] files = dir.list( new SuffixFileFilter(".java") );
  29. * for (int i = 0; i < files.length; i++) {
  30. * System.out.println(files[i]);
  31. * }
  32. * </pre>
  33. *
  34. * @since Commons IO 1.0
  35. * @version $Revision: 1.6 $ $Date: 2004/02/23 04:37:57 $
  36. *
  37. * @author Henri Yandell
  38. * @author Stephen Colebourne
  39. * @author Federico Barbieri
  40. * @author Serge Knystautas
  41. * @author Peter Donald
  42. */
  43. public class SuffixFileFilter extends AbstractFileFilter {
  44. /** The filename suffixes to search for */
  45. private String[] suffixes;
  46. /**
  47. * Constructs a new Suffix file filter for a single extension.
  48. *
  49. * @param suffix the suffix to allow, must not be null
  50. * @throws IllegalArgumentException if the suffix is null
  51. */
  52. public SuffixFileFilter(String suffix) {
  53. if (suffix == null) {
  54. throw new IllegalArgumentException("The suffix must not be null");
  55. }
  56. this.suffixes = new String[] {suffix};
  57. }
  58. /**
  59. * Constructs a new Suffix file filter for an array of suffixs.
  60. * <p>
  61. * The array is not cloned, so could be changed after constructing the
  62. * instance. This would be inadvisable however.
  63. *
  64. * @param suffixes the suffixes to allow, must not be null
  65. * @throws IllegalArgumentException if the suffix array is null
  66. */
  67. public SuffixFileFilter(String[] suffixes) {
  68. if (suffixes == null) {
  69. throw new IllegalArgumentException("The array of suffixes must not be null");
  70. }
  71. this.suffixes = suffixes;
  72. }
  73. /**
  74. * Constructs a new Suffix file filter for a list of suffixes.
  75. *
  76. * @param suffixes the suffixes to allow, must not be null
  77. * @throws IllegalArgumentException if the suffix list is null
  78. * @throws ClassCastException if the list does not contain Strings
  79. */
  80. public SuffixFileFilter(List suffixes) {
  81. if (suffixes == null) {
  82. throw new IllegalArgumentException("The list of suffixes must not be null");
  83. }
  84. this.suffixes = (String[]) suffixes.toArray(new String[suffixes.size()]);
  85. }
  86. /**
  87. * Checks to see if the filename ends with the suffix.
  88. *
  89. * @param file the File to check
  90. * @return true if the filename ends with one of our suffixes
  91. */
  92. public boolean accept(File file) {
  93. String name = file.getName();
  94. for (int i = 0; i < this.suffixes.length; i++) {
  95. if (name.endsWith(this.suffixes[i])) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /**
  102. * Checks to see if the filename ends with the suffix.
  103. *
  104. * @param file the File directory
  105. * @param name the filename
  106. * @return true if the filename ends with one of our suffixes
  107. */
  108. public boolean accept(File file, String name) {
  109. for (int i = 0; i < this.suffixes.length; i++) {
  110. if (name.endsWith(this.suffixes[i])) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. }