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