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 name.
  21. * <p>
  22. * For example, to print all files and directories in the
  23. * current directory whose name is <code>Test</code>:
  24. *
  25. * <pre>
  26. * File dir = new File(".");
  27. * String[] files = dir.list( new NameFileFilter("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.3 $ $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 NameFileFilter extends AbstractFileFilter {
  43. /** The filenames to search for */
  44. private String[] names;
  45. /**
  46. * Constructs a new name file filter for a single name.
  47. *
  48. * @param name the name to allow, must not be null
  49. * @throws IllegalArgumentException if the prefix is null
  50. */
  51. public NameFileFilter(String name) {
  52. if (name == null) {
  53. throw new IllegalArgumentException("The name must not be null");
  54. }
  55. this.names = new String[] {name};
  56. }
  57. /**
  58. * Constructs a new name file filter for any of an array of names.
  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 names the names to allow, must not be null
  64. * @throws IllegalArgumentException if the names array is null
  65. */
  66. public NameFileFilter(String[] names) {
  67. if (names == null) {
  68. throw new IllegalArgumentException("The array of names must not be null");
  69. }
  70. this.names = names;
  71. }
  72. /**
  73. * Constructs a new name file filter for a list of names.
  74. *
  75. * @param names the names to allow, must not be null
  76. * @throws IllegalArgumentException if the name list is null
  77. * @throws ClassCastException if the list does not contain Strings
  78. */
  79. public NameFileFilter(List names) {
  80. if (names == null) {
  81. throw new IllegalArgumentException("The list of names must not be null");
  82. }
  83. this.names = (String[]) names.toArray(new String[names.size()]);
  84. }
  85. /**
  86. * Checks to see if the filename matches.
  87. *
  88. * @param file the File to check
  89. * @return true if the filename matches
  90. */
  91. public boolean accept(File file) {
  92. String name = file.getName();
  93. for (int i = 0; i < this.names.length; i++) {
  94. if (name.equals(this.names[i])) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * Checks to see if the filename matches.
  102. *
  103. * @param file the File directory
  104. * @param name the filename
  105. * @return true if the filename matches
  106. */
  107. public boolean accept(File file, String name) {
  108. for (int i = 0; i < this.names.length; i++) {
  109. if (name.equals(this.names[i])) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. }